我希望添加一行(可能是Foreach的某种语句),该行在给定的收益上选择两个或多个日期中的较大者。到目前为止,我还没有精通Powershell(正在学习!),如果可能,我需要一些帮助。
我尝试添加一个ForEach-Object {$ CPDetails.TokenSigningCertificates.NotAfter |排序对象-属性NotAfter |选择对象-最后1}
到我的if语句,但是它会使输出一遍又一遍地重复相同的值。
$Results = @()
Try
{
$CPDetails = (Get-AdfsClaimsProviderTrust | select Name, TokenSigningCertificates)
}
Catch
{
$_.Exception.Message
Continue
}
If($CPDetails.TokenSigningCertificates)
{
$Object = New-Object PSObject -Property @{
NotAfter = $CPDetails.TokenSigningCertificates.NotAfter | ForEach-Object { $CPDetails.TokenSigningCertificates.NotAfter| Sort-Object -Property NotAfter|Select-Object -Last 1}
Name = $CPDetails.Name
}
}
$Results += $Object
If($Results)
{
$Results | Select-Object NotAfter, Name | Sort-Object NotAfter | Out-GridView -Title "Certificates"
}
当前:
DateTime { 12/31/2999 12:00:00 PM, 12/31/2999 12:00:00 PM, 12/31/2999 12:00:00 PM,
Name {Trust 1, Trust 2, Trust 3.....
需要为:
DateTime { 12/31/2999 12:00:00 PM, 01/2/2019 12:00:00 PM, 05/31/2021 12:00:00 PM,
Name {Trust 1, Trust 2, Trust 3.....
答案 0 :(得分:0)
我最终想到的是一个比最初的混乱还更优雅的解决方案:
Get-AdfsClaimsProviderTrust |
Select name, @{Name='TokenSigningCertificates'; Expression={[string]::join(";", ($_.TokenSigningCertificates.NotAfter|
Group-Object Name |
Foreach-Object {$_.Group | Sort-Object * | Select-Object -Last 1}))}}
感谢@MadTechnician的指导