1
votes

If I running this function (with out try..catch):

executezipFullBackup -PathFileLocation "Z:\" -PathSaveBackup "E:\" -NameOfBackup "MyNASStorage"

PoweShell give me this error:

sz : 
At C:\PowerShellF\zipFunction.ps1:6 char:9
+         sz a -t7z "$PathSaveBackup\$NameOfBackup" "$PathFileLocation"  #backup
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

ERROR:
Duplicate filename on disk:
BackupTest\private\103.tmp\usr\bin\mail
BackupTest\private\103.tmp\usr\bin\Mail

And that error is OK for me, because I know what is wrong on this step.

But my problem is, if I put this script in try..catch, I dont get this error. Example:

$ErrorActionPreference = "Stop";      #this is for Try Catch
try {
    zipFullBackup -PathFileLocation "$PathFileLocation" -PathSaveBackup "$PathSaveBackup" -NameOfBackup "$NameOfBackup"          
} catch {
    $MyError = ($Error[0] | Out-String);
    Write-Host "$MyError"
}

I get error like this:

sz : 
At C:\PowerShellF\zipFunction.ps1:6 char:9
+         sz a -t7z "$PathSaveBackup\$NameOfBackup" "$PathFileLocation" #backup
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

This error is not enough for me, because I don't see ERROR! I don't see this:

ERROR:
Duplicate filename on disk:
BackupTest\private\103.tmp\usr\bin\mail
BackupTest\private\103.tmp\usr\bin\Mail

How can I get try..catch to display this error?

1
what is "executezipFullBackup"? Is this an exe, a batch file, other? - Kory Gill

1 Answers

0
votes

That error message is not part of a PowerShell error/exception, but a message 7zip itself prints to STDOUT. Therefore it can't be caught with a try..catch block, not even if you set $ErrorActionPreference = 'Stop'.

What you can do is collect the command output in a variable, though:

$msg = zipFullBackup ...