Handle exception in powershell - powershell

I have a config file when you can add a list of performance counters, for example :
\Memory\Page Faults/sec
\Memory\Available Bytes
\ASP.NET Applications(*)\Requests in Application Queue
I have a powershell script, that parses the file and does a Get-Counter parameter where parameter is each counter in my config file. Everything works well, but I would like my script to be able to handle exception when an user add a bad counter, for example :
\Memory\Page Faults/sec
\Memory\Available Bytes
\ASP.NET Applications(*)\Requests in Application Queue
ddftrigjgigjij
With this, I get a red exception in my powershell console which I would like to replace with an appropiate message like "counter not found". I am beginning with powershell and I tried to use try\catch without success.
Thanks in advance for your help.

Add -ErrorAction Stop to Get-Counter, this will make try/catch work as you expect:
try{
Get-Counter "bogus counter" -ErrorAction stop
}
catch {
echo "counter not found"
}

Related

Try-Catch not working while invoking exe through powershell

I'm using microsoft's document translator msi to translate documents.I'm invoking this msi through powershell but if any error occures while processing document , script is unable to catch the error .
commandline to start instance of translator msi and translate document:
DocumentTranslatorCmd translatedocuments /documents:d:\testdocuments\*.docx /from:en /to:de,el
I tried below approaches but it looks like i'm doing something wrong :
using $LastExitCode
DocumentTranslatorCmd translatedocuments /documents:d:\testdocuments*.docx /from:en /to:de,el
if($LastExitCode -ne 0){
do something..
}
if error occurs during processing it does exit out of this command and control does not reaches to if().So, i used exit command like :
DocumentTranslatorCmd translatedocuments /documents:d:\testdocuments\*.docx /from:en /to:de,el | exit
-ErrorAction stop
try{
DocumentTranslatorCmd translatedocuments /documents:d:\testdocuments*.docx /from:en /to:de,el -ErrorAction stop
}catch{
}
if this is used DocumentTranslatorCmd command considering it as a argument.
Any kind of help would be greatly appreciated.

Powershell code not exiting after try catch block

I have a code that downloads a file from SharePoint, edits it, uploads it back to SharePoint and finally sends a confirmation email. I have functions for each of the tasks. The code works fine.
However, I want to add error exception for a condition when if the file is open in SharePoint by some user, show error message and exit code. The issue I am experiencing is the code continues to run even if there is an exception. In the below code, the sendMail function is called even when the getSharePointFile function fails.
I have tried $ErrorActionPreference = "Stop" but with that, the catch block is not executed and my custom error MessageBox is not displayed. Thanks in advance.
Here is the relevant code:
function getSharePointFile {
Connect-PnPOnline $SharepointURL -UseWebLogin
Get-PnPFile -Url $fileRelativeURL -Path $localFilePath -FileName $fileName -AsFile -Force
}
function runCatch {
$showMessage = [System.Windows.Forms.MessageBox]::Show($_)
exit
}
try {getSharePointFile}
catch{runCatch}
try {updateAuditResults}
catch{runCatch}
try {uploadToSharePoint}
catch{runCatch}
try {sendMail}
catch{runCatch}
Two issues:
First, you have four independent try catch blocks, and an error handled in one has no impact on the others.
Try something like this:
try {
getSharePointFile
updateAuditResults
uploadToSharePoint
sendMail
}
catch{runCatch}
The first line to generate an error will end the batch of commands and jump to the catch block. The rest of the batch will be skipped.
The second issue you might run into is not all PowerShell cmdlets return errors when they fail. You will need to test yours to verify. Some will just display error text and continue.
More info here: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_try_catch_finally
As #Mike Smith said, with the code you provided, it seems better to merge your function calls inside one try catch block.
Depending on your PowerShell version, you also need to add -ErrorAction Stop parameter to trap errors into the catch block (add it for the cmdlets, not the function call)
function getSharePointFile {
Connect-PnPOnline $SharepointURL -UseWebLogin -ErrorAction Stop
Get-PnPFile -Url $fileRelativeURL -Path $localFilePath -FileName $fileName -AsFile -Force -ErrorAction Stop
}

How to make verification process in PowerShell?

I have powershell script. It has a lot of process such are get content, copy file, rename extension file, remove file, append text.
I want to make verification for each process, so I will continue to the next process once I got correct result. For the example, for checking file, I use Test-Path. Is anyone give me idea, which verification is better to use in PowerShell?
Thank you for your advice.
You can add -WhatIf parameter to debug any cmdlet. Also you can handle any errors and exceptions using Try..Catch block.
E.G
Try {
Get-Content "C:\foo.txt" -ErrorAction Stop
}
Catch {
# The statements put here will be executed if any error was caught in try block
}
You can catch specific error using:
Try {
Get-Content "C:\foo.txt" -ErrorAction Stop
}
Catch [System.Management.Automation.ItemNotFoundException] {
# The statements put here will be executed if file not found
# Any exception can be placed in the [] braces
# ErrorAction Common parameter is used to pass the errors to catch block and halt script on error
# You can remove that if you don't want to halt script on error
}

Script stops execution when faces an error

I have a master script master.ps1 which calls two scripts One.ps1 and Two.ps1 like:
&".\One.ps1"
&".\Two.ps1"
When the One.ps1 script has an error, the execution gets stopped without continuing the execution of Two.ps1
How to continue execution of Two.ps1 even if there is an error in One.ps1?
You have to set the $ErrorActionPreference to continue:
Determines how Windows PowerShell responds to a non-terminating
error (an error that does not stop the cmdlet processing) at the
command line or in a script, cmdlet, or provider, such as the
generated by the Write-Error cmdlet.
You can also use the ErrorAction common parameter of a cmdlet to
override the preference for a specific command.
Source.
$ErrorActionPreference = 'continue'
Note: As a best practice I would recommend to first determine the current error action preference, store it in a variable and reset it after your script:
$currentEAP = $ErrorActionPreference
$ErrorActionPreference = 'continue'
&".\One.ps1"
&".\Two.ps1"
$ErrorActionPreference = $currentEAP
#Martin is correct assuming that the success or failure of .\One.ps1 does not impact .\Two.ps1 and if you don't care about logging or otherwise dealing with the error. but if you would prefer to handle the error rather than just continue past it you could also use a Try{}Catch{} block as below to log the error (or take any other action you would like in the Catch{})
Try{
&".\One.ps1"
} Catch {
$error | Out-File "OneError.txt"
}
Try{
&".\Two.ps1"
} Catch {
$error | Out-File "TwoError.txt"
}
Other ways to format this but you get the idea.

Task Scheduler with Powershell Sharepoint ps1

I am facing an issue using the Task Scheduler to run a Sharepoint Powershell script.
I'm using the following in the Task Scheduler :
-Command "& 'C:\Users\crpmcr\Desktop\Upload\Script.ps1'"
This is the resume of my script :
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
New-Item "[PATH]" -type file
$stream = [System.IO.StreamWriter] "[PATH]"
$stream.WriteLine("Message Example")
Try{
$web = Get-SPWeb "[WebApplicationUrl]"
}
Catch{
$stream.WriteLine("Error")
}
$stream.close()
If i remove the line in the try, i get the Message Example line in my new file. But it seems that the line in the try does make everything break. My file is created but it's empty. Even if some text has been added before. Also the rest of my script using the web is not working obviously.
Any idea about what my problem could be ?
Thanks!
If you are running PowerShell from
C:\Windows\SysWOW64\WindowsPowerShell\v1.0
Try changing it to
C:\Windows\System32\WindowsPowerShell\v1.0
And see if that makes any difference.
Solution found. My script was creating a file for logs and when i clicked in it it was empty. So i thought there was an issue but in fact it's because the line GetSP-web take severals seconds on my server. so it blocks the writing while it's looking for the web. 10 seconds later my file had the lines added. Obviously i was too fast and had to wait longer to see the result.