Powershell 帮助比较

Powershell Help to compare

祝大家平安健康!!

我是 运行 一个脚本,它为我提供了 Azure AD 应用及其秘密结束日期 属性。在 Azure AD 中提供所有详细信息的 属性 名称是“PasswordCredentials”,我正在使用 get-azureadapplication cmdlet。

检查应用程序是否有一个月内的结束日期值并对其进行过滤的最佳方法是什么,我尝试使用 get-date.adddays(30) 的 where-object 并尝试与 -lt 进行比较运算符。

在此感谢您的支持。

嗯,在你的情况下,你应该注意一个应用程序可能有几个 PasswordCredentials,所以你需要使用循环来检查每个 PasswordCredential

而你从Get-AzureADApplication命令得到的EndDateUTC时间,但是Get-Datereturns是本地时间,所以你需要使用 (Get-Date).AddDays(30).ToUniversalTime() 而不是 (Get-Date).AddDays(30)。另外,如果你只是想检查过期的,你必须排除那些已经过期的,所以像下面这样使用$PasswordCredential.EndDate -gt $nowdate

因为你没有给你的脚本,我可以给你一个示例,我的示例是针对单个应用程序的,如果你想检查你租户中的所有应用程序,请使用循环来完成。

$PasswordCredentials = (Get-AzureADApplication -ObjectId <object-id>).PasswordCredentials 
$nowdate = (Get-Date).ToUniversalTime()
$wantdate = (Get-Date).AddDays(30).ToUniversalTime()
foreach($PasswordCredential in $PasswordCredentials){
    if($PasswordCredential.EndDate -lt $wantdate -and $PasswordCredential.EndDate -gt $nowdate){
        $keyid = $PasswordCredential.KeyId
        Write-Output "The key with KeyId $keyid will expire"
    }
}

更新:

如果你想得到AppIdAppName,你可以使用下面的脚本。

$apps = Get-AzureADApplication -All $true
$nowdate = (Get-Date).ToUniversalTime()
$wantdate = (Get-Date).AddDays(30).ToUniversalTime()
foreach($app in $apps){
    $PasswordCredentials = $app.PasswordCredentials
    $appid = $app.AppId
    $displayname = $app.DisplayName
    foreach($PasswordCredential in $PasswordCredentials){
        if($PasswordCredential.EndDate -lt $wantdate -and $PasswordCredential.EndDate -gt $nowdate){
            $a = $app | select  @{Name="AppId"; Expression={$appid}}, @{Name="DisplayName"; Expression={$displayname}}, @{Name="EndDate"; Expression={$PasswordCredential.EndDate}} 
            Write-Output $a
        }
}
}