I built a small script that allows me to check if a users AD account is locked out and email me if it is. It asks for username and password the first time it is run and the script that starts the powershell script schedules a task to run this every 5 minutes until i stop it.
Right now I can do this for one user at a time. The powershell script can test multiple users but i need a way to make it write the xml it is reading the information from on consecutive automated runs to be able to pull multiple users and store multiple user information.
All Code Below
Powershell:
$ConfigPath = "C:\temp\userconfig.xml"
If (test-path $ConfigPath){
$Config = Import-Clixml $ConfigPath
}Else{
$ConfigHash = @{
email = read-host "What is your email address?"
username =read-host "What is the username of the person locked out?"
}
$Config = New-Object -TypeName PSObject -Property $ConfigHash
$Config | export-clixml $ConfigPath
}
Import-Module ActiveDirectory
$accts = ($config.username) | Get-ADUser -Properties LockedOut | Where-Object { $_.LockedOut -eq $true } | Select Name, LockedOut
if ($accts) {
$body = $accts | ConvertTo-HTML | Out-String
Send-MailMessage -From $config.email -SMTPServer mail.server.com -BodyAsHtml $body -Subject "Locked Out Accounts" -To ($config.email)
}
after first run it creates the XML script with all the information to run multiple times again. If i want to add a second person to test i would seperate the usernames by a comma where $config.username is. I don't want to manually edit the script though, i want to be able to add a second user by editing the XML.
Question: How would I acomplish being able to run this check on 2 or 3 users the best way? AND What would be the best way to add a second user after this is run for the first time?