INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Working with error traps

Working with error traps

(OP)
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)
} 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close