Import-Module ActiveDirectory $outputlogt = @() $outputlogf = @() $users = @() #$path1 = "ou=测试,dc=bjgas,dc=com" Write-Host "请输入第1个遍历路径" $user1 = Read-Host $User1 = Get-ADUser -Filter * -SearchBase "$path1" Write-Host "请输入第2个遍历路径" $path2 = Read-Host $user2 = Get-ADUser -Filter * -SearchBase "$path2" $users = $user1 #+ $user2 $nowdate = (get-date -Format "yyyy/mm/dd hh:mm").ToString() #Write-Host "Please Enter a NewPassword" #$newpass = "Read-Host" write-host "Please Enter a NewPssword!" Write-Warning "The Password will be applied to all Users in the $path1 and $path2 container" $newpass = Read-Host set-location c:\ $WorkingPath = '.\log\' foreach ($user in $users) { $userlogondate = Get-ADUser -Identity $user.Name -Properties LastLogondate | Select-Object -Property LastLogondate $days = (New-TimeSpan $userlogondate.lastlogondate).Days if ($days -gt 7) { #write-host "Please Enter a NewPssword!" #Write-Warning "The Password will be applied to all Users in the $path1 and $path2 container" #$newpass = Read-Host $user | Set-ADAccountPassword -Reset -NewPassword (ConvertTo-SecureString -AsPlainText "$newpass" -Force) $Log = New-Object PSObject $Log | Add-Member NoteProperty "UserName" $user.name $Log | Add-Member NoteProperty "LastLogonDate" $userlogondate $Log | Add-Member NoteProperty "ModifyPassWord" "True" $OutPutLogT += $Log $OutPutLogT | Export-Csv -Encoding UTF8 -NoTypeInformation -Path "$workingpath'true'.csv" } else { $Logf = New-Object PSObject $Logf | Add-Member NoteProperty "UserName" $user.name $Logf | Add-Member NoteProperty "LastLogonDate" $userlogondate $Logf | Add-Member NoteProperty "ModifyPassWord" "false" $OutPutLogF += $Logf $OutPutLogF | Export-Csv -Encoding UTF8 -NoTypeInformation -Path "$workingpath'False'.csv" } }
转载于:https://blog.51cto.com/suxin/1587515