设为首页 收藏本站
查看: 1275|回复: 0

PowerShell AD用户密码过期脚本更新版

[复制链接]

尚未签到

发表于 2018-9-2 07:13:06 | 显示全部楼层 |阅读模式
Function LogFile ($output, $initLog)  
{
  if ($initLog -eq $True)
  {
  $input | out-file -filepath $output -encoding default -width 17384
  }
  else
  {
  $input | out-file -filepath $output -encoding default -width 17384 -append
  }
  
}
  

  
function Send-Report
  
{
  param($LogConent,$LogPath,$MailAddress)
  try
  {
  Send-MailMessage -From "NO-Reply@contoso.com" -To $MailAddress -Subject 'Contoso Password check report' -Body $LogConent -Priority 'High' -SmtpServer mail.contoso.com -Port 25 -ErrorAction 'SilentlyContinue'
  }
  catch
  {
  $ErrorMessage = $Error[0].Exception.Message
  Write-Host -ForegroundColor 'Red' "$(Get-Date -uFormat %Y%m%d-%H:%M:%S)" $ErrorMessage
  ("$(Get-Date -uFormat %Y%m%d-%H:%M:%S): " + $ErrorMessage) | LogFile -output $LogPath
  }
  
}
  

  

  
#Main Code
  
#Import ActiveDirectory module
  
Import-Module ActiveDirectory
  

  

  
#Log initialization
  
[string]$LogDate = Get-Date -Format "yyyyMMdd"
  
$LogPath = "C:\PasswordLogs\DomainPasswordLog$LogDate.txt"
  
if ((Test-Path 'C:\PasswordLogs') -eq $false)
  
{
  New-Item -ItemType directory 'C:\PasswordLogs' | Out-Null
  
}
  

  

  
#======================================================================================
  
#Get MaxPasswordAge
  
$RootDSE = Get-ADRootDSE
  
$PasswordPolicy = Get-ADObject $RootDSE.defaultNamingContext -Property maxPwdAge
  
$maxPwdAge = $PasswordPolicy.maxPwdAge/-864000000000
  
if (($maxPwdAge -eq 0) -or ($maxPwdAge -eq $null))
  
{
  $ErrorMessage = "MaxPasswordAge is not correct"
  Write-Host -ForegroundColor 'Red' "$(Get-Date -uFormat %Y%m%d-%H:%M:%S)" $ErrorMessage
  ("$(Get-Date -uFormat %Y%m%d-%H:%M:%S): " + $ErrorMessage) | LogFile -output $LogPath
  $LogConent = Get-Content $LogPath -raw
  Send-Report -LogConent $LogConent -LogPath $LogPath -MailAddress 'abc@contoso.com'
  exit
  
}
  
#======================================================================================
  
#Check userlist
  
#我这里的用户列表是写在一个txt文档里的,这是因为在我的环境中大部分用户是不需要这种邮件提醒的,他们的账户会由我们负责维护
  
#如果需要在AD里检索需要检查的用户的话可以直接这样写$userList=Get-ADUser -Filter *|Select-Object -ExpandProperty SamAccountName
  
#这样的话下边这段就不需要了
  
$userList = "C:\Users\abc\UserList.txt"
  
if ((Test-Path $UserList) -eq $false)
  
{
  $ErrorMessage = "Can't find userList.txt"
  Write-Host -ForegroundColor 'Red' "$(Get-Date -uFormat %Y%m%d-%H:%M:%S)" $ErrorMessage
  ("$(Get-Date -uFormat %Y%m%d-%H:%M:%S): " + $ErrorMessage) | LogFile -output $LogPath
  $LogConent = Get-Content $LogPath -raw
  Send-Report -LogConent $LogConent -LogPath $LogPath -MailAddress 'abc@contoso.com'
  exit
  
}
  

  
#======================================================================================
  

  
#这里如果是使用检索AD用户的方法的话可以直接写
  
#foreach($user in $userlist)替代get-content即可
  

  
Get-Content $UserList | %{
  $name = $null
  $userinfo = $null
  $ExpireDate = $null
  $PasswordSetDate = $null
  $Today = $null
  $leftDays = $null
  $body = $null
  $subject = $null
  $IndividualPasswordPolicy = $null
  $OutputMessage = $null
  $name = $_
  $userinfo = Get-ADUser -Identity $name -Properties *
  
    #这里首先判断该用户信息是否存在,如果不存在直接进行记录即可
  if ($userinfo -eq $null)
  {
  $ErrorMessage = $name + ": " + $Error[0].Exception.Message
  Write-Host -ForegroundColor 'Red' "$(Get-Date -uFormat %Y%m%d-%H:%M:%S)" $ErrorMessage
  ("$(Get-Date -uFormat %Y%m%d-%H:%M:%S): " + $ErrorMessage) | LogFile -output $LogPath
  }
  else
  {
  if ($userinfo.PasswordNeverExpires -eq $true)
  {
  #这里记录谁的密码被设置为永久不过期了
  $ErrorMessage = "$name's Password has been set to NeverExpires"
  Write-Host -ForegroundColor 'Cyan' "$(Get-Date -uFormat %Y%m%d-%H:%M:%S)" $ErrorMessage
  ("$(Get-Date -uFormat %Y%m%d-%H:%M:%S): " + $ErrorMessage) | LogFile -output $LogPath
  }
  else
  {
  #这里会读取颗粒化密码策略的设置,它的优先级应该高于域策略的设置
  $IndividualPasswordPolicy = (Get-AduserResultantPasswordPolicy $name)
  if ($IndividualPasswordPolicy -ne $null)
  {
  $maxPwdAge = $IndividualPasswordPolicy.MaxPasswordAge.TotalDays
  }
  $PasswordSetDate = $userinfo.PasswordLastSet
  $ExpireDate = $PasswordSetDate.AddDays($maxPwdAge)
  $Today = Get-Date
  #对比过期时间和今天,得出的数值就是还有多少天过期
  $leftDays = (New-TimeSpan -Start $Today -End $ExpireDate).Days
  if ($leftDays -lt 0)
  {
  $body = "
  
    Dear $name ,
  
     Your Password has expired!!.
  
    Please change your Password as soon as possible so that you can work normally
  
   Thanks,
  
    "
  $subject = "Your Password has expired!!"
  $OutputMessage = "$(Get-Date -uFormat %Y%m%d-%H:%M:%S): $name's Password has expired"
  Write-Output $OutputMessage | LogFile -output $LogPath
  }
  elseif ($leftDays -eq 1)
  {
  $body = "
  
    Dear $name ,
  
     Your Password will expire in  $leftDays  Day!!.
  
    Please change your Password as soon as possible so that you can work normally
  
   Thanks,
  
    "
  $subject = "Your Password will expire in $leftDays day!!"
  $OutputMessage = "$(Get-Date -uFormat %Y%m%d-%H:%M:%S): $name's Password will expire in $leftDays day"
  Write-Output $OutputMessage | LogFile -output $LogPath
  }
  elseif ($leftDays -le 10)
  {
  $body = "
  
    Dear $name ,
  
     Your Password will expire in  $leftDays  Days!!.
  
    Please change your Password as soon as possible so that you can work normally
  
   Thanks,
  
    "
  $subject = "Your Password will expire in $leftDays days"
  $OutputMessage = "$(Get-Date -uFormat %Y%m%d-%H:%M:%S): $name's Password will expire in $leftDays days"
  Write-Output $OutputMessage | LogFile -output $LogPath
  }
  else
  {
  $OutputMessage = "$(Get-Date -uFormat %Y%m%d-%H:%M:%S): $name's Password will expire in $leftDays days"
  Write-Output $OutputMessage | LogFile -output $LogPath
  }
  #这里设置的是如果10天以内过期的话就会发送提醒
  if ($leftDays -le 10)
  {
  #注意如果EmailAddress为空的话就需要自己处理如何找到邮件发送的地址了
  $MailAddress =   $userinfo.EmailAddress
  if ($MailAddress -ne $null)
  {
  try
  {
  Send-MailMessage -From "No-Reply@contoso.com" -To $MailAddress -Subject $subject -Body $body -BodyAsHtml -Priority 'High' -SmtpServer mail.contoso.com -Port 25 -ErrorAction 'SilentlyContinue'
  }
  catch
  {
  $ErrorMessage = $Error[0].Exception.Message
  Write-Host -ForegroundColor 'Red' "$(Get-Date -uFormat %Y%m%d-%H:%M:%S)" $ErrorMessage
  ("$(Get-Date -uFormat %Y%m%d-%H:%M:%S): " + $ErrorMessage) | LogFile -output $LogPath
  }
  }
  }
  }
  }
  
}
  

  
#最后把这份报告发送给IT管理员
  
if ((Test-Path $LogPath) -eq $true)
  
{
  $LogConent = Get-Content $LogPath -Raw
  Send-Report -LogConent $LogConent -LogPath $LogPath -MailAddress 'it@contoso.com'
  
}



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-561282-1-1.html 上篇帖子: PowerShell AD 管理 下篇帖子: powershell查询没有邮箱的账号
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表