Handling Warnings in Powershell

I have been attempting to do some maintenance on thousands of exchange mailboxes and would like to base a statement on one of the warnings returned by the get-mailboxstatistics cmdlet.  I'm running get-mailboxstatistics -warningvariable X -identity <alias> | select lastlogontime,storagelimitstatus.  I'm getting the warning "The user hasn't logged on to mailbox '<alias>' (big long SID), so ther is no data to return.  After the user logs on, this warning will no longer appear."  I would like to trap this warning and use it for decision making in my script but I can't seem to get it into the warning variable.   Any suggestions?

thanks
spicer411Asked:
Who is Participating?
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
A warning is a warning - not an error. It will neither stop the script, nor be captured by a trap or try/catch. You need to use -WarningAction Stop for both to happen (default value is Continue), or set the preference variable $WarningPreference to Stop. In my tests only changing the preference var is working, the -WarningAction seems to be ignored. However, you won't get the warning text, only the error message that the script is stopped because of the preference setting.

The warning variable should nevertheless contain something whenever a warning is printed out, and hence evaluated for checking if there were warnings. This does not seem to work and is reported as a bug, according to http://jdhitsolutions.com/blog/2011/05/warning-signs/, as it seems the setting is not propagated to the underlying Write-Warning commandlet. The -errorVariable works, btw.
Probably that is fixed in PS3, but that isn't released yet.

Sorry, there is no workaround different than to scan the command output, or check for $null in variables/attributes you expect to get back.
0
 
Brent ChallisPrincipal: ITCommented:
The preferred way to deal with problems occurring in your code at run time is to use the try/catch construct:
try
{
  your code goes here
}
catch
{
  you deal with the problem here.  The variable $_ contains the exception that occurred
}

You can put as much code as you want to in the try block and if one of the lines fails control goes to the catch block.

Here is the on-line reference:
http://technet.microsoft.com/en-us/library/dd315350.aspx
0
 
prashanthdCommented:
You can also use Transcript, which would capture all the warnings.

"Start-Transcript filepath" before executing command and "Stop-Transcript" after finishing
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
Brent ChallisPrincipal: ITCommented:
The down side of using Transcript is that it is simply providing information for review rather than getting your code to respond.
0
 
spicer411Author Commented:
Is there any reason why the -warningvariable doesn't work at all?  I receive a warning when executing the command but it doesn't terminate the script.  The script keeps processing but I want to collect any warning information so I can do something a little different with the accounts that have never been logged onto.

I've seen the -warningvariable switch all over the place but it seems to not work at all.  Anyone else experience this?
0
 
chrismerrittCommented:
As above, does the Try Catch statement not work? I use this all the time (granted sometimes more success than others) but even so, worth at least trying it :)

Try
{
	Get-MailboxStatistics -Identity $Mailbox
}
Catch [Exception]
{
	Write-Host -ForeGroundColor "Magenta" "$_"
}
Finally
{
	#Do something here, even if an error occurs if you want to.
}

Open in new window

0
 
spicer411Author Commented:
I've requested that this question be deleted for the following reason:

It's been a year since I've been on Experts-Exchange.  I have forgotten about that script.
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Objection: Since this thread contains valuable information about bugs/misbehaviour of PowerShell, and direct advice how to work with warnings arising when executing a script, I reckon it should not get just disposed, but posts accepted. In particular mine ;-).
0
 
spicer411Author Commented:
So Qlemo,

Are you telling me that you were able to actually capture the warning and examine the text of the warning to make a determination as to which direction you wanted your script to go?  If so, that's what I needed.  I can capture errors and examine their text all day but warnings seem to just disappear into the bitbucket.  It has been a year since I worked with that script.  I must've found another way to handle the situation and moved on.  I quit using Experts Exchange because I've rarely gotten an answer to my questions that could be implemented in my particular situations.  I'm back again now looking to see if I can find an answer to a problem I have with DPM 2010 backing up VM's in a 2012 cluster.  Wish me luck. ;-)
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
This part

Sorry, there is no workaround different than to scan the command output, or check for $null in variables/attributes you expect to get back.
is what I refer to. The warning variable just does not work yet with many cmdlets.
0
 
spicer411Author Commented:
No problem.  This thread can be a source to help someone else realize that there's a problem with powershell capturing warning text....as you mentioned.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.