首页 文章

Powershell脚本,用于并行执行远程服务器上的EXE

提问于
浏览
-1

我编写了一个脚本来并行执行远程服务器上的exe . 但是当exe执行时,我收到了一个错误 . 任何人都可以帮我纠正我的剧本吗?

ForEach ($Computer in Get-Content C:\servers.txt)
{

#To enable winrm if not already
$result = winrm id -r:$computer 2> $null
    if ($lastExitCode -eq 0) {
    Write-Host "WinRM already enabled on" $computer "..." -ForegroundColor green
    } else {
    Write-Host "Enabling WinRM on" $computer "..." -ForegroundColor red 
     \\slcmpx\c$\windows\system32\PsExec.exe \\$computer -accepteula -s C:\Windows\System32\winrm.cmd qc -quiet 

    if ($LastExitCode -eq 0) {
        \\slcmpx\c$\windows\system32\PsService.exe \\$computer restart WinRM 
        $result  = winrm id -r:$computer 2>$null

    if ($LastExitCode -eq 0) {Write-Host "WinRM successfully enabled!" -ForegroundColor green}
        else {Write-Host "WinRM not enabled!" -ForegroundColor red}

       } #end of if

    } #end of else  

       echo "$Computer"
      Invoke-Command -ComputerName $Computer -ScriptBlock {Set-ExecutionPolicy unrestricted -force}

      $creds = Get-Credential
      $PSSession = New-PSSession -ComputerName $Computer -Credential $creds -Authentication Credssp
      Invoke-Command -Session $PSSession -ScriptBlock {Invoke-Expression -Command 'c:\windows\WUInstall.exe /install /reboot'}                             
}

错误:

PS C:\ Users \ sand \ Desktop> . \ pssession.ps1已在server1 ... server1上启用WinRM

命令管道位置1的cmdlet Get-Credential以下参数的提供值:Credential New-PSSession:[server1]连接到远程服务器server1失败,并显示以下错误消息:WinRM客户端无法处理请求 . CredSSP身份验证当前在客户端配置中已禁用 . 更改客户端配置并再次尝试请求 . 还必须在服务器配置中启用CredSSP身份验证 . 此外,必须编辑组策略以允许将凭据委派给目标计算机 . 使用gpedit.msc并查看以下策略:计算机配置 - >管理模板 - >系统 - >凭据委派 - >允许委派新凭证 . 验证是否已启用并配置了适用于目标计算机的SPN . 例如,对于目标计算机名称“myserver.domain.com”,SPN可以是以下之一:WSMAN / myserver.domain.com或WSMAN / * .domain.com有关详细信息,请参阅about_Remote_Troubleshooting帮助主题 . 在C:\ Users \ sand \ Desktop \ pssession.ps1:34 char:20 $ PSSession = New-PSSession -ComputerName $ Computer -Credential $ creds -Aut ... ~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ OpenError:(System.Manageme .... RemoteRunspace:RemoteRunspace)[New-PSSession],PSRemotin gTransportException FullyQualifiedErrorId:-2144108126,PSSessionOpenFailed Invoke-Command:无法验证参数'Session'的参数 . 参数为null或空 . 提供非null或空的参数,然后再次尝试该命令 . 在C:\ Users \ sand \ Desktop \ pssession.ps1:35 char:32 Invoke-Command -Session $ PSSession -ScriptBlock {Invoke-Expression -Comma ... ~~~~~~~~~~ CategoryInfo:InvalidData: (:) [Invoke-Command],ParameterBindingValidationException FullyQualifiedErrorId:ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeCommandCommand

PS C:\ Users \ sand \ Desktop>

Note: WUInstall.exe 是通过联系每个地区的通信WSUS服务器来安装补丁的工具

1 回答

  • 0

    尝试在远程服务器上本地运行此命令或通过连接远程服务器的PSsesion . 如果能够成功执行此方法,请确保在使用PSsession远程服务器的上述脚本中使用相同的凭据 . 例如

    New-PSSession -ComputerName $ Computer -cred

相关问题