Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #FINDING THE USERS TO DISABLE
- #Variables
- $DaysInactive = 91
- $time = (Get-Date).Adddays(-($DaysInactive))
- $SearchBase = "OU=Users,OU=Site,DC=Domain,DC=Local"
- $date = Get-date -Format ddMMyyyy
- #Finds users defined in Searchbase that are enabled and the last log on was more than the $DaysInactive variable.
- Get-ADUser -SearchBase $SearchBase -Filter {LastLogonTimeStamp -lt $time -and Enabled -eq $true} -Properties samAccountName,Name,EmailAddress,LastLogonTimeStamp | Select-object -Property samAccountName,Name,EmailAddress,@{N='LastLogonTime'; E={[DateTime]::FromFileTime($_.LastLogonTimeStamp).ToString('d MMMM yyyy')}} | Export-CSV -Path "c:\Script\UsersToDisable$Date.CSV"
- #EMAILING USERS YOUR ARE DISABLING - RENAME YOUR CSV TO SOMETHING SIMPLE BEFORE DOING THIS
- $smtpServer="smtp.server.com"
- $from = "Email Address <me@me.com>"
- $days = "# of Days"
- $subject="Your account will expire in $Days"
- $Users = Import-CSV -Delimiter "," -Path "c:\Script\UsersToDisable.CSV"
- ForEach ($User in $Users)
- {
- $to = $user.EmailAddress
- $name = $user.Name
- $stale = $user.LastLogonTime
- $emailbody = "<font face=Arial Size=2>
- Hello $Name,
- <p> You have not logged in to your account since $stale and it will be disable in $days days, unless you respond to this message</p>
- </br>
- <p>Thanks,</p>
- </br>
- <p>Your friendly neighbourhood IT Guy</p>
- </font>"
- # Send Email Message
- Send-Mailmessage -smtpServer $smtpServer -from $from -to $to -subject $subject -body $body -bodyasHTML -priority High
- }
- #DISABLE THE USER ACCOUNTS
- $users = Import-CSV -Delimiter "," -Path "c:\Script\UsersToDisable.CSV"
- ForEach ($User in $Users)
- {
- $SAM = $User.samAccountName
- Set-Aduser -Identity $SAM -enabled $false
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement