Invoke-Command issued to multiple remote computer

This topic contains 1 reply, has 2 voices, and was last updated by  Dave Wyatt 4 years, 1 month ago.

  • Author
  • #15169

    Bob Rains

    I am trying to issue an Invoke-Command simultaneously to several computers. I am reading the names of the computers from a .txt file. My issue is this. Invoke-Command will take multiple computer names as the -cn parameter. If I issue the command: Invoke-Command -cn server1, server2 =scriptblock {xxx} interactively, it works fine, however when I try to read the list of computers from the .txt file (in the form of: server1, server2) the -cn parameter is a concatenation of all the server names into one. I've tried using a .split(",") when I read the file and that splits the names, but then my code issues the command for one at a time instead of simultaneously. Here is my code:

    $computers = (gc C:\psscripts\IMM_Patch_Install\computers.txt).split(",")

    foreach ($computer in $computers) {
    if (test-Connection -cn $computer -quiet) {
    invoke-command -cn $computer -scriptblock {c:; cd \IMM2_Patch; cmd /c uxspi up -e all -i ibm_fw_imm2_1aoo56d-3.73_anyos_noarch -s all -u –ignore-req –latest –update-args="IMM: –backup –no-reboot"}
    invoke-command -cn $computer -scriptblock {c:; cd \IMM2_Patch; cmd /c uxspi up -e all -i ibm_fw_imm2_1aoo56d-3.73_anyos_noarch -s all -u –ignore-req –latest}
    $completed = get-date
    Echo "IMM update to $computer completed successfully on $completed" >> C:\PSscripts\IMM_Patch_Install\completed.txt
    } else {
    $completed = get-date
    Echo "IMM update to $computer failed on $completed" >> C:\PSscripts\IMM_Patch_Install\completed.txt


    My .txt file looks like this:

    So what I need is to read the file and have the Invoke-Command issued to all computers on one line simultaneously. Any assistance would be greatly appreciated. Thanks much.


  • #15172

    Dave Wyatt

    You could do something like this:

    $computers = Get-Content .\computers.txt |
                 ForEach-Object { $_ -split ',' }
    Invoke-Command -ComputerName $computers -ScriptBlock { Do-Stuff }

You must be logged in to reply to this topic.