Powershell code not exiting after try catch block - powershell

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
}

Related

PowerShell ErrorRecord

when I run a script and it throws an exception, another script is run (log script).
I have to give the current error object to the log script.
try
{
New-Item -Path 'E:\test.txt' -ItemType 'File' -ErrorAction Stop
}
catch
{
Write-Log -ErrorObject $Error
}
How can I access the error object of the first script directly from the second script?
The best way would be to use the .Net framework (System.Management.Automation.ErrorRecord).
I want to output a complete error message.

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
}

How to catch error from Remove-Item and emit a warning instead?

In a post-deployment script used in a continuous integration pipeline (Azure DevOps), I'm removing old files.
Basically, it's a PowerShell script that removes every release folder but the current one in the deployment directory.
Sometimes, the Remove-Item fails for some reason (old file still opened by someone one the deplyoment machine, for instance)
It's not a big deal. I don't want an error saying my whole deployment failed because of this. However, I want a warning, so I'm aware that it happened.
For instance (MCVE):
Remove-Item INEXISTENT_FILE
Problem : it causes an error.
Attempt 1 :
Remove-Item INEXISTENT_FILE -ErrorAction SilentlyContinue
Problem : It removes the Error completely, that's not what I want (I want a warning)
Attempt 2 : I tried to use ErrorVariable as recommended here : https://devblogs.microsoft.com/powershell/erroraction-and-errorvariable/
Remove-Item INEXISTENT_FILE -ErrorAction SilentlyContinue -ErrorVariable $removeItemError
if ($removeItemError) {
Write-Warning "Warning, something failed!"
}
Problem : it doesn't work, it doesn't show the if part. If I remove "SilentlyContinue" error action, it just emits an error, and in any case never goes into the if part.
Attempt 3 : I tried to use also Try Catch block as proposed here : PowerShell -ErrorAction SilentlyContinue Does not work with Get-ADUser
Try {
Remove-Item INEXISTENT_FILE
}
Catch {
Write-Warning "Warning, something failed!"
}
Problem : it never goes into the catch block either (!?)
Anyone has another option to show a warning instead of an error if Remove-Item fails ?
The error produced by Remove-Item is considered 'non-terminating', which means that it is ignored by 'try/catch'. To force it to become 'visible' to 'try/catch' use the ErrorAction parameter:
Remove-Item INEXISTENT_FILE -ErrorAction Stop
Alternatively, you can change this at the script level (i.e. for all subsequent commands) like this:
$ErrorActionPreference = 'Stop'
The error message can be retrieved using $_.Exception.Message or $error[0]

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.