首页 > 解决方案 > 将 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 LogonMost Recent LogonLastLogonDate合并到一个适当的列中?

标签: powershellactive-directory

解决方案


至于...

“我们如何将上述三个列 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

推荐阅读