I am working on a script that adds a local administrator to remote systems. This is what i have so far but on some of the systems the account might already exists so the first part changes the password for it the second two add an account and add it to the administrators group. Everything works in the script but I get an error if there is no existing account for the password reset or an error for the adding the account and group if there is already an account. Is there a way I can do something with these expected errors but still show the unexpected? Thanks
Code:
$PCName = gc "C:\Test\ADComputers.txt"
$Account= Read-Host "Please enter account name: "
$Password= Read-Host "Please enter password: "
foreach ($Computer in $PCName) {
"Computer:", $Computer
"Password set"
$Old=[ADSI]("WinNT://$Computer/$Account,User")
$Old.SetPassword($Password)
"User Added"
$server=[ADSI]"WinNT://$Computer"
$User=$server.Create('User',$Account)
$User.SetPassword($Password)
$User.SetInfo()
"Group Set"
$group=[ADSI]"WinNT://$Computer/Administrators,Group"
$group.Add($User.Path)
}