首页 > 解决方案 > Powershell 将所有 exe 和 msu 安装在远程服务器上的文件夹中

问题描述

我能够使用此解决方案将所有补丁和 exe 从一台服务器复制到服务器列表 现在我正在尝试将所有补丁和 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 安装在远程服务器上的文件夹中吗?谢谢!

标签: powershell

解决方案


Is the MSU file that you are trying to install? if yes, you need to decompress the file.. will generate the cab file I guess... and than you can make it.

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

推荐阅读