Powershel script notify users with unchanged password
$servermail="192.168.11.1"
[string]$userName = 'dom\user'
[string]$userPassword = 'goodpass'
[string]$from = 'user#dom.test'
$secStringPassword = ConvertTo-SecureString $userPassword -AsPlainText -Force
$credObject = New-Object System.Management.Automation.PSCredential ($userName, $secStringPassword)
Function SendMail{
param ($credentials , $mailServer, $from, $to, $subject, $body)
Send-MailMessage" "-To $to -From $from -Subject $subject -Body $body -SmtpServer $mailServer -ErrorAction Stop -Credential $credentials"
}
#generate list with all users
$usrlist = Get-ADUser -Properties PasswordLastSet,Created,whenChanged,msDS-UserPasswordExpiryTimeComputed,lastlogon,EmailAddress,memberOf -Filter * | Select-Object Name,sAMAccountName,ObjectClass,EmailAddress,Enabled,Created,whenChanged,PasswordLastSet,@{Name='LastLogon';Expression={[DateTime]::FromFileTime($_.LastLogon)}},@{N="msDS-UserPasswordExpiryTimeComputed";E={[datetime]::FromFileTime($_.'msDS-UserPasswordExpiryTimeComputed')}},distinguishedname,@{n='OU';e={($_.DistinguishedName.Split(",") | Where-Object {-Not $_.StartsWith("CN=")}) -join ";"}},@{N="MemberOf";E={($_.'MemberOf') -join(";")}}
$nrmonth = -6
$dateref = ((Get-Date).AddMonths($nrmonth))
$cnt=0
foreach ($useritem in $usrlist ) {
$cnt = $cnt+1
if (( $useritem.Enabled -eq "True" ) -AND ([datetime]::parseexact($useritem.PasswordLastSet, 'MM/dd/yyyy HH:mm:ss', $null) -lt $dateref) -AND (-Not ([string]::IsNullOrEmpty($useritem.EmailAddress))) ) {
SendMail $credObject $servermail $from $useritem.EmailAddress 'Warning' ( -join ('Because your password was not changed in ' , -$nrmonth, ' months please change your password. last password change: ',$useritem.PasswordLastSet ))
}
}
Comentarii
Trimiteți un comentariu