9

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 ?

Pac0
  • 21,465
  • 8
  • 65
  • 74

1 Answers1

23

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]

Pac0
  • 21,465
  • 8
  • 65
  • 74
boxdog
  • 7,894
  • 2
  • 18
  • 27
  • It works indeed ! The ErrorVariable is not working, though, do you know if I can get the actual error emssage ? – Pac0 Sep 26 '19 at 14:25
  • 1
    Do you mean you want the error message once inside the `catch` block? The exception details are stored in the `$_` variable in that case. You could try adding to your warning like this: `Write-Warning "Warning, something failed: $($_.Exception.Message)"` – boxdog Sep 26 '19 at 14:30
  • Thank you, indeed. I also incidentally checked that I can get them from the predefined `$error[0]` as well. – Pac0 Sep 26 '19 at 14:32