Powershell脚本 - Start-Job& MSIEXEC

时间:2016-04-01 11:16:57

标签: powershell timeout msiexec start-job

我想知道你能帮忙吗?我需要编写一个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()
$32p = @("/X", "$uninstall32", "/b")
}
Echo  "uninstall32 :" $uninstall32
Echo  "u32 :" $32p


$32j = Start-Job msiexec  -ArgumentList $32p

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

2 个答案:

答案 0 :(得分:0)

你不必乱用工作来做那件事。这是一个简单的方法:

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

如果应用程序生成另一个应用程序并退出,则ID可能无效。在这种情况下,您可能需要在进程列表中或通过cmd line params搜索它。

答案 1 :(得分:0)

感谢majkinetor,我设法改变了代码,以实现我的目标。

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

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

为了解释寻找类似解决方案的其他人:

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

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

/ x =卸载

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

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

卸载将运行您在$ 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
}
相关问题