Solved

Try / Catch error during Powershell script

Posted on 2012-03-14
1
956 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

16 Experts available now in Live!

Get 1:1 Help Now