1

I am fairly new to writing code in Powershell. For my job I have to write multiple Powershell scripts to make changes in the Hardware and Software settings as well as the Registry and Group Policy Editor to get these applications to run. These applications are a little older. Upgrading these software applications or the hardware then run on is NOT an option. as an example, when Microsoft releases the new patches on like Patch Tuesday...when those patches are applied there is a high probability that something will be changed which is where I come in to write a script to fix the issue. I have multiple scripts that I run. When those scripts are ran they may end up terminating because of an Error Code or an Exit Code. A large part of the time I do not that the script has failed immediately.

I am trying to figure out a script that I can run in a 2nd PowerShell Console Window. I am thinking that the only purpose of this script is to just sit there on the screen and wait and monitor. Then when I execute a script or Application (the only file extensions that I am worried about are: EXE, BAT, CMD, PS1) if the script/application that I just ran ends with an exit code or an error code....then output that to the screen...in REAL TIME.

Below, I have a small piece of code that kind of works, but it is not what I am wanting. I have researched online and read and read tons of stuff. But I just can't seem to find what I am looking for. Could someone please help me with getting a script that will do what I am wanting.

Thank you for your help!!!!

$ExitErrorCode = 
"C:\ThisFolder\ThatFolder\AnotherFolder\SomeApplication.EXE # (this 
would 
           # either be an EXE or CMD or BAT or PS1)"
$proc = Start-Process $ExitErrorCode -PassThru
$handle = $proc.Handle # cache proc.Handle
$proc.WaitForExit();

if ($proc.ExitCode -ne 0) {
    Write-Warning "$_ exited with status code $($proc.ExitCode)"
}
papa2017
  • 11
  • 2
  • It sounds like to me that you want is an already running PowerShell script to be aware of, and run, new EXE, BAT, CMD, and PS1 files. If I have this wrong, let me know. This would seem to need [communication](https://stackoverflow.com/a/36240299/4190564) between that already running script and the script that is deciding what needs to be ran. The nature of the communication would be relatively simple, run this, run that, and run this other thing, while maybe passing args. I haven't ever tried anything like that, so I don't yet have code for doing it. – Darin Sep 18 '22 at 15:55
  • [Look at using PowerShell parallel jobs](https://devblogs.microsoft.com/scripting/parallel-processing-with-jobs-in-powershell/). There are lots of docs/blogs all over the [web](https://duckduckgo.com/?q=%27powershell+job+monitor+another+script%27&t=h_&ia=web) with examples, and right here on Stackoverflow (just use the search box above) or your favorite search engine to find them. – postanote Sep 18 '22 at 22:08

1 Answers1

0

Possible duplicate of the approaches shown here:

Monitoring jobs in a PowerShell session from another PowerShell session

Monitoring jobs in a PowerShell session from another PowerShell session

PowerShell script to monitor a log and output progress to another

PowerShell script to monitor a log and output progress to another

postanote
  • 15,138
  • 2
  • 14
  • 25