0

I wrote a script to execute an exe on list of remote servers, but script is running. but I can't see the exe executing on remote machines.

can anyone help me to correct below script.

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

        echo "$Computer"
        $command = "c:\windows\WUInstall.exe /install /reboot"
        Invoke-Command -ComputerName $Computer -ScriptBlock {$command}

}
Sandeep
  • 137
  • 11
  • Have you configured PSremoting? have you checked by running any small command that it is returning proper value(s) or not? Are you sure that you do not need any credentials to pass . This indicates your logged on user has the capacity and the access to fetch information from all the systems mentioned in the file.have you checked the execution policy? – Ranadip Dutta Aug 16 '17 at 15:40
  • 3
    `$command` is a local variable, try `-ScriptBlock {$using:command}` –  Aug 16 '17 at 15:40
  • 2
    or directly put the command inside the scriptblock. – Ranadip Dutta Aug 16 '17 at 15:42
  • 1
    Beware that with `$command` defined as a string you'd need to use `Invoke-Expression` (which is not recommended). Better use it like this: `-ScriptBlock { & 'C:\windows\WUInstall.exe' /install /reboot }` – Ansgar Wiechers Aug 16 '17 at 15:45
  • will his run the execution parallelly. I would like to execute the script parallelly on all remote machine at a time – Sandeep Aug 16 '17 at 15:56
  • `Invoke-Command -AsJob` will run the command as a background job. For job management see [here](https://stackoverflow.com/a/35853502/1630171). To make sure you don't run too many jobs in parallel you may want to consider using a [job queue](https://stackoverflow.com/a/18193195/1630171). – Ansgar Wiechers Aug 16 '17 at 19:46
  • I have tried this, but exe is not getting executed on remote servers. Invoke-Command -AsJob -ComputerName $Computer -ScriptBlock {& 'c:\windows\WUInstall.exe' /install /reboot} – Sandeep Aug 17 '17 at 03:59

0 Answers0