首页 > 解决方案 > 使用 powershell 在远程主机上安装 MSI

问题描述

您好,我无法file.msi在远程计算机上的 PowerShell 中安装。我可以复制file.msi到远程计算机,但这项工作没有执行。仅当 PowerShell 像管理员一样运行时,此单行程序才有效:

Invoke-Command -Credential(Get-Credential) -ComputerName $computer -ScriptBlock {
   Start-Process powershell -Verb runAs {msiexec.exe '/i' "C:\Users\file.msi", '/passive'}
}

标签: powershellwindows-installer

解决方案


以下方法应该可以用作脚本块,您需要找到一种方法让您的包执行$logcheck事件日志或文件。此示例有 MS Office 未安装。

请注意,如果您打算/定期在大量工作站上运行它,我建议您while($true)用有限的 for 循环或延时摄影替换。

我还按原样放置了您的执行脚本,但对我来说 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

推荐阅读