Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 将日期时间戳作为计算属性进行比较和组合的最佳方法是什么?_Powershell_Active Directory - Fatal编程技术网

Powershell 将日期时间戳作为计算属性进行比较和组合的最佳方法是什么?

Powershell 将日期时间戳作为计算属性进行比较和组合的最佳方法是什么?,powershell,active-directory,Powershell,Active Directory,创建以下脚本是为了从所有域控制器获取实际的最新AD LastLogon。 我需要从LastLogon属性中获取最早的日期或最近的条目 然而,结果是不同的,它是相当混乱的,如下所示: 这是我到目前为止提出的脚本: function Get-ADUserLastLogon([string]$userName) { $dcs = Get-ADDomainController -Filter { Name -like "*" } $time = 0 fore

创建以下脚本是为了从所有域控制器获取实际的最新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组合成一个适当的列?

听起来您的目标是提取三个日期时间值,相互比较,然后返回最近的一个。在这种情况下,您可以将所有变量传递给测量对象,如下所示:

(($MostRecentLogon,$RealLastLogon,$LastLogonDate) | Measure-Object -Maximum).Maximum

这将比较所有三个日期并输出最高值,即最近的日期。

听起来您的目标是提取三个日期时间值,相互比较,然后返回最近的日期。在这种情况下,您可以将所有变量传递给测量对象,如下所示:

(($MostRecentLogon,$RealLastLogon,$LastLogonDate) | Measure-Object -Maximum).Maximum
这将比较所有三个日期并输出最高值,即最近的日期。

至于

如何将上述三列Real Last Logon、Most Recent Logon和LastLogonDate组合成一个适当的列

我手头没有一个ADDS环境来使用它,所以只能使用文件系统和随机文本

可以对所有三个日期使用一个计算属性。比如说,@{Name='LogOnDates';表达式{'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
或者通过以下方法获得更多创意:

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
或者像这样的把戏:

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

至于

如何将上述三列Real Last Logon、Most Recent Logon和LastLogonDate组合成一个适当的列

我手头没有一个ADDS环境来使用它,所以只能使用文件系统和随机文本

可以对所有三个日期使用一个计算属性。比如说,@{Name='LogOnDates';表达式{'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
或者通过以下方法获得更多创意:

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
或者像这样的把戏:

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


这是真的,但OP问:“我需要从LastLogon属性中获取最早的日期或最近的条目。”因此,尽管OP显示了3个日期,但OP要求一个,然后在帖子的后半部分中使用这三个,我提出了一个答案。所以OP真的需要更好地定义他们的目标。所以,OP是说,他们希望所有三个都在一列中,排序,或者一列中的所有三个,或者该列中的一个或另一个。意思是“最早的日期或最近的”,部分。当然,我看到你和我同时发布了一个答案,OP肯定不清楚他们需要什么。另一种解释是,因为他们说我需要从LastLogon属性中获取最早的日期或最近的条目。它可以是获取最早的日期,或者只是获取LastLogon属性,这没有什么意义。明白了。OP显然可以结合我们提供的内容,然后使用Sort Object或Sort Object-降序是一种条件检查,将其中一个放在最上面,同时列出所有三个。这是真的,但OP问:“我需要从LastLogon属性中获取最早的日期或最近的条目。”因此,尽管OP显示了3个日期,OP要求我给出一个答案,在这篇声明中,然后在文章的后半部分中给出所有三个答案。所以OP真的需要更好地定义他们的目标。所以,OP是说,他们希望所有三个都在一列中,排序,或者一列中的所有三个,或者该列中的一个或另一个。意思是“最早的日期或最近的”,部分。当然,我看到你和我同时发布了一个答案,OP肯定不清楚他们需要什么。另一种解释是,因为他们说我需要从LastLogon属性中获取最早的日期或最近的条目。它可以是获取最早的日期,或者只是获取LastLogon属性,这没有什么意义。明白了。很明显,OP可以结合我们提供的,然后使用排序对象或排序对象-降序是一种条件检查,将其中一个放在顶部,同时列出所有三个。哇,太好了,我从来都不知道这一点。谢谢,不用担心。我经常尝试让人们利用OGV、Show命令、Msgbox和HTML
避免编写完整的UX/UI WinForm/WPF解决方案。使用这三种方法,您可以真正获得创造性,并且永远不会编写任何表单代码。有几种方法可以让OGV做一些非常有趣的把戏,而不是默认的东西。哇,太好了,我从来都不知道。谢谢,不用担心。我经常尝试让人们利用OGV、Show命令、Msgbox和HTML来避免编写完整的UX/UI WinForm/WPF解决方案。使用这三种方法,您可以真正获得创造性,并且永远不会编写任何表单代码。有几种方法可以让OGV做一些真正有趣的把戏,而不仅仅是默认的东西。