Get-ADUser - 操纵列

时间:2018-06-04 13:47:06

标签: powershell

我的powershell脚本:

  $MaxPwdAge   = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge.Days
  $expiredDate = (Get-Date).addDays(-$MaxPwdAge)

  $users = Get-ADUser -Filter {(Enabled -eq $true)} -Properties PasswordNeverExpires, PasswordLastSet `
  | select samaccountname, PasswordLastSet, @{name = "DaysUntilExpired"; Expression = {$_.PasswordLastSet - $expiredDate | select -ExpandProperty Days}}, PasswordNeverExpires `
  | Sort-Object PasswordNeverExpires, PasswordLastSet

  $users | Format-Table | Out-String|% {Write-Host $_}
  Write-Host -NoNewLine "Press any key to return to the Main Menu..."
  $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

目标: 我想摆脱PasswordNeverExpires列,相反,在DaysUntilExpired列中,我希望它为密码永不过期的用户说'密码永不过期'。

我还在学习powershell,有人可以告诉我如何做到这一点吗?

1 个答案:

答案 0 :(得分:2)

我相信这样的事情可以解决问题。

$MaxPwdAge   = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge.Days
$expiredDate = (Get-Date).addDays(-$MaxPwdAge)

$users = Get-ADUser -Filter {(Enabled -eq $true)} -Properties PasswordNeverExpires, PasswordLastSet `
    | select samaccountname, PasswordLastSet, @{name = "DaysUntilExpired"; Expression = {
        if($_.PasswordNeverExpires -eq $true) { "Password Never Expires" } else { $_.PasswordLastSet - $expiredDate | select -ExpandProperty Days}}
    } | Sort-Object PasswordLastSet

$users | Format-Table | Out-String|% {Write-Host $_}
Write-Host -NoNewLine "Press any key to return to the Main Menu..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")