微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 powershell 在远程主机上安装 MSI

如何解决使用 powershell 在远程主机上安装 MSI

您好,我无法在远程计算机上的 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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。