Powershell script is not executing EXe remotely on list of servers paralelly RRS feed

  • Question

  • Hi,

    I have created a script to execute an EXE on list of remote servers parallelly. The command is getting executed without any issue, but EXE is not running on remote servers.

    Can anyone help me to correct the script ?

    Set-ExecutionPolicy unrestricted -force
    ForEach ($Computer in Get-Content C:\server.txt)
        echo "$Computer"
                        Invoke-Command -AsJob -ComputerName $Computer -ScriptBlock {& 'c:\windows\WUInstall.exe' /install /reboot}
    • Moved by Bill_Stewart Monday, October 2, 2017 9:43 PM Help vampire
    Thursday, August 17, 2017 4:37 AM

All replies

  • Within your script block, you could try using Invoke-Expression (I've used it quite a bit successfully for running remote commands)

    ForEach ($Computer in Get-Content C:\server.txt)
           echo "$Computer"
           Invoke-Command -AsJob -ComputerName $Computer -ScriptBlock {

          Invoke-Expression -Command "c:\windows\WUInstall.exe' /install /reboot"


    Alternatively, you could use a PSSession instead of ComputerName

    ForEach ($Computer in Get-Content C:\server.txt)
           echo "$Computer"

          $PSSession = New-PSSession -ComputerName $Computer
           Invoke-Command -Session $PSSession -ScriptBlock {

          Invoke-Expression -Command "c:\windows\WUInstall.exe' /install /reboot"


    Good luck!

    Thursday, August 17, 2017 5:11 AM
  • What is WUInstall?  It is not part of Windows.  You cannot run a GUI app remotely.  Many apps cannot be run remotely.  Contact the vendor of the app to find out is this can be done.


    Thursday, August 17, 2017 11:37 AM
  • WUInstall.exe is the tool to install patches by contacting the correspondent WSUS server in each region.I have edited my script like below even this is giving error

     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 WinRM already enabled on server1 ... server1

    cmdlet Get-Credential at command pipeline position 1 Supply values for the following parameters: Credential New-PSSession : [server1] Connecting to remote server server1 failed with the following error message : The WinRM client cannot process the request. CredSSP authentication is currently disabled in the client configuration. Change the client configuration and try the request again. CredSSP authentication must also be enabled in the server configuration. Also, Group Policy must be edited to allow credential delegation to the target computer. Use gpedit.msc and look at the following policy: Computer Configuration -> Administrative Templates -> System -> Credentials Delegation -> Allow Delegating Fresh Credentials. Verify that it is enabled and configured with an SPN appropriate for the target computer. For example, for a target computer name "myserver.domain.com", the SPN can be one of the following: WSMAN/myserver.domain.com or WSMAN/*.domain.com For more information, see the about_Remote_Troubleshooting Help topic. At C:\Users\sand\Desktop\pssession.ps1:34 char:20 + $PSSession = New-PSSession -ComputerName $Computer -Credential $creds -Aut ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotin gTransportException + FullyQualifiedErrorId : -2144108126,PSSessionOpenFailed Invoke-Command : Cannot validate argument on parameter 'Session'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again. At 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>

    Tuesday, August 22, 2017 4:19 PM
  • Always solve the first error in the list and retest.  Do this until you fix all errors.


    Tuesday, August 22, 2017 4:22 PM
  • Invoke-Command does not require a session.  Why use both PsExec and WinRM?

    You are making this way to complicated.  Start small and get one piece at a time working.

    WUInstall cannot be run remotely.  WU is restricted to interactive or system installs only.

    You cannot remotely set execution policy.  Use Group Policy.


    • Edited by jrv Tuesday, August 22, 2017 4:29 PM
    Tuesday, August 22, 2017 4:28 PM