最后一个进程关闭后重新启动 PowerShell 脚本

问题描述 投票:0回答:3

我写了一个脚本,它打开 7 个程序大约 10 次(是的,这是一个恶作剧脚本)。

我的问题是,有没有办法观察最后一个进程是否关闭,如果是,则再次重新启动整个脚本?

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
}

我的脚本现在如下所示:

$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
}

我确实测试过这个,但它又重新开始...我认为我使用

Get-Process
错了...

还有其他方法观察进程是否关闭吗?

powershell
3个回答
1
投票

如果可以接受在相同的、无限期运行的脚本中处理重新启动:

# 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.
}

然后您可以通过

Start-Process
在后台隐形启动脚本;例如:

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

警告:要停止操作,您必须找到隐藏的 PowerShell 进程并终止它。如果添加

-PassThru
,您将获得一个代表隐藏进程的进程信息对象。


如果您希望能够正常调用脚本本身,并让它生成一个隐藏的后台进程来监视启动的进程,然后重新调用脚本(不可见),则需要做更多的工作:

# 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\"'
"@

警告:要停止操作,您必须找到隐藏的 PowerShell 进程并终止它。


0
投票

在没有看到你的实际脚本的情况下,你可以使用类似的东西

$validate  = Get-Process -Name pwsh 

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

0
投票

我喜欢这个脚本,我搜索的解决方案不是 1 到 10,而是用户名。 我喜欢为每个用户启动一个浏览器,关闭浏览器后我想打开下一个用户。毕竟用户我想重新开始。

到目前为止,我的脚本似乎有效,但用户似乎是“随机”选择的,我相信当边缘仍然处于活动状态并等待用户结束时,集合中的项目不会被搁置。

while ($true) {
$collection = "aap","noot","mies"
foreach ($item in $collection) {
    $Procs = Get-Process msedge | ? {$_.SI -eq (Get-Process -PID $PID).SessionId} -ErrorAction SilentlyContinue
    if (($procs).Count -eq 0) {
        Start-Process -FilePath msedge https://outlook.office.com/mail/$item/junkemail
Write-Host $item
    }
} 
}
© www.soinside.com 2019 - 2024. All rights reserved.