header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Subject: how to capture gwmi failure output?
Prev Next
You are not authorized to post a reply.

Author Messages
KarlMitschkeUser is Offline
Power User
Power User
Posts:144

07/23/2008 1:36 PM  

I have a script that first pings 200 servers to see if they are alive.

The ones that are alive, i do :

Get-WmiObject -Class Win32_Share -ComputerName $servername

On some subset of the servers, I get this in the console:

Get-WmiObject : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

How can I capture that, so I can add it to an object that I build to hold failures?

I tried

$result = Get-WmiObject -Class Win32_Share -ComputerName $servername

which doesn't help at all.

I thought of doing a start-transcript, and stop-transcript, for each server, and then parsing the output file, but that's just ugly!

Any other ideas, besides using

$?
which just returns true or false?

bsonposhUser is Offline
CLI Addict
CLI Addict
Posts:363

07/23/2008 1:40 PM  
you have two options

1) redirect the error stream into the pipe using 2>&1

2) you can check for error status with using the special variable $?

Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
bsonposhUser is Offline
CLI Addict
CLI Addict
Posts:363

07/23/2008 1:40 PM  
oh.. by checking $? you can determin if you want to access $errorΎ] or not. That will contain the last error.

Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
KarlMitschkeUser is Offline
Power User
Power User
Posts:144

07/23/2008 1:45 PM  

Brandon;

I had actually tried

$result = Get-WmiObject -Class Win32_Share -ComputerName $servername 2>&1

that didn't seem to help - should $result hold the error?

bsonposhUser is Offline
CLI Addict
CLI Addict
Posts:363

07/23/2008 1:46 PM  
I am not sure about you, but all I see is a scroll bar.

Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
KarlMitschkeUser is Offline
Power User
Power User
Posts:144

07/23/2008 1:48 PM  

I saw that too - I don't know why, but for some reason I always have to edit my code /code posts before they show up.

bsonposhUser is Offline
CLI Addict
CLI Addict
Posts:363

07/23/2008 1:50 PM  
oh... so the problem is that your getting a terminating error. This stops it before the 2>&1 takes effect.

You can do this
$result = @(Get-WmiObject -Class Win32_Share -ComputerName $servername 2>&1)

Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
KarlMitschkeUser is Offline
Power User
Power User
Posts:144

07/23/2008 1:51 PM  
Brandon;

Is $errorΎ] supposed to be error[ 0 ] without the spaces?

Thanks!

Karl
bsonposhUser is Offline
CLI Addict
CLI Addict
Posts:363

07/23/2008 1:53 PM  
$error[ 0 ] is the last error.

Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
KarlMitschkeUser is Offline
Power User
Power User
Posts:144

07/23/2008 1:55 PM  
$result = @(Get-WmiObject -Class Win32_Share -ComputerName $servername 2>&1)
$result is null
$error[ 0 ] shows:
Get-WmiObject : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
At line:1 char:27 + $result = @(Get-WmiObject <<<< -Class Win32_Share -ComputerName $servername 2>&1)

I get the same error [ 0 ] With:
Get-WmiObject -Class Win32_Share -ComputerName $servername

Get-WmiObject : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
At line:1 char:14 + Get-WmiObject <<<< -Class Win32_Share -ComputerName $servername

So, it looks like I can just read error [ 0 ] for each gwmi call

Thanks for stearing me in the right direction

bsonposhUser is Offline
CLI Addict
CLI Addict
Posts:363

07/23/2008 2:00 PM  
"So, it looks like I can just read error [ 0 ] for each gwmi call "
No... you $error[ 0 ] is going to have what ever the last error was. It does not clear its self.

Ideally you can do something like

If(!$?){$error[ 0 ]}

Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
bsonposhUser is Offline
CLI Addict
CLI Addict
Posts:363

07/23/2008 2:01 PM  
as a side note:

This did work for me.
$result = @(Get-WmiObject -Class Win32_Share -ComputerName $servername 2>&1) 
$result

Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
KarlMitschkeUser is Offline
Power User
Power User
Posts:144

07/23/2008 2:17 PM  
If(!$?){$error[ 0 ]} works fine for me - thanks

$result actually has data in it? Mine is completly blank.

$result |gm gives me this: "Get-Member : No object has been specified to get-member."
KarlMitschkeUser is Offline
Power User
Power User
Posts:144

07/23/2008 2:28 PM  
You know, this is rather a moot point - I was doing

if ($result -eq $null)
{
$WMIFailureObj = new-object psObject
$WMIFailureObj  | Add-Member -membertype noteproperty -name Servername -Value $servername
$WMIFailureObj  | Add-Member -membertype noteproperty -name "WMI Status" -Value "failure"
$WMIFailure += $WMIFailureObj
}

I realize now, I don't really care what the real error is :)
bsonposhUser is Offline
CLI Addict
CLI Addict
Posts:363

07/23/2008 3:43 PM  
so... do this

f(!$?)
{
$WMIFailureObj = new-object psObject
$WMIFailureObj  | Add-Member -membertype noteproperty -name Servername -Value $servername
$WMIFailureObj  | Add-Member -membertype noteproperty -name "WMI Status" -Value $error[ 0 ]
$WMIFailure += $WMIFailureObj
}

Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
KarlMitschkeUser is Offline
Power User
Power User
Posts:144

07/24/2008 7:01 AM  
When I try that, $error[ 0 ] is "Missing statement block after if ( condition )."
$error[ 1 ] has the correct error, however.

So:

if(!$?)
{
$WMIFailureObj = new-object psObject
$WMIFailureObj  | Add-Member -membertype noteproperty -name Servername -Value $servername
$WMIFailureObj  | Add-Member -membertype noteproperty -name "WMI Status" -Value $errorΏ].tostring().split(".")Ύ]
$WMIFailure += $WMIFailureObj
}


Thanks!
edm365f31User is Offline
New Member
New Member
Posts:17

08/05/2008 8:58 AM  

My question is getting back on how to avoid hangs or bypass in wmi when runing scripts. Can you use this output error and move on to the next box ?

something goto statement move to next list

If $result = $error

glnsizeUser is Offline
Shell Enthusiast
Shell Enthusiast
Posts:60

08/05/2008 6:56 PM  

I recently had to tackle the same problem.  Even the smallest hang in a large gwmi loop will take forever.  Eventually I found the combination of streamlining your query, and simply eliminating machines that were off, will drastically improve performance. Access denied is returned back immediately, the big killer is RPC service unavailable.   However, 90% of those are either Firewall, or powered off, and almost all of those will fail a ping.

Unfortunately, if an error is encountered you can suppress the reporting, but you cannot prevent PowerShell from processing it.  My answer to this was LINK... the PSCX Ping-Host can eliminate the stragglers (RPC unavailable), and a simple switch statement to catch the error's. Version 1 of that script took over 2hours to run. I ran it today before I posted it 200+ servers in less than 7 min. The flip side of that was the 40+ that were skipped but those 40 would have added at least 50-80 min, and I have a different script to handle them…

~Glenn

 

 

halr9000User is Offline
CLI Addict
CLI Addict
Posts:245


08/05/2008 7:01 PM  
If you don't want to depend on snapins, my Select-Alive script can help. I also blogged about it.

Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast (http://powerscripting.net)
Author, TechProsaic (http://halr9000.com)
You are not authorized to post a reply.
Forums > Using PowerShell > General PowerShell > how to capture gwmi failure output?



ActiveForums 3.7
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer