比较和组合计算 属性 的日期时间戳的最佳方法是什么?
What's the best way to compare and combine the DateTime stamp as calculated property?
创建以下脚本是为了从所有域控制器获取实际的最新 AD LastLogon。
我需要从 LastLogon 属性中获取最早的日期或最近的条目。
然而,结果是不同的,而且很混乱,如下所示:
这是我到目前为止提出的脚本:
function Get-ADUserLastLogon([string]$userName) {
$dcs = Get-ADDomainController -Filter { Name -like "*" }
$time = 0
foreach ($dc in $dcs) {
Write-Host "Processing $($userName) ... $($dc.HostName) [$($(Resolve-DnsName -Name $dc.hostname).IPaddress)]" -ForegroundColor Green
$user = Get-ADUser $userName | Get-ADObject -Properties lastLogon
if ($user.LastLogon -gt $time) {
$time = $user.LastLogon
}
}
$dt = [DateTime]::FromFileTime($time)
Write-Host "`n" $username "last logged on at:" $dt " `n" -ForegroundColor Cyan
return $dt
}
$filter = { Enabled -eq $True}
$properties = 'Name', 'canonicalName', 'whenCreated', 'displayName', 'lastlogondate', 'lastlogon'
$Name = 'Enterprise.Admins'
Get-ADUser $Name -Properties $properties |
Select-Object -Property DisplayName,
SamAccountName,
UserPrincipalName,
WhenCreated,
@{ n = 'Most Recent Logon'; e = { Get-ADUserLastLogon -UserName $_.SamaccountName } },
@{ n = 'Real Last Logon'; e = { $sam = $_.SamaccountName; [datetime]::FromFileTime((Get-ADDomainController -Filter { Name -like "*" } | ForEach { Write-Host "Server: $($_.Name) - DisplayName: $($sam)" -ForegroundColor Yellow ; Get-ADUser $sam -Properties $properties -Server $_.Name | Select-Object LastLogon} | Measure-Object -Property LastLogon -Maximum).Maximum) } },
LastLogonDate | Out-GridView
如何将以上三列Real Last Logon、Most Recent Logon和LastLogonDate[=26]组合起来=] 到一个适当的列?
听起来您的目标是提取三个日期时间值,将它们相互比较,return最近的一个。在这种情况下,您可以像这样将所有变量传递给 Measure-Object:
(($MostRecentLogon,$RealLastLogon,$LastLogonDate) | Measure-Object -Maximum).Maximum
这将比较所有三个日期并输出最大值(即最近的日期)。
至于...
"How can we combine the above three columns Real Last Logon, Most Recent Logon and LastLogonDate into one proper column?"
我没有方便使用它的 ADDS 环境,所以只能使用文件系统和随机文本内容。
您可以对所有三个日期使用一个计算得出的 属性。比如说,@{Name = 'LogOnDates';Expression {'你所有的日期信息都在这里'}},但这将在该单列中显示为单行表示。
Clear-Host
Get-ChildItem -Path 'D:\Temp\*.txt' |
Select-Object -First 1 |
Select-Object -Property FullName,
@{
Name = 'DateDetails'
Expression = {@(
$PSItem.CreationTime,
$PSItem.LastAccessTime,
$PSItem.LastWriteTime
)
}
} | Out-Gridview
然而,听起来您所追求的也是让这些航向,而这不是 OGV 能够做到的。您必须定义自己的 OGV 自定义版本或执行以下操作:
Clear-Host
Get-ChildItem -Path 'D:\Temp\*.txt' |
Select-Object -First 1 |
Select-Object -Property FullName,
@{
Name = 'DateDetails'
Expression = {@(
[PSCUstomObject]@{
CreationTime = $PSItem.CreationTime
LastAccessTime = $PSItem.LastAccessTime
LastWriteTime = $PSItem.LastWriteTime
}
)
}
} | Out-Gridview
或者通过这样的方法获得更多创意:
$col = @(
(New-Object –TypeName PSObject –Prop @{'id'='01';'name'='a';'items'=@('the first item','the second item', 'the third item')}),
(New-Object –TypeName PSObject –Prop @{'id'='02';'name'='b';'items'=@('the first item','the second item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item')}),
(New-Object –TypeName PSObject –Prop @{'id'='03';'name'='c';'items'=@('the first item','the second item', 'the third item')})
)
$col | Select -p id,@{E={$_.items -join "`n"};L="Items"},name | Out-GridView
或者这样的技巧:
('[{"a":1,"b":2},{"a":3,"b":4},{"a":5,"b":6}]' |
ConvertFrom-Json) |
Out-GridView
或
$List = @()
$List += [pscustomobject]@{
Column1 = "abc"
Column2 = "cde"
Column3 = "fgd"
}
$List += [pscustomobject]@{
Column1 = "cab"
Column2 = "dre"
Column3 = "fde"
}
$List | Out-GridView
创建以下脚本是为了从所有域控制器获取实际的最新 AD LastLogon。 我需要从 LastLogon 属性中获取最早的日期或最近的条目。
然而,结果是不同的,而且很混乱,如下所示:
这是我到目前为止提出的脚本:
function Get-ADUserLastLogon([string]$userName) {
$dcs = Get-ADDomainController -Filter { Name -like "*" }
$time = 0
foreach ($dc in $dcs) {
Write-Host "Processing $($userName) ... $($dc.HostName) [$($(Resolve-DnsName -Name $dc.hostname).IPaddress)]" -ForegroundColor Green
$user = Get-ADUser $userName | Get-ADObject -Properties lastLogon
if ($user.LastLogon -gt $time) {
$time = $user.LastLogon
}
}
$dt = [DateTime]::FromFileTime($time)
Write-Host "`n" $username "last logged on at:" $dt " `n" -ForegroundColor Cyan
return $dt
}
$filter = { Enabled -eq $True}
$properties = 'Name', 'canonicalName', 'whenCreated', 'displayName', 'lastlogondate', 'lastlogon'
$Name = 'Enterprise.Admins'
Get-ADUser $Name -Properties $properties |
Select-Object -Property DisplayName,
SamAccountName,
UserPrincipalName,
WhenCreated,
@{ n = 'Most Recent Logon'; e = { Get-ADUserLastLogon -UserName $_.SamaccountName } },
@{ n = 'Real Last Logon'; e = { $sam = $_.SamaccountName; [datetime]::FromFileTime((Get-ADDomainController -Filter { Name -like "*" } | ForEach { Write-Host "Server: $($_.Name) - DisplayName: $($sam)" -ForegroundColor Yellow ; Get-ADUser $sam -Properties $properties -Server $_.Name | Select-Object LastLogon} | Measure-Object -Property LastLogon -Maximum).Maximum) } },
LastLogonDate | Out-GridView
如何将以上三列Real Last Logon、Most Recent Logon和LastLogonDate[=26]组合起来=] 到一个适当的列?
听起来您的目标是提取三个日期时间值,将它们相互比较,return最近的一个。在这种情况下,您可以像这样将所有变量传递给 Measure-Object:
(($MostRecentLogon,$RealLastLogon,$LastLogonDate) | Measure-Object -Maximum).Maximum
这将比较所有三个日期并输出最大值(即最近的日期)。
至于...
"How can we combine the above three columns Real Last Logon, Most Recent Logon and LastLogonDate into one proper column?"
我没有方便使用它的 ADDS 环境,所以只能使用文件系统和随机文本内容。
您可以对所有三个日期使用一个计算得出的 属性。比如说,@{Name = 'LogOnDates';Expression {'你所有的日期信息都在这里'}},但这将在该单列中显示为单行表示。
Clear-Host
Get-ChildItem -Path 'D:\Temp\*.txt' |
Select-Object -First 1 |
Select-Object -Property FullName,
@{
Name = 'DateDetails'
Expression = {@(
$PSItem.CreationTime,
$PSItem.LastAccessTime,
$PSItem.LastWriteTime
)
}
} | Out-Gridview
然而,听起来您所追求的也是让这些航向,而这不是 OGV 能够做到的。您必须定义自己的 OGV 自定义版本或执行以下操作:
Clear-Host
Get-ChildItem -Path 'D:\Temp\*.txt' |
Select-Object -First 1 |
Select-Object -Property FullName,
@{
Name = 'DateDetails'
Expression = {@(
[PSCUstomObject]@{
CreationTime = $PSItem.CreationTime
LastAccessTime = $PSItem.LastAccessTime
LastWriteTime = $PSItem.LastWriteTime
}
)
}
} | Out-Gridview
或者通过这样的方法获得更多创意:
$col = @(
(New-Object –TypeName PSObject –Prop @{'id'='01';'name'='a';'items'=@('the first item','the second item', 'the third item')}),
(New-Object –TypeName PSObject –Prop @{'id'='02';'name'='b';'items'=@('the first item','the second item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item')}),
(New-Object –TypeName PSObject –Prop @{'id'='03';'name'='c';'items'=@('the first item','the second item', 'the third item')})
)
$col | Select -p id,@{E={$_.items -join "`n"};L="Items"},name | Out-GridView
或者这样的技巧:
('[{"a":1,"b":2},{"a":3,"b":4},{"a":5,"b":6}]' |
ConvertFrom-Json) |
Out-GridView
或
$List = @()
$List += [pscustomobject]@{
Column1 = "abc"
Column2 = "cde"
Column3 = "fgd"
}
$List += [pscustomobject]@{
Column1 = "cab"
Column2 = "dre"
Column3 = "fde"
}
$List | Out-GridView