'Restarting a PowerShell script after the last process is closed

I wrote a script, which opens 7 programs approximately 10 times (yes its a prankscript).

My question is, is there a way to observe, if the last process is closed and if so, restarting the whole script again?

Thanks for the help.

while ($start -le 10){
  Start-Process mspaint.exe
  Start-Process notepad.exe
  Start-Process write.exe
  Start-Process cmd.exe
  Start-Process explorer.exe
  Start-Process control.exe
  Start-Process calc.exe
  $start =+ 1
}

Edit 1:

My script now looks like following:

$start; $process

PowerShell.exe -windowstyle hidden { script.ps1 }

while ($start -le 10){
Start-Process mspaint.exe
Start-Process notepad.exe
Start-Process write.exe
Start-Process cmd.exe
Start-Process explorer.exe
Start-Process control.exe
Start-Process calc.exe
$start =+ 1
}

$process = Get-Process mspaint.exe

if ($process = $false){
Start-Process -FilePath c:/script.ps1
}

I did test this, but it starts all over again... I think that i use Get-Processwrong...

Is there another way to observe, if the process is closed or not?



Solution 1:[1]

If it's acceptable to handle the re-launching inside the same, indefinitely running script:

# Note: This runs indefinitely.
# If run in the foreground, you can use Ctrl-C to stop.
while ($true) {
  1..10 | ForEach-Object {
    # Launch all processes and pass information 
    # about them through (-PassThru)
    'mspaint.exe',
    'notepad.exe',
    'write.exe',
    'cmd.exe',
    'explorer.exe',
    'control.exe',
    'calc.exe' | ForEach-Object {
        Start-Process -PassThru $_
      }
  } | Wait-Process # Wait for all processes to terminate.
  # Continue the loop, which launches the programs again.
}

You could then launch the script invisibly in the background, via Start-Process; e.g.:

Start-Process -WindowStyle Hidden powershell.exe '-File c:\script.ps1'

Caveat: To stop the operation, you'll have to locate the hidden PowerShell process and terminate it. If you add -PassThru, you'll get a process-information object representing the hidden process back.


More work is needed if you want to be able to call the script itself normally, and let it spawn a hidden background process that monitors the launched processes and then reinvokes the script (invisibly):

# Launch all processes 10 times and
# collect the new processes' IDs (PIDs)
$allPids = (
  1..10 | ForEach-Object {
    'mspaint.exe',
    'notepad.exe',
    'write.exe',
    'cmd.exe',
    'explorer.exe',
    'control.exe',
    'calc.exe' | ForEach-Object {
        Start-Process -PassThru $_
    }
  }
).Id

# Launch a hidden PowerShell instance
# in the background that waits for all launched processes
# to terminate and then invisibly reinvokes this script:
Start-Process -WindowStyle Hidden powershell.exe @"
-Command Wait-Process -Id $($allPids -join ',');
Start-Process -WindowStyle Hidden powershell.exe '-File \"$PSCommandPath\"'
"@

Caveat: To stop the operation, you'll have to locate the hidden PowerShell process and terminate it.

Solution 2:[2]

Without seeing your actual script you can use something along the lines of

$validate  = Get-Process -Name pwsh 

if ($validate){
Start-Process -FilePath c:/script.ps1
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 sam.solo.works