powershell - 将 DateTime 戳记作为计算属性进行比较和组合的最佳方法是什么?
问题描述
创建以下脚本以从所有域控制器获取实际最新的 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合并到一个适当的列中?
解决方案
至于...
“我们如何将上述三个列 Real Last Logon、Most Recent Logon 和 LastLogonDate 合并到一个适当的列中?”
我没有方便的 ADDS 环境来使用它,所以只需使用文件系统和随机文本内容。
您可以为所有三个日期使用一个计算属性。比如说,@{Name = 'LogOnDates';Expression {' all your date info here'}},但这将在该单列中显示为单行表示。
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
推荐阅读
- javascript - 发送后如何删除消息?
- css - 需要帮助重新调整 CSS 滑动文本动画的用途
- android - 如何解决 Flutter Camera 插件 stopVideoRecording 方法的异常?
- r - R:获取p个二进制变量的所有组合
- python - 如何用其中一个键修改 dict
- scala - Spark,检查某些列是否为 True,然后用 Y 或 N 填充新列
- flutter - 如何获得特定范围内的随机 BigInt (Dart)
- php - 是否可以在没有 webhook 的情况下进行 twilio 入站呼叫或短信
- syslog-ng - syslog-ng 尾文件缺少最后一行
- angular - 代码覆盖阈值不会阻止脚本在 Angular 应用程序中运行