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

时间:2018-06-25 15:31:39

标签: powershell

我能够使用this solution将所有补丁和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到远程服务器上的文件夹中吗? 谢谢!

2 个答案:

答案 0 :(得分:0)

您要安装的MSU文件吗?如果是,则需要解压缩该文件..我猜会生成cab文件...然后您就可以创建它。

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

答案 1 :(得分:0)

$comname = Get-Content -Path ‘H:\InstallationFiles\server.txt’

$ fname = Get-ChildItem'H:\ InstallationFiles \ Patch'-递归-force |选择对象全名 设置项wsman:\ localhost \ client \ trustedhosts * -Force Foreach($ comname中的$ sname){     Foreach($ fname中的$ installpath){          $ newproc =([[WMICLASS]” \ $ sname \ root \ cimv2:win32_Process”)。create(“ wusa.exe $ installpath / extract:$ destpath”)           If($ newproc.ReturnValue -eq 0){               写主机$ _ $ newproc.ProcessID           }            其他{                写主机$ _进程创建失败,并带有$ newproc.ReturnValue            }       } }

$ fname = Get-ChildItem‘H:\ InstallationFiles \ Patch * .cab’-递归-force |选择对象全名 Foreach($ comname中的$ sname){     Foreach($ fname中的$ installpath){          $ newproc =([WMICLASS]” \ $ sname \ root \ cimv2:win32_Process”)。create(“ DISM.exe / online / Add-package / packagepath:$ destpath”)           If($ newproc.ReturnValue -eq 0){               写主机$ _ $ newproc.ProcessID           }            其他{                写主机$ _进程创建失败,并带有$ newproc.ReturnValue            }       } }

尝试一下。.但是我认为我们要在wusa.exe和dism.exe ..上添加一个等待,以等待将其关闭。.

因此,仅尝试使用一个文件即可。