Powershell Active Directory帐户过期日期比较

Powershell Active Directory帐户过期日期比较,powershell,active-directory,Powershell,Active Directory,我正试图编写一个小脚本来检查Active DirectoryAccountExpirationDate是否已过期,或者它是否处于活动状态且为空。看来我能做一件或另一件,但不能两者兼而有之。$accexpoutput仅用于在屏幕上打印时删除空白。这是我目前的剧本 $username = Read-Host -Prompt "Input Username" $currdate = Get-Date $accexp = Get-ADUser -Filter 'Name -like $username'

我正试图编写一个小脚本来检查Active Directory
AccountExpirationDate
是否已过期,或者它是否处于活动状态且为空。看来我能做一件或另一件,但不能两者兼而有之。
$accexpoutput
仅用于在屏幕上打印时删除空白。这是我目前的剧本

$username = Read-Host -Prompt "Input Username"
$currdate = Get-Date
$accexp = Get-ADUser -Filter 'Name -like $username' | Select -ExpandProperty AccountExpirationDate
$accexpoutput = (Get-ADUser -Filter 'Name -like $username'  -Properties AccountExpirationDate | Format-list AccountExpirationDate | Out-String).trim()

if($accexp -gt $currdate -or !$accexp){write-host $accexpoutput $currdate -ForegroundColor Blue -BackgroundColor Green}

else {Write-Host $accexpoutput $currdate -ForegroundColor Red -BackgroundColor Black}

任何提示或帮助都将不胜感激

在您的情况下,您可以使用以下选项:

$accexp = Get-ADUser -Filter 'Name -like $username' -Properties AccountExpirationDate |
    Select -ExpandProperty AccountExpirationDate
说明:

默认情况下,
ADUser
返回的对象具有默认属性集。任何不在默认集合中的属性都必须显式传递到
*
参数中的
-properties
中,或由该参数表示

由于
-properties
接受数组,因此可以使用语法
-Property1、property2、property3
检索多个非默认属性

特别是将一组属性传递到
-properties
中不会抑制默认属性输出。如果只想查看一组特定的属性,则需要使用其他一些过滤机制,如
Select Object

大多数情况下不建议使用
-Properties*
,因为查询多余的属性需要额外的资源。因为您的案例中只需要一个额外属性,所以您应该只将该属性传递到参数中


您可以查看由提供的,以查看扩展属性和默认属性列表。请注意,并非所有属性都被视为默认属性或扩展属性。您可以使用
-properties

通过LDAP显示名称检索这些非默认和非扩展属性。缺少
-properties AccountExpirationDate
。哇,我怎么会错过这个。谢谢你,我很感激。