PowerShell - Get Password Expiration for all non Disabled Users in AD

℡╲_俬逩灬. 提交于 2021-02-08 15:46:12

问题


My goal is to create a single object in powershell that shows the AD display name and password expiration date for all non-disabled users. This is relatively easy. However, the date is retrieved in an unreadable format so I convert the date.

Creating the two variables that contain the data I want is working. The problem is when I try to combine those two variables I get a single object with two headers as expected but the two columns below are empty.

I'm using PowerShell V2 on Win 7 Pro SP1

Any ideas what the issue could be?

# Get users DisplayName and password expiration time from AD
$msdsComputed = Get-ADUser -filter {Enabled -eq $True -and PasswordNeverExpires -eq  $False} -Properties "DisplayName", "msDS-UserPasswordExpiryTimeComputed" |  
        Where-Object {$_.DisplayName -ne $null} 

# Convert date to human readable format
$ExpiryDate = $msdsComputed | Foreach-Object {
             ([datetime]::FromFileTime(($_)."msDS-UserPasswordExpiryTimeComputed")) 
                          } | Select-Object "DateTime"



$combined = @{            
              DisplayName   = $msdsComputed.DisplayName
              ExpiryDate    = $ExpiryDate.DateTime
             }
New-Object PSObject -Property $combined | ConvertTo-Csv -NoTypeInformation

回答1:


Here's a revised version of your script without looping issues:

$reportObject = @()
$userList = Get-ADUser -filter {Enabled -eq $True -and PasswordNeverExpires -eq  $False} -Properties "DisplayName", "msDS-UserPasswordExpiryTimeComputed" |  Where-Object {$_.DisplayName -ne $null}
$userList | %{

    $output = "" | Select DisplayName, ExpiryDate
    $output.DisplayName = $_.DisplayName
    $output.ExpiryDate = ([datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")).DateTime
    $reportObject += $output
    #Next 2 Lines provide debugging... I'm not sure the date time portion will work without having AD to play with
    $output | fl *
    ([datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")).DateTime 
}

$reportObject | Convertto-CSV -NoTypeInformation


来源:https://stackoverflow.com/questions/11567165/powershell-get-password-expiration-for-all-non-disabled-users-in-ad

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!