Home > database >  Restarting a PowerShell script after the last process is closed
Restarting a PowerShell script after the last process is closed

Time:04-08

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
}

CodePudding user response:

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
}

CodePudding user response:

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

# Note: This runs indefinitely. 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.
}
  • Related