Calzor Suzay
Calzor Suzay

Reputation: 53

Suppressing fatal error (access denied)

Going round in a circle here... I'm trying to handle and continue from 'fatal errors' in scripts. I know the -EA silentlycontinue doesn't work but keep coming back to using foreach to get around it but solutions I find don't work for me for example and this is an example not what I'm trying to do...

The code:

get-content serverLists.txt | 
foreach {get-wmiobject -computer $_ -query "Select * from win32_logicaldisk where drivetype=3"} | 
Format-Table SystemName,DeviceID,Size,FreeSpace,VolumeName

Dies with:

get-wmiobject : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
At line:1 char:40
+ get-content serverLists.txt | foreach {get-wmiobject -computer $_ -query "Select ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-WmiObject], UnauthorizedAccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

I want it to just continue, I've tried and investigated try/catch this just gives me the error in a readable format and stops, I've looked at ping/port check solutions but some servers are behind firewalls but certain ports are open etc I just want it to handle the fatal error and just carry on...

BTW this isn't a rights issue, it'll pass through a whole bunch of servers fine then the script will die on one and stop

Upvotes: 0

Views: 2969

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200493

try..catch should do what you want:

Get-Content serverLists.txt | % {
  try {
    gwmi -Computer $_ -Query "SELECT * FROM Win32_LogicalDisk WHERE DriveType=3"
  } catch {}
} | Format-Table SystemName,DeviceID,Size,FreeSpace,VolumeName

or you could change $ErrorActionPreference:

$ErrorActionPreference = "SilentlyContinue"
Get-Content serverLists.txt | % {
  gwmi -Computer $_ -Query "SELECT * FROM Win32_LogicalDisk WHERE DriveType=3"
} | Format-Table SystemName,DeviceID,Size,FreeSpace,VolumeName

Upvotes: 2

Related Questions