隐藏的屏幕睡觉

时间:2016-12-29 16:44:43

标签: powershell

我试图停止一个进程然后再睡10秒钟,杀掉下一个进程再睡10秒钟,然后开始另一个进程。问题是一切都在运行。所以我想要开始的过程不会运行,因为其他人还没有停止。

Start-Process Powershell.exe -windowstyle Minimized { Stop-Process -processname vpnagent -Force }
Start-Process powershell.exe -windowstyle Minimized { start-sleep -s 10 }
Start-Process Powershell.exe -windowstyle Minimized { Stop-Process -processname vpnui -force }
Start-Process powershell.exe -windowstyle Minimized { start-sleep -s 10 }
Start-Process -filepath "C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpncli.exe" -ArgumentList 'connect company.domain.com' -WindowStyle Minimized

2 个答案:

答案 0 :(得分:3)

提出这个更简单的代码并解释我的意思的答案:

Stop-Process -ProcessName vpnagent -Force
Start-Sleep -Seconds 10

Stop-Process -ProcessName vpnui -Force
Start-Sleep -Seconds 10

# (to make line shorter)
$path = "C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpncli.exe"
$args = "connect company.domain.com"

Start-Process -FilePath $path -ArgumentList $args -WindowStyle Minimized

编辑:这似乎可以做你想要的,但你会看到第一个powershell窗口:

Start-Process powershell.exe -ArgumentList "-File path/to/script.ps1" -WindowStyle Minimized

答案 1 :(得分:1)

这是一个可能的解决方案。

PowerShell脚本:

function StopProcess {
  param(
    $processName
  )
  $ErrorActionPreference = "SilentlyContinue"
  if ( -not (Get-Process $processName) ) {
    return
  }
  Stop-Process $processName -Force
  while ( $true ) {
    if ( -not (Get-Process $processName) ) {
      break
    }
    Start-Sleep 5
  }
}

StopProcess vpnagent
StopProcess vpnui

$app = Join-Path ${Env:ProgramFiles(x86)} `
  "Cisco\Cisco AnyConnect Secure Mobility Client\vpncli.exe"
& $app "connect vpn.fabrikam.com"

使用我编写的名为ExecGUI.exe(http://www.westmesatech.com/misctools.html)的简短可执行文件运行PowerShell脚本:

ExecGUI -s 7 -- powershell.exe -File c:\pathtoscript\script.ps1

ExecGUI.exe本身就是一个GUI应用程序,不显示控制台窗口,因此您可以使用它在最小化窗口(-s 7)中执行powershell.exe。

相关问题