码迷,mamicode.com
首页 > 其他好文 > 详细

修改距离当天日期7天以上的AD用户密码,并生成日志

时间:2014-12-08 15:50:35      阅读:295      评论:0      收藏:0      [点我收藏+]

标签:powershell   密码   用户   

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"
        }
    
    }

本文出自 “百草素心” 博客,请务必保留此出处http://suxin.blog.51cto.com/535505/1587515

修改距离当天日期7天以上的AD用户密码,并生成日志

标签:powershell   密码   用户   

原文地址:http://suxin.blog.51cto.com/535505/1587515

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!