0

I tried all suggestion on how to catch errors of 7Zip as explained in:

and played with try / catch.

Second contains only

Cannot find drive. A drive with the name ' 7-Zip 18.05 (x64) ' does not exist.

in Error[0]

If I write the console output

7-Zip 18.05 (x64) : Copyright (c) 1999-2018 Igor Pavlov : 2018-04-30

Scanning the drive for archives:
1 file, 51273 bytes (51 KiB)

Extracting archive: \\...\850\DAY01
--
7z.exe : ERROR: Data Error : DAY01.RAW
At C:\Users\MyUser\Code\7Zip.ps1:6 char:1
+ & $7ZIP_FullPath x $IN_FullPath -o$OUT_Directory -y
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (ERROR: Data Error : DAY01.RAW:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
 
Path = \\...\850\DAY01
Type = gzip
Headers Size = 20


Sub items Errors: 1

Archives with Errors: 1

Sub items Errors: 1

in a variable, the variable will contain only

7-Zip 18.05 (x64) : Copyright (c) 1999-2018 Igor Pavlov : 2018-04-30

Scanning the drive for archives:
1 file, 51273 bytes (51 KiB)

Extracting archive: \\...\850\DAY01
--    
Path = \\...\850\DAY01
Type = gzip
Headers Size = 20


Sub items Errors: 1

Archives with Errors: 1

Sub items Errors: 1

It looks like, that

7z.exe : ERROR: Data Error : DAY01.RAW
    At C:\Users\MyUser\Code\7Zip.ps1:6 char:1
    + & $7ZIP_FullPath x $IN_FullPath -o$OUT_Directory -y
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (ERROR: Data Error : DAY01.RAW:String) [], RemoteException
        + FullyQualifiedErrorId : NativeCommandError

is created by PowerShell (what explaiuns 7z.exe : ERROR:..) and can only be captured if I use $out:

& $7ZIP_FullPath x $IN_FullPath -o$OUT_Directory -y > $out

It looks like $out triggers something but what?

Stefan
  • 332
  • 1
  • 15
  • `> $out` sends the output, which would have been written to the console, to a file with the name given by the variable `$out`. I'm guessing you aren't setting `$out` so this would be effectively be `> $nul`. – Mark Elvers Feb 24 '21 at 14:54
  • 1
    You could try `$out = & $7ZIP_FullPath x $IN_FullPath -o$OUT_Directory -y *>&1` – Daniel Feb 24 '21 at 15:39
  • `$out` is not defined but I can use it when I do `write-host $out` afterwards. Wondering how to capture the error properly. Moreover, it looks like that PowerShell IDE and VS Code behave differently – Stefan Feb 24 '21 at 15:40
  • @Daniel `$7ZIPExtractResult= & $7ZIP_FullPath x $IN_FullPath -o$OUT_Directory -y *>&1` but not clue why. In addition `$7ZIPExtractResult` is `System.Object[]`, so I have to do `7ZIPExtractResultAsString` but there is no error thrown if I use `try` and `catch` to I have to check the returning string, why ever??? – Stefan Feb 24 '21 at 16:20

1 Answers1

0

@daniel comments got me on the right track but still, I can not explain fully why only this solution is working.

$7ZIPExtractResult= & $7ZIP_FullPath x $IN_FullPath -o$OUT_Directory -y *>&1

Redirects the specified stream to the Success stream.

My guess is, that only that way it can be stored in a variable. I found help in What does 2>&1 Mean in PowerShell - Stack Overflow and about_Redirection - PowerShell | Microsoft Docs.

In addition, the variable $7ZIPExtractResult is a System.Object[], so I have to do convert an array to a string doing 7ZIPExtractResultAsString = "$7ZIPExtractResult", expecting that is always possible and not throwing a different error.

It is still unknown to me why this error is not caught by

try {
}
catch {
}
Stefan
  • 332
  • 1
  • 15