Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Working with error traps

Status
Not open for further replies.

Herdrich

Technical User
Dec 30, 2008
83
0
0
US
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)
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top