system nullreferenceexception
2 TopicsIt's time to make an exception
My script logs into the CIMC of my Cisco servers to gather hardware information. There's one particular server which it seems the CIMC is not responding and instead of getting a "unable to connect" error from PowerShell, I'm getting Write-Host "$StringLabel Error: $($PSItem.ToString())" [MyServerName/172.2.12.167 Error: Object reference not set to an instance of an object. I'm having difficulty capturing this exception. When I query the Exception type, I get: $Error[0].Exception.GetType().FullName System.NullReferenceException However my Catch [System.NullReferenceException] statement is not picking it up. Instead my [System.Exception] Catch is picking it up? Code is: Try { # Let's connect to the CIMC. $Connected = $True Write-Host "$StringLabel Attempting a connection to server IMC." -ForegroundColor Yellow $IMCHandle = Connect-IMC -Name $RackServerIMCIP -Credential $IMCServerCredential -NotDefault -ErrorAction Stop } Catch [System.NullReferenceException] { Write-Host "$StringLabel Error: $($PSItem.ToString())" -ForegroundColor Red $Connected = $False $ErrorPropertyValue = "Null Reference Error" } Catch [System.Exception] { # Did not connect due to connection issue. Write-Host "$StringLabel Error: $($PSItem.ToString())" -ForegroundColor Red If ($PSItem.Exception.ToString().Contains("Unable to connect")) { # Set connection flag false. $Connected = $False $ErrorPropertyValue = "Not Connected" } Else { $Connected = $False $ErrorPropertyValue = "System Exception" Write-Host "Not a connection issue." -ForegroundColor Red } } Catch { Write-Host "$StringLabel Error: $($PSItem.ToString())" -ForegroundColor Red $Connected = $False $ErrorPropertyValue = "Unknown Error" } What am I doing wrong?Solved969Views0likes2CommentsLync Server Application Pool on 2013 FE's Crashes- Why?
First published on TECHNET on Aug 30, 2016 by Steve Schiemann, UC Escalation Engineer – MicrosoftReviewed by - Jason Epperly, Windows Escalation Engineer, MicrosoftIntroductionI am a Skype for Business Escalation Engineer.1.9KViews0likes0Comments