1

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?

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
}

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-Process wrong...

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

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

It works, how i wanted it to be! Thats exactly what i need, thank you so much!
Glad to hear it, @ItsJustHaga; my pleasure.
0

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
}

5 Comments

I added my script. Thanks for the reminder, i forgot to add it >.<
$validate = Get-Process -Name Calculator if ($validate){ Start-Process -FilePath c:/script.ps1 }
I tested your code, but sadly the script will repeat itself, even when the processes aren't closed...
Well you don't have a good stop for your while loop since its says while($true) and $true is always $true it will never exit the while loop. Meaning it will keep running through whatever you put in there. You should try putting something in your while loop that will change. Or use a countdown to make it exit. Then use the check I gave you to go back to the while loop if needed. Here is some documentation that might help. learn.microsoft.com/en-us/powershell/module/…
I tried it like followed: Powershell $process = Get-Process mspaint.exe if ($process = $false){ Start-Process -Filepath c:/script.ps1 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.