Reputation: 3
I'm trying to make a function in my script that loops through usernames and creates them subsequently. For example when I have a "User1" and "User2" it will create a "User3" and if I were to delete one of these users it would create that user again on the next run of the script.
Edit: There's no error in what I linked I'm just wondering how I can make something like this possible since I couldn't find anything similar on Google if I'm wrong about this and there is a viable source a link would be very much appreciated.
Import-Module ActiveDirectory
Get-ADUser -filter "enabled -eq 'false'" -SearchBase 'OU=ScriptLuuk,OU=GastAccounts,OU=Leerlingen,DC=cvo-zwfryslan,DC=nl' |
Remove-ADUser -Confirm:$false
$ExpirationDate = Read-Host -Prompt "Enter Expiration Date like so 10/18/2018"
function Get-RandomCharacters($length, $characters) {
$random = 1..$length | ForEach-Object { Get-Random -Maximum $characters.length }
$private:ofs = ""
return [String]$characters[$random]
}
$password = Get-RandomCharacters -length 3 -characters 'abcdefghijklmnopqrstuvwxyz'
$password += Get-RandomCharacters -length 1 -characters 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
$password += Get-RandomCharacters -length 1 -characters '1234567890'
$username = @("User1, User2, User3, User4, User5, User6, User7, User8, User9, User10")
New-ADUser `
-Name User1 `
-AccountPassword (ConvertTo-SecureString "$password" -AsPlainText -Force) `
-Path "OU=ScriptLuuk,OU=GastAccounts,OU=Leerlingen,DC=cvo-zwfryslan,DC=nl" `
-AccountExpirationDate "$ExpirationDate" `
-Enabled 1
Add-ADGroupMember -Identity ScriptGast -Member Luuk1
if (!(Test-Path "cvo-zwfryslan.nl\cvo\Personeel\KELU\Bureaublad\ScriptGast.txt")) {
New-Item -Path cvo-zwfryslan.nl\cvo\Personeel\KELU\Bureaublad\ -Name ScriptGast.txt -Type "file" -Value "my new text"
Write-Host "Created new file and text content added"
Write-Output `n | Out-File cvo-zwfryslan.nl\cvo\Personeel\KELU\Bureaublad\ScriptGast.txt -Append
} else {
Add-Content -Path cvo-zwfryslan.nl\cvo\Personeel\KELU\Bureaublad\ScriptGast.txt -Value "School1:$Password : $ExpirationDate"
Write-Output `n | Out-File cvo-zwfryslan.nl\cvo\Personeel\KELU\Bureaublad\ScriptGast.txt -Append
}
Upvotes: 0
Views: 583
Reputation: 23355
You're defining this array incorrectly:
$username = @("User1, User2, User3, User4, User5, User6, User7, User8, User9, User10")
It needs to be:
$username = @("User1", "User2", "User3", "User4", "User5", "User6", "User7", "User8", "User9", "User10")
Upvotes: 1