Advertisement

07.21.2008 at 07:00AM PDT, ID: 23581813
[x]
Attachment Details

Exception Handeling for Powershell

Asked by PennCharter in Programming Languages, Scripting Languages, MSH/Monad PowerShell

Tags: powershell, v2.0

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:
Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
$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
  }
}
[+][-]07.21.2008 at 12:37PM PDT, ID: 22053646

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.21.2008 at 01:25PM PDT, ID: 22054000

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Programming Languages, Scripting Languages, MSH/Monad PowerShell
Tags: powershell, v2.0
Sign Up Now!
Solution Provided By: BSonPosh
Participating Experts: 1
Solution Grade: A
 
 
[+][-]07.21.2008 at 01:27PM PDT, ID: 22054020

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628