Link to home
Start Free TrialLog in
Avatar of PennCharter
PennCharter

asked on

Exception Handeling for Powershell

I have a powershell script that goes through all the computers in my domain. When the script comes to a computer that is not turned on, it will throw the error:

"Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)"

What I would like to do is catch the exception and have it so I can create an array of computers that were not turned on, so I can scan them later.

here is the script:

$date = ( get-date ).ToString('yyyyMMdd')
"Computer info on $date" > "Q:\logs\$date-comp-info.txt"
$computers = Get-Content Q:\logs\computers.txt
$pass = cat C:\securestring.txt | convert-tosecurestring                                                                                 
$mycred = new-object -typename System.Management.Automation.PSCredential -argumentlist DOMAIN\admin,$pass
 
foreach($computer in $computers){
  if($computer){
    echo "Scanning $computer"
    $info = gwmi win32_computersystem -Computername $computer -Credential $mycred
    $info | Add-Content -Path "Q:\logs\$date-comp-info.txt" -Encoding Unicode
  }
}

Open in new window

Avatar of PennCharter
PennCharter

ASKER

Anyone know how to do this?
ASKER CERTIFIED SOLUTION
Avatar of BSonPosh
BSonPosh
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial