try, catch doesent seem to work - powershell

I have the following code which I thought would allow me to catch an error and instead of generating the error write out "An Error Occurred".
Unfortunately, it still shows the error "Failed to restart the computer: Access is denied" instead.
I know why this is happening but I want to be able to catch the error and reformat it. What am I doing wrong?
try {
Restart-Computer -ComputerName MFG-KY-PC74 -Force
} catch {
Write-Host "An Error Occurred"
}

In PowerShell there are terminating and non-terminating errors. The former terminate script execution (if not caught) and can be caught by try..catch, the latter don't terminate script execution (so there's nothing to catch). The error you're receiving is a non-terminating one, so you need to make it a terminating error by appending -ErrorAction Stop to the statement:
try {
Restart-Computer -ComputerName MFG-KY-PC74 -Force -ErrorAction Stop
} catch {
write-host "An Error Occurred"
}
Alternatively you can set $ErrorActionPreference = "Stop" if you want all errors to become terminating errors.
See this article on the Scripting Guy blog for more information.

Related

Catching script-terminating errors only in PowerShell

PowerShell 5.1
I've written a wrapper script that launches another script, logs its output (via Start-Transcript), sends an email if it exited with a non-zero code, and finally rotates the log file based on file size.
I only want to catch errors that would cause the called script to exit when executed on its own.
(Script-terminating term based on the discussion here: https://github.com/MicrosoftDocs/PowerShell-Docs/issues/1583).
try {
Invoke-Expression "& '$Script' $ArgList"
$err = $LastExitCode
} catch {
Write-Error -ErrorRecord $_
$err = 1
}
Catch this:
$ErrorActionPreference = 'Stop'
Remove-Item -Path DoesntExist
or this:
throw "Error"
But not this (script should continue executing as it would if not called via the logging script):
Test-Path -Foo bar
Is it possible? Or is there a better approach?
A try { ... } catch { ... } statement makes no distinction between a statement-terminating (Test-Path -Foo bar) and a script-terminating error (throw "Error"): both sub-types of terminating errors trigger the catch block.
In other words: Any intrinsically statement-terminating error, such as 1 / 0 or Test-Path -Foo bar (an invocation-syntax error, because there is no such parameter) will invariably also trigger your catch block.
So will any non-terminating error that is promoted to a script-terminating one, either with $ErrorActionPreference = 'Stop' or -ErrorAction Stop (which is what you want).
As an aside: $ErrorActionPreference = 'Stop' - unlike -ErrorAction Stop - will also promote statement-terminating errors to script-terminating ones, which is just one of the problematic aspects of PowerShell's surprisingly complex error handling, discussed in detail in the GitHub docs issue linked to in your question.
Related information (which won't solve your problem):
With respect to post-mortem error analysis, the best you can do to analyze what kind of error occurred is to check if it was a script-terminating one triggered by a throw statement, by examining $_.Exception.WasThrownFromThrowStatement, but you won't generally be able to distinguish between a statement-terminating error, a non-terminating error, and a non-terminating error promoted to a script-terminating one.
GitHub issue #4781 discusses a potential future improvement for detecting the intrinsic vs. effective "fatality" of an error.

Powershell: cannot hide 'Access is denied' error on 'Remove-Item'

I would like to keep a Remove-Item instruction quiet, exception or not. I'm running below command in a script to delete a certificate:
Remove-Item $store\$thumbprint
If I run the script as local Admin, fine... it keeps quiet and the file is deleted.
If however I run it as unpriviledged user, I get an 'Access in denied' error as expected, but I would like to keep this quiet in any case.
I've tried the following:
$output = (Remove-Item $store\$thumbprint)
# or...
try{Remove-Item $store\$thumbprint} catch{}
# or...
Remove-Item $store\$thumbprint -ErrorAction SilentlyContinue
But I always get the error/exception
displayed on the console.
By default, a non-terminating error is generated by Remove-Item and it adds an error to the $Error variable without throwing an exception. To see what Windows PowerShell will do when a non-terminating error arises, look at the value of the $ErrorActionPreference variable (its default value is Continue).
The Access to the path '…' is denied is an example of such a non-terminating error so you can use ErrorAction parameter which overrides the value of the $ErrorActionPreference variable for the current command:
Remove-Item $store\$thumbprint -ErrorAction SilentlyContinue
On the other side, $ErrorActionPreference and the ErrorAction parameter don't affect how PowerShell responds to terminating errors that stop cmdlet processing. So if we are not sure whether an error is terminating or not then it's safe to handle any error the Try-Catch-Finally blocks using -ErrorAction Stop as follows:
try {
Remove-Item $store\$thumbprint -ErrorAction Stop
} catch {
### A Catch block can include commands for tracking the error
### or for recovering the expected flow of the script
}

Powershell database connection error detection and error handling

I am trying to detect if a database connection succeeded, and if so, do work, otherwise,report the connection error and obviously bypass the DB tasks.
I'm embarrassed that I seem to be failing Syntax 101.
I've tried two different approaches, neither which works.
Approach 1
In this code, I can't seem to get the $connection.State = Open syntax correct. I can see in the debugger $connection.State = Open -- i just can't seem to test for it properly.
$connection = New-Object Oracle.ManagedDataAccess.Client.OracleConnection($conninfo)
Try {$connection.open()}
Catch {Write-Warning "Sorry, DB Connect failed"}
If ($connection.State = Open) {
Write-Output "No Error"
DoWork()
$connection.Close()
}
Approach 2:
In this code, connection errors are caught, but successful connections do not enter the expect "No error block".
$connection = New-Object Oracle.ManagedDataAccess.Client.OracleConnection($conninfo)
Try {$connection.open()}
Catch {Write-Warning "Sorry, DB Connect failed"}
If (!$error) {
Write-Output "No Error"
DoWork()
$connection.Close()
}
}
Try{}Catch{} only hit terminating errors. Try something like:
Try
{
$erroraction = "stop"
$connection.open()
$erroraction = "continue"
}
Catch {
Write-Warning "Sorry, DB Connect failed"
}
of use non-terminating error handling
$connection.open()
if(!$?) #did the last command fail?
{
Write-Warning "Sorry, DB Connect failed"
}
edit: I may have misunderstood the exact question, but if the problem is that $? is incorrectly true on an error, its because it uses only the last command. So your catch block running write-warning is successful, you'll need to store the $? value immediately after the command you expect to fail or set a bool in the catch block directly to trigger your next block.
OK, although it doesn't answer all my questions, here is working code:
try {
$connection.Open()
Write-Debug "No Error"
doStuffHere()
}
Catch {Write-Warning "Sorry, DB Connect failed"}
My failure was not remembering any error occurring inside the try block results in immediately exiting the try block and going to the catch. (Actually, its one of those things I forgot I knew -- I had to be reminded.)
Aside from that though, I'd still like to determine the correct way to test Connection.State to see if the connection is open or closed. Probably need to open another question.

Exchange anagement Shell catch enable-mailbox error

I have script that creates user accounts and establishes an e-mail address for those accounts. All 'create-commands' are surrounded by a try/catch block to catch errors and add a message to the output log. This works fine... except for the enable-mailbox command
try {
Enable-Maibox (.. parameters ...)
}
catch {
$errorsEncountered = $true
Write-Output "Error establishing e-mail address for $($UserData.username)"
}
when the enable-mailbox command fails... the catch-part is skipped. Why is this? And how can I fix this?
Non-termineting errors are not catched. Use '-ErrorAction Stop' to make the errors terminating errors.
Enable-Maibox (.. parameters ...) -ErrorAction Stop
I could be wrong but "Enable-Maibox" looks mis-spelled.

Powershell Try Catch invoke-sqlcmd

I am having problems catching an error in PowerShell when a connection fails to a SQL Server using Invoke-Sqlcmd. This is some generic code to demonstrate the issue:
CLS
$server = "Localhost\fake"
try
{
Invoke-Sqlcmd -Query "SELECT DB_NAME() as [Database]" -Server $server
}
catch
{
Write-Host "Error connecting to server " $server
}
I get the following error:
Invoke-Sqlcmd : A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name
is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
I was expecting to get the one line statement: "Error connecting to server Localhost\fake"
It would appear that error is considered non-terminating which is a bit odd. Try the Invoke-SqlCommand with an additional parameter: -ErrorAction Stop. If there error is non-terminating, this will convert it to a terminating error that you can catch.
Posting additional info to supplement the answer by #KeithHill as an answer since it is too long for a comment.
If the error record was created by the Write-Error commandlet it is non-terminating and subject to the behavior specified by the -ErrorAction argument or the $ErrorActionPreference system variable. Errors using throw are terminating. See the documentation for Write-Error (Note that PowerShell 4.0 and below do not have the -Exception parameter mentioned in the web page.) and about_Throw in the PowerShell help system.
If you want to add custom error information and make it a terminating error, throw from your catch block as follows:
catch
{
throw (New-Object System.Exception "Error connecting to server $($server).", $_.Exception)
}
You can use Write-Error if you want termination to behave as specified by the -ErrorAction argument. In PowerShell 4.0 and below the Write-Error commandlet does not allow a -Exception argument and therefore will not provide an InnerException. That means that the caller would have to examine the collection in the $Error system variable if it needs to determine the original exception. In later versions you can Use Write-Error -Message "Some additional error information." -Exception $_.Exception in your catch block.
Try
CLS
$server = "Localhost/fake"
try
{
Invoke-Sqlcmd -Query "SELECT DB_NAME() as [Database]" -Server $server
}
catch
{
$_ | Out-Null
Write-Host "Error connecting to server " $server
}
This will capture the error and redirect it to null and display your write-host