Difference between revisions of "Power Shell"

Jump to navigation Jump to search
311 bytes added ,  10:09, 22 March 2010
(→‎Exceptions and Error Handling: Added "Try...Catch")
Line 372: Line 372:




=== Basic Error Catcher ===
=== Basic Error Handler ===
 
If you know where the error is likely to occur, then just place an error catcher immediately after it.  This doesn't stop the exception appearing on the console, but does allow you to take some action as a result.
If you know where the error is likely to occur, then just place an error catcher immediately after it.  This doesn't stop the exception appearing on the console, but does allow you to take some action as a result.
<pre>
<source lang="powershell">
if (-not $?) {
if (-not $?) {
     # Handle error here
     # Handle error here
   }
   }
</pre>
</source>
 
=== Try...Catch ===
Used to catch an exception in a script block where an exception may be likely.  Stops the exception being shown on the console
<source lang="powershell">
try
{
    # Something in which an exception is likely
}
catch
{
    Write-Host "FAILED: $_"
    Exit
}
</source>


[[Category:PowerShell]]
[[Category:PowerShell]]
[[Category:WMI]]
[[Category:WMI]]
[[Category:MySQL]]
[[Category:MySQL]]

Navigation menu