3

I need to test some archives in a Powerhsell script as I am automating the compression of a bunch of backup files with 7z.

Now I know that normally when a program runs it returns 0 if everything completed normally; but will return some other value if it did not complete normally.

For example we see here in Scheduled Tasks that the Last Result displays 0x0 when the task completes successfully, and other values if it did not.

Scheduled Tasks window with the Last Result column displaying 0x0 at successful tasks and 0x5 for non successful tasks (though it could be another value for unsuccessful tasks)

When I use Invoke-Expression in Powershell to run 7z to test the archive, I need to be able to pull the program return value to make sure that the archive tested successfully; is there any way to get this value from Invoke-Expression?

I'm calling the following command from Invoke-Expression:

7z t C:\backups\somezip.7z *.* -r

Also what is the value I'm talking about called? I know that if you write a program in C++ you return the value from the main method, but I'm not certain what it is called.

leeand00
  • 25,510
  • 39
  • 140
  • 297
  • 1
    Can I ask why you might be using Invoke-Expression? Do you know for sure that you need it? The following might also help you: [http://stackoverflow.com/questions/12320275/how-to-pipe-output-of-invoke-expression-to-string](http://stackoverflow.com/questions/12320275/how-to-pipe-output-of-invoke-expression-to-string) – FLGMwt Apr 25 '14 at 13:42
  • @FLGMwt What's a good reason for using `Invoke-Expression`? Spaces in the path within the called command? – leeand00 Apr 25 '14 at 13:48
  • @FLGMwt I'm not interested in the output, I'm interested in the result. – leeand00 Apr 25 '14 at 14:01

1 Answers1

3

You can enclose the paths in speechmarks ie

& "C:\Program Files\7-Zip\7z" t "C:\backups\somezip.7z" *.* -r

and the value you are looking for is called $LASTEXITCODE.

Raf
  • 9,681
  • 1
  • 29
  • 41