?
Solved

Try / Catch error during Powershell script

Posted on 2012-03-14
1
Medium Priority
?
977 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 11

Accepted Solution

by:
Joe Klimis earned 2000 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

743 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