I have a script that works good overall, but one of the parameters doesn't show as it should.
Script :
# Create user account in AD. After execution, you will ask to set the password.
New-ADUser `
-Name "John Travolta" `
-GivenName "John" `
-Surname "Travolta" `
-UserPrincipalName "JohnT"`
-DisplayName "John Travolta" `
-AccountPassword (Read-Host -AsSecureString "Input User Password") `
-ChangePasswordAtLogon $True `
-Company "CLASS" `
-Title "Actor" `
-State "ON" `
-City "Kitchener" `
-Description "Bold dude from face off" `
-EmployeeNumber "4" `
-Department "Paramount Cinema" `
-Country "CA" `
-PostalCode "N2B G5T" `
-SamAccountName "JohnT"`
-MobilePhone "519-7218652"`
-Enabled $True
#Add user to the specific OU.
$MOVEOU = Move-ADObject -Identity "CN=John Travolta,CN=Users,DC=class,DC=com" -TargetPath "OU=paramount cinema,DC=class,DC=com"
if ($moveou -eq "error") {
Write-Host "Move ADObject failed" -ForegroundColor DarkRed
}
else {
Write-Host "Move ADObject completed" -ForegroundColor Cyan
}
#Add user to the specific group
$MoveToGroup = Add-ADGroupMember -Identity SG-ParamountCinema -Members JohnT
if ($movetogroup -eq "error") {
Write-Host "Add ADGroupMember Failed" -ForegroundColor DarkRed
}
else {
Write-Host "Add ADGroupMember completed" -ForegroundColor Cyan
}
The result is good but, in case of an error it doesn't show the "Failed" in dark red, only the success one.
Please Help, thank you!
-ErrorAction Stopparameter, and wrap things in aTry{}Catch{}. Such asTry{$MoveToGroup = Add-ADGroupMember -Identity SG-ParamountCinema -Members JohnT -ErrorAction Stop;Write-Host "Add ADGroupMember completed" -ForegroundColor Cyan}Catch{Write-Host "Add ADGroupMember Failed" -ForegroundColor DarkRed}