Need to grab Exceptions from a test-outlookconnectivity test in powershell...
If I run the command test-outlookconnectivity -protocol:http -verbose, I will get a large amount of output on the screen. it gives me the verbose or complete informatiuon of what is being tested. Also what will be included in the verbose
are Error: system.exceptions like (The RPC server is unavailable), is an example. What I would like to be able to do is catch or grab if you will, that exception and add it to a variable for a custom output. Example:
If my test is like the following:
$variable = @()
$test = test-outlookconnectivity -protocol:http -verbose
c:\$test
error = exception microsoft.system.connectivity (The rpc server is unavailable). I would like to store the error itself into $variable, and then be able to output $test and $variable seperately to the screen together. Any thoughts?
I also found the following article that looks like it might be the same topic:
http://social.technet.microsoft.com/Forums/en/ITCG/thread/f9551e49-fe32-47f6-b4d0-f3e7e5858925JCtech1123, Cheers
February 28th, 2012 10:15pm
A couple of different ways to do this:
Try {
$test = test-outlookconnectivity -protocol:http -verbose -ErrorAction Stop
} Catch {
$ErrorMessage = $_.Exception.Message
Write-Warning ("{0}" -f $_.Exception.Message)
}
Of use the -ErrorVariable to hold the error itself
$test = test-outlookconnectivity -protocol:http -verbose -ErrorAction silentlycontinue -ErrorVariable ErrorMessage
$ErrorMessage will hold the error message now.
Boe Prox
Please remember to mark the best solution as the answer using Mark as Answer. If you find a solution to be helpful, please use
Vote as Helpful.
Looking for a script? Check out the
Script Repository
Need a script written for you? Submit a request at the
Script Request Page
Free Windows Admin Tool Kit Click here and download it now
February 28th, 2012 10:28pm
ok so i tried both exactly how you have them and i got a -verbose itself to the screen, output to the screen. So if my test is like this:
$result = test-outlookconnectivity -protocol:http -trustanysslcert:$true -verbose. How would I incorporate it into what you posted earlier:
Try {
$test = test-outlookconnectivity
-protocol:http -verbose
-ErrorAction Stop
} Catch {
$ErrorMessage = $_.Exception.Message
Write-Warning
("{0}" -f $_.Exception.Message)
}
I want to store the verbose error so I can use it based on other variables. Thx!JCtech1123, Cheers
March 1st, 2012 12:03am
Update, so when I use the try in powershell, like this:
try {command} catch [error.exception...] {run action},
I get the following error in powershell in server 2008, exchange 2010: "the command try is not recognized as a valid command".
Any thoughts?JCtech1123, Cheers
Free Windows Admin Tool Kit Click here and download it now
March 26th, 2012 5:07pm