问题描述
您好,我无法在远程计算机上的 PowerShell 中安装 file.msi
。我可以将 file.msi
复制到远程计算机,但无法执行此作业。这种单行模式仅在 PowerShell 像管理员一样运行时才有效:
Invoke-Command -Credential(Get-Credential) -ComputerName $computer -ScriptBlock {
start-process powershell -Verb runAs {msiexec.exe '/i' "C:\Users\file.msi",'/passive'}
}
解决方法
以下方法应该可以用作脚本块,您需要为您的包找到一种方法来执行 $logcheck
,无论是事件日志还是文件。此示例已卸载 MS Office。
请注意,如果您打算在大量工作站上/定期运行它,我建议将 while($true)
替换为有限循环或游戏中时光倒流。
我也按原样放置了您的执行脚本,但对我来说 msiexec.exe 无需执行 PowerShell 即可工作
$scriptUninstallApp = {
Start-Process powershell -Verb runAs {msiexec.exe '/i' "C:\Users\file.msi",'/passive'}
$logcheck = ""
while($true)
{
if($logcheck -match "Windows Installer removed the product")
{
return
}
else
{
start-sleep -Seconds 1
[string]$logcheck = get-eventlog -logname application -newest 10 -Source msiinstaller | ?{$_.message -like "*Windows Installer removed the product. Product Name: Microsoft Office*"} | select -ExpandProperty message
}
}
}
Invoke-Command -Credential(Get-Credential) -ComputerName $computer -ScriptBlock $scriptUninstallApp