How to pass multiple computer names to cmdlet using pipeline?

This topic contains 3 replies, has 3 voices, and was last updated by  Dan Potter 8 months, 2 weeks ago.

  • Author
    Posts
  • #61318

    dolphin89
    Participant

    Hello,

    I have a list of computer names (it includes two or more items), How I can pass the list to a cmdlet (e.g., get-hotfix) using pipeline parameter binding?

    My computers.csv file:

    ComputerName
    server2012
    desktop1
    

    I have tried this command:

    import-csv computers.csv | get-hotfix
    

    But its output contains list of hot fixes only for one of computers:

    Source        Description      HotFixID      InstalledBy          InstalledOn
    ------        -----------      --------      -----------          -----------
    DESKTOP1      Update           KB2693643     NT AUTHORITY\SYSTEM  1/4/2017 12:00:00 AM
    DESKTOP1      Update           KB3199986     NT AUTHORITY\SYSTEM  1/5/2017 12:00:00 AM
    DESKTOP1      Security Update  KB3209498     NT AUTHORITY\SYSTEM  1/5/2017 12:00:00 AM
    DESKTOP1      Security Update  KB3206632     NT AUTHORITY\SYSTEM  1/5/2017 12:00:00 AM
    

    I can get list of hot fixes for all of computers using parentheses:

    PS C:\Users\johndoe\documents> get-hotfix -computername (import-csv computers.csv | select -expand computername)
     
    Source        Description      HotFixID      InstalledBy          InstalledOn
    ------        -----------      --------      -----------          -----------
    SERVER2012    Update           KB2919355     MACLOCALDOMAIN\Ad... 3/21/2014 12:00:00 AM
    SERVER2012    Update           KB2919442     MACLOCALDOMAIN\Ad... 3/21/2014 12:00:00 AM
    SERVER2012    Update           KB2937220     MACLOCALDOMAIN\Ad... 3/21/2014 12:00:00 AM
    SERVER2012    Update           KB2938772     MACLOCALDOMAIN\Ad... 3/21/2014 12:00:00 AM
    SERVER2012    Update           KB2939471     MACLOCALDOMAIN\Ad... 3/21/2014 12:00:00 AM
    SERVER2012    Hotfix           KB2949621     MACLOCALDOMAIN\Ad... 3/21/2014 12:00:00 AM
    DESKTOP1      Update           KB2693643     NT AUTHORITY\SYSTEM  1/4/2017 12:00:00 AM
    DESKTOP1      Update           KB3199986     NT AUTHORITY\SYSTEM  1/5/2017 12:00:00 AM
    DESKTOP1      Security Update  KB3209498     NT AUTHORITY\SYSTEM  1/5/2017 12:00:00 AM
    DESKTOP1      Security Update  KB3206632     NT AUTHORITY\SYSTEM  1/5/2017 12:00:00 AM
    

    But I expected same output for both commands.

    My environment: Windows 10 Enterprise 64-bit, powershell 5.1.14393.576

  • #61320

    Christian Sandfeld
    Participant

    It would be easier to help if we could see your code. But from your explanation, it sounds as if you did not use a process block.

    Do a search for begin, end and process blocks, that should point you in the right direction.

  • #61339

    dolphin89
    Participant

    Hello, Christian, thank you for your answer! I have not code at all because I enter commands using powershell command line, and I have a single command that produces an unexpected (for me) output:

    PS C:\Users\johndoe\documents> import-csv computers.csv | get-hotfix
    
    Source        Description      HotFixID      InstalledBy          InstalledOn
    ------        -----------      --------      -----------          -----------
    DESKTOP1      Update           KB2693643     NT AUTHORITY\SYSTEM  1/4/2017 12:00:00 AM
    DESKTOP1      Update           KB3199986     NT AUTHORITY\SYSTEM  1/5/2017 12:00:00 AM
    DESKTOP1      Security Update  KB3209498     NT AUTHORITY\SYSTEM  1/5/2017 12:00:00 AM
    DESKTOP1      Security Update  KB3206632     NT AUTHORITY\SYSTEM  1/5/2017 12:00:00 AM
    

    I expect that it should print list of hot fixes for each computer from the computers.csv file (posted above), because:
    1) import-csv produces a list of PSCustomObject-s
    2) each PSCustomObject has property 'ComputerName' with a String value
    3) get-hotfix cmdlet supports pipeline input with ByPropertyName binding
    4) when I use import-csv combined (pipelined) with an another command (e.g. new-aduser) it works as I expect and creates all users from csv list instead of creating a single user

    I have an ugly solution for now:

    PS C:\Users\johndoe\documents> import-csv computers.csv | foreach { get-hotfix -computername $_.computername }
    
    Source        Description      HotFixID      InstalledBy          InstalledOn
    ------        -----------      --------      -----------          -----------
    SERVER2012    Update           KB2919355     MACLOCALDOMAIN\Ad... 3/21/2014 12:00:00 AM
    SERVER2012    Update           KB2919442     MACLOCALDOMAIN\Ad... 3/21/2014 12:00:00 AM
    SERVER2012    Update           KB2937220     MACLOCALDOMAIN\Ad... 3/21/2014 12:00:00 AM
    SERVER2012    Update           KB2938772     MACLOCALDOMAIN\Ad... 3/21/2014 12:00:00 AM
    SERVER2012    Update           KB2939471     MACLOCALDOMAIN\Ad... 3/21/2014 12:00:00 AM
    SERVER2012    Hotfix           KB2949621     MACLOCALDOMAIN\Ad... 3/21/2014 12:00:00 AM
    DESKTOP1      Update           KB2693643     NT AUTHORITY\SYSTEM  1/4/2017 12:00:00 AM
    DESKTOP1      Update           KB3199986     NT AUTHORITY\SYSTEM  1/5/2017 12:00:00 AM
    DESKTOP1      Security Update  KB3209498     NT AUTHORITY\SYSTEM  1/5/2017 12:00:00 AM
    DESKTOP1      Security Update  KB3206632     NT AUTHORITY\SYSTEM  1/5/2017 12:00:00 AM
    

    But I think that this task may be solved without foreach cmdlet.

  • #61392

    Dan Potter
    Participant

    computername parameter accepts an array.

    [-ComputerName ]

    get-hotfix -ComputerName (get-content .\servers.txt)

    or

    $arrayofcomputersex = (get-content .\servers.txt)
    get-hotfix -ComputerName $arrayofcomputersex

You must be logged in to reply to this topic.