1

I'm trying to wrap a piece of code into a WPF application so that a user can just hit the button and the code will run. However, because the script can take time to run, I would like to give a status to the end user so that they know it finished. Is there a way to do that with powershell and this style of code posted below?

1..255 | %{
    $I = "192.168.2.$_"
    Get-MacAddress($I);
    function Get-MacAddress {
        ...
    }
}
Jason
  • 811
  • 1
  • 12
  • 26

1 Answers1

1

TL;DR: You can send an exit 1 to exit with a return code of 1 (or any number other than 0), which means it failed for some reason.

Normally (and under specific circumstances) PowerShell scripts exit with a code of 0. Most other applications return an exit code of 0 whenever they are 'successfully completed,' and without any sort of exception or error that the program is consciously aware occurred.

If you're trapping the exit code in the WPF, you could report whether it was successful (exit 0 inserted at some point in your code, or let it finish as expected), or if it failed (exit of any other number) - at which point you would want to consider reporting unique exit codes specific to the reasons that occurred wrong.

Consider also looking into try, catch, and throw as they're quite useful, as well.

EDIT: As a final note, take a good look at how %errorLevel% (where the exit code is stored) is handled under some unique situations. %errorLevel% is what you want to focus on, if you use exit codes.

Community
  • 1
  • 1
gravity
  • 2,175
  • 2
  • 26
  • 34