Invoke-Command issued to multiple remote computer

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Dave Wyatt Dave Wyatt 2 years, 7 months ago.

  • Author
    Posts
  • #15169
    Profile photo of Bob Rains
    Bob Rains
    Participant

    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:
    server1,server2,server3,server4
    server5,server6,server7,server8

    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.

    Bob

  • #15172
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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.