Advertisement
zakarakus

Stupid Powershell

Dec 11th, 2019
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. $Server = "contoso.co.uk"
  2. $SearchBase = "OU=People,DC=AD,DC=Contoso,DC=co,DC=uk"
  3. $AdminOU = "OU=Admin Accounts,OU=IT,OU=contoso,OU=People,DC=AD,DC=contoso,DC=co,DC=uk"
  4. $Office365 = "OU=Office 365,OU=contoso,OU=People,DC=AD,DC=contoso,DC=co,DC=uk"
  5.  
  6. $InactiveDays = 90
  7.  
  8. $EmailServer = "exchange"
  9. $Recipients = "notifications@contoso"
  10.  
  11. $WhatIfPreference = $false
  12.  
  13. $InactiveUsers = Search-ADAccount -AccountInactive -UsersOnly -TimeSpan (New-TimeSpan -Days $InactiveDays) -Server $Server -SearchBase $SearchBase | Get-ADUser -Properties Created | Where-Object {$_.DistinguishedName -notlike "*,$AdminOU,$Office365" -and $_.Enabled -and $_.Created -lt (Get-Date).AddDays(-30)}
  14.  
  15. If ($InactiveUsers) {
  16.  
  17. $InactiveUsers | Set-ADUser -Description "Inactive disabled: $(Get-Date -Format D )" -Enabled $false
  18. $HTML = $InactiveUsers | Sort-Object | ConvertTo-Html -As Table -Fragment -Property Name, @{Label = "Location"; Expression = {$_.DistinguishedName.Split(',')[1].Substring(3)}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement