Hi,
I have found a very handy powershell script to disable inactive user accounts. It works just how i want it apart from one thing. When an account is disabled its supposed to put in the description "Account Disabled on <date here> for Inactivity". But there its supposed to put the date its failing and just leaving a white space.
Can anyone help please?
Thanks
Code:### User Variables ### # Query Options # $searchRoot = "domain.local/" # Where to begin your recursive search - If you use top-level (e.g. "domain.local/") make sure to have a trailing slash, otherwise do not use a slash (e.g. "domain.local/Users") $inactiveDays = 90 # Integer for number of days of inactivity (e.q. 90) $timeSinceCreation = 30 # Integer for number of "grace" days since the account was created (to prevent disabling of brand new accounts) $sizeLimit = 0 # How many users do you want returned. 0 = unlimited. Without setting this the default is 1000 # Email Settings # $emailAlerts = 1 # Turn e-mail alerts on or off. 0 = off $fromAddr = "InactiveAccounts@Domain.com" # Enter the FROM address for the e-mail alert $toAddr = "User@Domain.com" # Enter the TO address for the e-mail alert $smtpsrv = "192.168.1.1" # Enter the FQDN or IP of a SMTP relay # Enable Script # $enableAction = 1 # Change to 0 if you want to "whatif" this script - It will bypass the actual account disabling (turn e-mail alerts on!) ###################### Add-PSSnapin "Quest.ActiveRoles.ADManagement" $creationCutoff = (Get-Date).AddDays(-$timeSinceCreation) $inactiveUsers = @(Get-QADUser -SearchRoot $searchRoot -Enabled -NotLoggedOnFor $inactiveDays -CreatedBefore $creationCutoff -SizeLimit $sizeLimit | Select-Object Name,SamAccountName,LastLogonTimeStamp,Description | Sort-Object Name) ### Disable Accounts ### if ($enableAction -eq 1 -and $inactiveUsers -ne $null){ foreach($user in $inactiveUsers){ Set-QADUser $user.SamAccountName -Description "Account Disabled on $date for Inactivity - $($user.Description)" | Disable-QADUser } } ###### ### Email Alerts ### if ($emailAlerts -eq 1 -and $inactiveUsers -ne $null){ $date = Get-Date -DisplayHint Date $body = @(" <center><table border=1 width=50% cellspacing=0 cellpadding=8 bgcolor=Black cols=3> <tr bgcolor=White><td>Name</td><td>Account</td><td>Last Login</td></tr>") $i = 0 do { if($i % 2){$body += "<tr bgcolor=#D2CFCF><td>$($inactiveUsers[$i].Name)</td><td>$($inactiveUsers[$i].SamAccountName)</td><td>$($inactiveUsers[$i].LastLogonTimestamp)</td></tr>";$i++} else {$body += "<tr bgcolor=#EFEFEF><td>$($inactiveUsers[$i].Name)</td><td>$($inactiveUsers[$i].SamAccountName)</td><td>$($inactiveUsers[$i].LastLogonTimestamp)</td></tr>";$i++} } while ($inactiveUsers[$i] -ne $null) $body += "</table></center>" Send-MailMessage -To $toAddr -From $fromAddr -Subject "Info: $($inactiveUsers.Count) User Accounts Disabled on $date" -Body "$body" -SmtpServer $smtpsrv -BodyAsHtml } ###### exit



LinkBack URL
About LinkBacks
Reply With Quote


your are correct thats fixed it. One thing is that is shows the exact time as well. It really inst needed do you happen to know how to get it to display only the date? Also It show the date in American format, do you know how to switch it to the British way please?


