Powershell只接收基于日期的唯一和最新结果



我得到了一个大型脚本的一部分:

$clientsecrets = @()
$applist = Get-MsolServicePrincipal -all |
Where-Object -FilterScript {
($_.DisplayName -like "*SI*") -or
($_.DisplayName -like "*FD*") -or
($_.DisplayName -like "*AP*") -and
($_.DisplayName -notlike "*Microsoft*") -and
($_.DisplayName -notlike "autohost*") -and
($_.ServicePrincipalNames -notlike "*localhost*") }
foreach ($appentry in $applist) {
$principalId = $appentry.AppPrincipalId
$principalName = $appentry.DisplayName

$clientsecret = Get-MsolServicePrincipalCredential -AppPrincipalId $principalId -ReturnKeyValues $false |
? { $_.Type -eq "Password" } |
% {
$principalName, $principalId;, ($enddate = $_.EndDate.ToString())
} |
select {$principalName}, {$principalId}, {$enddate}

$clientsecret | Add-Member -MemberType NoteProperty -Name 'principalId' -Value $principalId
$clientsecret | Add-Member -MemberType NoteProperty -Name 'principalName' -Value $principalName
$clientsecrets+=$clientsecret
}

这将返回大约140行,但如果我只捕获每个uniqe中最新的一个(uniqe名称+id(,它应该返回3。

我做的第一件事是尝试使用"排序对象-属性$enddate-降序-唯一"选项,但这不行,我尝试了我能想到的任何排序对象、select、with First、last等。

我以前从来没有做过这样的事情,所以我在这里有点迷失了如何进行正确的分类。

或者,只在今天之后的1天内获得每个带有$enddate+1的uniqe(是的,我尝试过使用(get-date(.AddDays(1((有谁能让我朝着正确的方向前进?

显然我并没有这么想。

Where-Object {$enddate -gt (Get-Date $Datum -Format yyyy-MM-dd)} 
| Sort-Object -Property $principalName -Unique

解决了这个问题,我在一台电脑上工作,我没有任何设置为英语的东西,所以用-格式修复了它,这样它就匹配了。

最新更新