Right now I’m using a line like thus: (based on code others have discussed on SO)
(New-Object System.Net.WebClient).DownloadFile( $servicePath, $responseFile)
And in a few cases my $servicePath is tossing a 500. While I could very neatly throw that information up in a browser window, I prefer to download it (collection of previous events is a good thing yeah?) into a file and allow others to refer to it later.
The issue is, my try-catch is hitting an exception for the 500 and I’m not sure how to capture the “yellowtext” of the IIS error. How can I do that with PowerShell 2.0? I’m willing to switch from a WebClient to something else, but I would prefer to stay in purely PS space, without adding in a new library if I can do so.
If I attempt it like thus in the powershell ISE, I get the following:
$o = @{};
$err = @{};
try {
$o = New-Object System.Net.WebClient;
$o.DownloadFile($servicePath, $responseFile);
} catch {
$err = $_;
}
and
$err.Exception.Message => "The remote server returned an error: (500) Internal Server Error."
How can I capture the yellowtext? Obviously my browser can capture the yellowtext as it shows it to me. Do I need to use a HttpWebResponse or something?
AAAAHHHHH I actually have the answer to a problem on here!!!! I had this same problem yesterday!!! 🙂 enough of my ranting. The thing is $o stores that message your getting as an object if you want to actually do anything with it the easiest thing would be to convert it to a string by piping your output to the out-string cmdlet. Now you can do whatever you need to do with the $strerror variable. Let me know if I can be of further assistance.