Powershell 将所有 exe 和 msu 安装在远程服务器上的文件夹中

Posted

技术标签:

【中文标题】Powershell 将所有 exe 和 msu 安装在远程服务器上的文件夹中【英文标题】:Powershell to Install all exe and msu in a folder on remote server 【发布时间】:2018-06-25 15:31:38 【问题描述】:

我能够将所有 Patch 和 exe 从一台服务器复制到具有 this solution 的服务器列表 现在我正在尝试在远程服务器上的目标文件夹中远程安装所有补丁和 exe。 使用

$comname = Get-Content -Path ‘H:\InstallationFiles\server.txt’
$fname = Get-ChildItem ‘H:\InstallationFiles\Patch’ -Recurse -force | select-object FullName
Set-Item wsman:\localhost\client\trustedhosts * -Force
Foreach($sname in $comname)
    Foreach($installpath in $fname)
         $newproc=([WMICLASS]”\\$_\root\cimv2:win32_Process”).Create(“$installpath /s”)
          If($newproc.ReturnValue -eq 0)
              Write-Host $_ $newproc.ProcessID
          
           Else 
               Write-Host $_ Process Create failed with $newproc.ReturnValue
           
      

但我收到以下错误

Process Create failed with
Cannot convert value “\\\root\cimv2:win32_Process” to type “System.Management.ManagementClass”. Error: “Invalid parameter “
At H:/InstallationFiles/installfiletoserver.ps1:15 char:9
+            $newproc=([WMICLASS]”\\$_\root\cimv2:win32_Process”).Create(“$installFil ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo : InvalidArgument: (:) [], Runtime.Exception
          + FullyQualfiedErrorId : InvalidCastToWMIClass

即使我尝试使用 Invoke-command,但也失败了。

Invoke-command -ComputerName $sname -ScriptBlock 
       Start-Process $installpath -ArgumentList ‘/silent’ -wait

调用命令失败

[servername] Connection to remote server servername  failed with the following error message : The WinRM client cannot process the request because the server name cannot be resolved l. For more information, see the about_Remote_Troubleshooting Help topic.
+ CategoryInfo : OpenError: (servername:String) [], PSRemotingTransportException
+FullyQualifiedErrorId : ComputerNotFound, PSSessionStateBroken

我在所有服务器上都收到此错误,甚至在服务器上使用 Invoke-Command 上的脚本块运行其他命令(如 new-item 等)。 我不确定我错过了什么。有人可以帮我使用 Powershell 远程安装远程服务器上的文件夹中的所有 exe 和 msu 吗? 谢谢!

【问题讨论】:

$_ 是一个特殊值,仅在您的情况下有时才可用我相信您想要$sname @EBGreen 感谢您的回复。现在错误消失了,但我得到所有服务器的返回代码 9“Process Create failed with 9”。 错误代码 9 是找不到路径错误。 @EBGreen 是的,能够找到问题,它是“select-object FullName”,它返回“@FullName=H:/InstallationFiles/Patch/filename.exe”关于如何解决的任何想法解决这个问题? Select-Object -ExpandProperty FullName 【参考方案1】:

是您尝试安装的 MSU 文件吗?如果是的话,你需要解压文件..我猜会生成cab文件......而且你可以制作它。

$newproc=([WMICLASS]”\\$_\root\cimv2:win32_Process”).Create(“DISM.exe /Online /Add-Package /PackagePath:$installpath ”)

【讨论】:

我试过这个答案,它运行成功,但服务器上没有安装msu。你能帮忙修一下吗? 哼!!我的坏人,你在哪里找到“...\\$_\root\cimv2....”切换到“...\\$sna​​me\root\cimv2...” 我一直在尝试 \\$sna​​me\root...,但我仍然看到为每个 exe 和 msu 生成进程 ID,但在目标服务器上未安装 msu。 第一次你需要解压msu文件...第二次要生成CAB文件...第三次使用这个cab文件 是的,我正在提取并运行 cab,但没有任何效果,但是当我直接在目标服务器上尝试时,它可以工作! “$newproc=([WMICLASS]”\\$sna​​me\root\cimv2:win32_Process”).create(“wusa.exe $installpath /extract:$destpath”)” 然后“$newproc1=([WMICLASS])”\ \$sna​​me\root\cimv2:win32_Process”).create(“DISM.exe /online /Add-package /packagepath:$destpath “)”【参考方案2】:
$comname = Get-Content -Path ‘H:\InstallationFiles\server.txt’

$fname = Get-ChildItem ‘H:\InstallationFiles\Patch’ -Recurse -force |选择对象全名 设置项 wsman:\localhost\client\trustedhosts * -Force Foreach($sname in $comname) Foreach($fname 中的 $installpath) $newproc=([WMICLASS]”\$sna​​me\root\cimv2:win32_Process”).create(“wusa.exe $installpath /extract:$destpath”) 如果($newproc.ReturnValue -eq 0) 写主机 $_ $newproc.ProcessID 别的 写入主机 $_ 进程创建失败并出现 $newproc.ReturnValue

$fname = Get-ChildItem ‘H:\InstallationFiles\Patch*.cab’ -Recurse -force |选择对象全名 Foreach($sname in $comname) Foreach($fname 中的 $installpath) $newproc=([WMICLASS]”\$sna​​me\root\cimv2:win32_Process”).create(“DISM.exe /online /Add-package /packagepath:$destpath”) 如果($newproc.ReturnValue -eq 0) 写主机 $_ $newproc.ProcessID 别的 写入主机 $_ 进程创建失败并出现 $newproc.ReturnValue

试试这个.. 但我认为我们要在 wusa.exe 和 dism.exe 上添加一个等待.. 等待关闭它..

所以尝试只使用一个文件.. 开始...

【讨论】:

我只是在屏幕上打印了返回码,但我没有看到解压缩到目标服务器上的 cab 文件的 msu,也没有看到安装的补丁。 ☹️ $newproc1=([WMICLASS]”\\$sna​​me\root\cimv2:win32_process”).create(“DISM.exe /online /Add-package /Packagepath:$destinpath”)跨度> $newproc=([WMICLASS]”\\$sna​​me\root\cimvs:win32_process”).create(“wusa.exe $installpath /extract:$destinpath”) 我只是把进程ID打印在屏幕上!没有返回码! 嘿... 你在哪里找到 dism.exe... 在你的代码切换到这个... dism.exe /online /add-package /packagepath:$destinpath /ignorecheck /quiet /norestart在上面的这段代码中......

以上是关于Powershell 将所有 exe 和 msu 安装在远程服务器上的文件夹中的主要内容,如果未能解决你的问题,请参考以下文章

windows系统中安静的安装补丁

如何使用脚本块和参数将 powershell.exe 与 -Command 一起使用

PowerShell 将powershell脚本转换成exe

从CMD管道到PowerShell

从 powershell.exe 将参数传递给脚本

如何使用 cmd / powershell 对文件夹中的所有程序执行 taskkill