Powershell 脚本 - Start-Job 和 MSIEXEC

Powershell script - Start-Job & MSIEXEC

不知道你能帮忙吗?我有写Powershell脚本执行MSI脚本的需求

我还需要在此过程中设置超时(因为我们获得的 MSI 有时会挂起)。

我看到您可以通过使用 Start-Job/Wait-Job 进程来实现此目的

显然下面的代码目前处于严重的屠杀状态

提前致谢

    $timeoutSeconds = 20

$uninstall32    = gci "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" | foreach { gp $_.PSPath } | ? { $_ -match "My File" } | select UninstallString$uninstall64    = gci "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" | foreach { gp $_.PSPath } | ? { $_ -match "Vix.Cbe.SalesTransaction" } | select UninstallString
Echo  "uninstall32 :" $uninstall32

if ($uninstall32) {
$uninstall32 = $uninstall32.UninstallString -Replace "msiexec.exe","" -Replace "/I","" -Replace "/X",""
$uninstall32 = $uninstall32.Trim()
p = @("/X", "$uninstall32", "/b")
}
Echo  "uninstall32 :" $uninstall32
Echo  "u32 :" p


j = Start-Job msiexec  -ArgumentList p

if (Wait-Job j -Timeout $timeoutSeconds) { Receive-Process j }
Remove-Process -force j

您不必为此而打乱工作。这是简单的方法:

start Notepad -PassThru | select -ExpandProperty id | set id
sleep 60
kill $id -ea 0

如果应用生成另一个应用并退出,这可能不起作用,因为 Id 是错误的。在那种情况下,您可能必须在进程列表中或通过 cmd 行参数搜索它。

感谢 majkinetor,我成功地修改了代码以实现我的目标。

唯一的问题是,显然该进程是否仍在主动卸载,它在 TOSecs 值之后被杀死。

这应该足以满足我的需要。

因此,为寻找类似解决方案的其他人解释一下:

此过程会检查 32 位和 64 位注册表项以查找类似于 ServiceName 的 MSI(Urbancode Deploy 参数是在 运行 时传递给脚本的“${p:ServiceName}”)

如果找到条目,它将执行特定 32/64 MSI 的卸载代码

/x = 卸载

$uninstall64/32 = MSI 卸载部分的 GUID

/nq = 没有 GUI 的安静卸载(事实上在隔离测试中你会得到一个 Yes/No 对话)

卸载将 运行 您在 $TOSecs

中设置的秒数

希望这对其他人有帮助

$TOSecs      = 30
$uninstall32 = gci "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" | foreach { gp $_.PSPath } | ? { $_ -match "'${p:ServiceName}'" } | select UninstallString
$uninstall64 = gci "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" | foreach { gp $_.PSPath } | ? { $_ -match "'${p:ServiceName}'" } | select UninstallString

if ($uninstall64) {
$uninstall64 = $uninstall64.UninstallString -Replace "msiexec.exe","" -Replace "/I","" -Replace "/X",""
$uninstall64 = $uninstall64.Trim()
Write "Uninstalling 64bit " '${p:ServiceName}'

start-process "msiexec.exe" -arg "/X $uninstall64 /nq" -PassThru | 
select -ExpandProperty id | set id
#Echo "proc id = "$id
sleep $TOSecs
kill $id -ea 0
}

if ($uninstall32) {
$uninstall32 = $uninstall32.UninstallString -Replace "msiexec.exe","" -Replace "/I","" -Replace "/X",""
$uninstall32 = $uninstall32.Trim()
Write "Uninstalling 32bit " '${p:ServiceName}'

start-process "msiexec.exe" -arg "/X $uninstall32 /nq" -PassThru | 
select -ExpandProperty id | set id
#Echo "proc id = "$id
sleep $TOSecs
kill $id -ea 0
}