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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

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
Qlemo"Batchelor", 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Qlemo"Batchelor", 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
Qlemo"Batchelor", 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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.