Get-Aduser question
hi want retrieve users password expire in less 10 days.
so far script goes:
#this gets number of days expiry $daystoexpire = (([datetime]::fromfiletime((get-aduser -identity $user -properties "msds-userpasswordexpirytimecomputed")."msds-userpasswordexpirytimecomputed"))-(get-date)).days #this gets users $users= get-aduser -filter {(passwordneverexpires -eq $false) -and (enabled -eq $true)} -searchscope subtree -searchbase "ou=contoso,dc=com" -properties *
i want pipe $daystoexpire $users users $daystoexpire -le 10
or can modify this
$daystoexpire = (([datetime]::fromfiletime((get-aduser -identity $user -properties "msds-userpasswordexpirytimecomputed")."msds-userpasswordexpirytimecomputed"))-(get-date)).days
to filter inly property "msds-userpasswordexpirytimecomputed" has value -le 10?
another way trying is creating new object again shows users , daystoexpire, want to display users days -le 10
$expiring_users= get-aduser -filter {(passwordneverexpires -eq $false) -and (enabled -eq $true)} -searchscope subtree -searchbase "ou=contoso,dc=com" -properties * foreach($user in $expiring_users){ $days = $name = $mail = $givenname = $null $days = (([datetime]::fromfiletime((get-aduser -identity $user -properties "msds-userpasswordexpirytimecomputed")."msds-userpasswordexpirytimecomputed"))-(get-date)).days #where {($_.passwordexpires -le $date.adddays($days_to_expiry))} $name = $user.samaccountname $mail = $user.mail $givenname = $user.givenname $userobj = new-object –typename psobject –prop(@{'givenname'=$givenname;'name'=$name;'mail'=$mail;'days'=$days;}) $userobj }
thanks
swap (get-date) , ([datetime]::fromfiletime($_."msds-userpasswordexpirytimecomputed"))
get-aduser -filter {(passwordneverexpires -eq $false) -and (enabled -eq $true)} -searchscope subtree -searchbase "ou=contoso,dc=com" -properties "msds-userpasswordexpirytimecomputed" | { $diff = new-timespan (get-date) ([datetime]::fromfiletime($_."msds-userpasswordexpirytimecomputed")) add-member -inputobject $_ -membertype noteproperty -name daystoexpire -value $diff.days -force $diff.days -le 10 -and $diff.days -ge 0 }
output:
ps c:\users\administrator> get-aduser -filter {(passwordneverexpires -eq $false) -and (enabled -eq $true)} -properties " msds-userpasswordexpirytimecomputed" | { >> $diff = new-timespan (get-date) ([datetime]::fromfiletime($_."msds-userpasswordexpirytimecomputed")) >> add-member -inputobject $_ -membertype noteproperty -name daystoexpire -value $diff.days -force >> $diff.days -le 50 -and $diff.days -ge 0 >> } | ft name,daystoexpire -a >> name daystoexpire ---- ------------ administrator 25 user1 41
Windows Server > Windows PowerShell
Comments
Post a Comment