Solved

Try / Catch error during Powershell script

Posted on 2012-03-14
1
959 Views
Last Modified: 2012-03-15
hi,

I'm running this little script :

&{
      $myHosts
      get-Stat -Entity $myHosts
}
trap [VimException]
{
      Write-Error "fails"
}

I want to trap the error from get-stat but it's never catch.
Here is the output of the script :

Get-Stat : 14.03.2012 21:07:39    Get-Stat        Object reference not set to a
n instance of an object.
At C:\vCheck6\test.ps1:3 char:10
+     get-Stat <<<<  -Entity $myHosts
    + CategoryInfo          : NotSpecified: (:) [Get-Stat], VimException
    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomatio
   n.ViCore.Cmdlets.Commands.GetViStats

Any ideas are welcome !
0
Comment
Question by:Sybux
1 Comment
 
LVL 10

Accepted Solution

by:
JoeKlimis earned 500 total points
ID: 37722906
Hi your , problem is that it is generating a non terminating error

here is an example of how to trap them

$ErrorActionPreference = "Stop"

try 
{ 
    copy-item "c:\notexists\file.txt" "c:\temp" 
} 
catch {

write-host "bad copy"

}

Open in new window


The key is Line1   ,
this can be reverted by issuing
$ErrorActionPreference = "Continue"
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now