Powershell:查找 运行 进程,如果进程是 运行 则中止脚本,如果不是则继续脚本

Powershell: Find running process, if process is running abort the script, if not then continue with the script

希望使用 SCCM 在 100 多台 PC 上更新 Putty。

Putty 不太擅长覆盖,因此我创建了一个 Powershell 脚本来删除任何旧版本,然后安装新版本。

我想在开头添加一两行来检查用户是否已经打开了 Putty。如果 putty 是打开的,脚本应该 cancel/abort。如果不是 运行 脚本应该继续并卸载旧版本并安装新版本。

有什么想法吗?

您可以使用 PSAppDeployToolkit 来实现这一点。取决于您希望如何将其部署到您的用户。您可以轻松地将其配置为等待程序关闭、自动关闭或提示用户关闭进程。

或者,如果您只想让脚本等到 Putty 关闭,您可以只使用一个简单的循环来等待进程关闭。

 Do {
        $status = Get-Process "putty" -ErrorAction SilentlyContinue
        If (($status)) { Write-Host 'Waiting for process to close...' ; Start-Sleep -Seconds 3 }
        Else { Write-Host 'Process has closed' ; $closed = $true }
    }
 Until ( $closed )

终于成功了。感谢 Austen Ewald。

如果进程 (Putty) 是 运行,这将退出脚本,如果不是,则删除旧的并安装新的。我将简单地 "monitor" 在 SCCM 集合中查找早于 0.73 的 Putty 版本的结果。

Do {
        $status = Get-Process "putty*" -ErrorAction SilentlyContinue
        If (($status)) { Write-Host 'Waiting for process to close...' ; exit }
        Else { Write-Host 'Process has closed' ; $closed = $true }
    }
 Until ( $closed )

$uninstall32 = gci "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" | foreach { gp $_.PSPath } | ? { $_ -match "Putty*" } | select UninstallString
$uninstall64 = gci "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" | foreach { gp $_.PSPath } | ? { $_ -match "Putty*" } | select UninstallString
if ($uninstall64) {
$uninstall64 = $uninstall64.UninstallString -Replace "msiexec.exe","" -Replace "/I","" -Replace "/X",""
$uninstall64 = $uninstall64.Trim()
Write "Uninstalling..."
start-process msiexec.exe -arg "/X $uninstall64 /quiet /norestart" -Wait}
if ($uninstall32) {
$uninstall32 = $uninstall32.UninstallString -Replace "msiexec.exe","" -Replace "/I","" -Replace "/X",""
$uninstall32 = $uninstall32.Trim()
Write "Uninstalling..."
start-process msiexec.exe -arg "/X $uninstall32 /quiet /norestart" -Wait}
    Start-Sleep -s 10
msiexec.exe /i .\putty-64bit-0.73-installer.msi /quiet /norestart