locked
why does this fail ? what is the best way to accomplish this task ? RRS feed

  • Question

  • PS C:\scripts> $computers = Get-Content workstations.txt
    
    PS C:\scripts> Invoke-Command -ComputerName $Computers
    Invoke-Command : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Provide an argument that is not null 
    or empty, and then try the command again.
    At line:1 char:30
    + Invoke-Command -ComputerName $Computers
    +                              ~~~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Invoke-Command], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeCommandCommand

    Thursday, September 26, 2019 5:25 PM

Answers

  • You can't have blank lines in your file. Remove all blank lines and try again.


    \_(ツ)_/

    • Marked as answer by Beno19 Friday, September 27, 2019 1:55 AM
    Friday, September 27, 2019 12:58 AM

All replies

  • Try to output the variable $Computers. And you have to provide either a script file or a scriptblock.

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Proposed as answer by jrv Thursday, September 26, 2019 6:23 PM
    Thursday, September 26, 2019 5:53 PM
  • Hi Beno19.

    The $computers variable contains an array of computers: the -ComputerName parameter of the Invoke-Command cmdlet accepts a comma-separated list of computers, which is not available when you import the content of the text file into that variable.
    You could refactor your command using a ForEach loop: at each iteration, you'll get the name of the next computer in the array and execute the Invoke-Command cmdlet as below

    $computers = Get-Content workstations.txt
    ForEach ($machine In $computers)
    {
      Invoke-Command -ComputerName $machine -ScriptBlock {}
    }

    You have to include the instructions to be executed into the braces after the ScriptBlock parameter; alternatively, you could use the FilePath parameter which specifies a local script that this cmdlet runs on one or more remote computers.

    Bye.


    Luigi Bruno
    MCP, MOS, MTA, MCTS, MCSA, MCSE



    Thursday, September 26, 2019 6:05 PM
  • Hi Beno19.

    The $computer variable contains an array of computers: the Invoke-Command cmdlet accepts a comma-separated list of computers, which is not available when you import the content of the text file into that variable.
    You could refactor your command using a ForEach loop: at each iteration, you'll get the name of the next computer in the array and execute the Invoke-Command cmdlet as below

    $computers = Get-Content workstations.txt
    ForEach ($machine In $computers)
    {
      Invoke-Command -ComputerName $machine
    }

    Bye.


    Luigi Bruno
    MCP, MOS, MTA, MCTS, MCSA, MCSE

    No.  The computer parameter takes an array of names which is what we have when a file is loaded into a variable.  Read the help for that parametrer to see its definition.

    help Invoke-Command -Parameter ComputerName

    When you learn PowerShell you will learn that help is a fist stop to learn how a parameter is defined.  It is an invaluable resource that lets us understand the exact power and capability of each individual command.  Learning basic PowerShell is critical to understanding how to use these things.


    \_(ツ)_/

    Thursday, September 26, 2019 6:23 PM
  • Hi Beno19.

    The $computer variable contains an array of computers: the Invoke-Command cmdlet accepts a comma-separated list of computers, which is not available when you import the content of the text file into that variable.
    You could refactor your command using a ForEach loop: at each iteration, you'll get the name of the next computer in the array and execute the Invoke-Command cmdlet as below

    $computers = Get-Content workstations.txt
    ForEach ($machine In $computers)
    {
      Invoke-Command -ComputerName $machine
    }

    Bye.


    Luigi Bruno
    MCP, MOS, MTA, MCTS, MCSA, MCSE

    No.  The computer parameter takes an array of names which is what we have when a file is loaded into a variable.  Read the help for that parametrer to see its definition.

    help Invoke-Command -Parameter ComputerName

    When you learn PowerShell you will learn that help is a fist stop to learn how a parameter is defined.  It is an invaluable resource that lets us understand the exact power and capability of each individual command.  Learning basic PowerShell is critical to understanding how to use these things.


    \_(ツ)_/

    I've made a typo when writing: I was referring to the $computers variable; I've also added the ScriptBlock parameter to make my reply complete, as well as the FilePath one as the other alternative.
    Anyway, the Microsoft Docs page for the Invoke-Command cmdlet states that the ComputerName parameter must be (quoting) "...the NETBIOS name, IP address, or fully qualified domain name of one or more computers in a comma-separated list."

    Bye.


    Luigi Bruno
    MCP, MOS, MTA, MCTS, MCSA, MCSE

    Thursday, September 26, 2019 6:43 PM

  • $computers = Get-Content workstations.txt
    ForEach ($machine In $computers)
    {
      Invoke-Command -ComputerName $machine
    }

    This will not work without a scrpitblock or a script file. Please re-read te help - especially the parameter sets and the mandatory parameters for these sets.

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Thursday, September 26, 2019 6:45 PM

  • $computers = Get-Content workstations.txt
    ForEach ($machine In $computers)
    {
      Invoke-Command -ComputerName $machine
    }

    This will not work without a scrpitblock or a script file. Please re-read te help - especially the parameter sets and the mandatory parameters for these sets.

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    I've fixed my first reply.
    Thank you.

    Bye.


    Luigi Bruno
    MCP, MOS, MTA, MCTS, MCSA, MCSE

    Thursday, September 26, 2019 6:50 PM
  • This is why I suggest that al users actually take time to learn PowerShell. This is what the help document actually says.  Notice that it takes a string array.  Learning PowerShell instead of guessing how to use it will teach you how to learn about PowerShell commands, parameters and their usage.  You cannot learn this from forums, blogs and guesswork.

    PS C:\scripts> help invoke-command -par computername

    -ComputerName <String[]>
        Specifies the computers on which the command runs. The default is the local computer.

        When you use the ComputerName parameter, Windows PowerShell creates a temporary connection that is used only to run the specified command and is then closed. If you
        need a persistent connection, use the Session parameter.

        Type the NETBIOS name, IP address, or fully qualified domain name of one or more computers in a comma-separated list. To specify the local computer, type the
        computer name, localhost, or a dot (.).

        To use an IP address in the value of ComputerName , the command must include the Credential parameter. Also, the computer must be configured for HTTPS transport or
        the IP address of the remote computer must be included in the WinRM TrustedHosts list on the local computer. For instructions for adding a computer name to the
        TrustedHosts list, see "How to Add a Computer to the Trusted Host List" in about_Remote_Troubleshooting.

        On Windows Vista and later versions of the Windows operating system, to include the local computer in the value of ComputerName , you must open Windows PowerShell
        by using the Run as administrator option.

        Required?                    false
        Position?                    0
        Default value                None
        Accept pipeline input?       False
        Accept wildcard characters?  false


    \_(ツ)_/

    Thursday, September 26, 2019 7:02 PM
  • We do not need a loop with this command. In fact it will run faster when used correctly.

    $computers = Get-Content workstations.txt
    Invoke-Command -ComputerName $computers -ScriptBlock {...}

    Again I recommend that anyone wishing to use PowerShell or answer technical questions must learn basic PowerShell including how to read the help and how to understand what the help is telling you.  Help is not just a novel or short story.  It is a very well structured technical document that gives the fundamental information needed to describe the usage of each CmdLet.  Guessing and copying others guesses will not help you learn PowerShell.


    \_(ツ)_/

    Thursday, September 26, 2019 7:07 PM
  • This is why I suggest that al users actually take time to learn PowerShell. This is what the help document actually says.  Notice that it takes a string array.  Learning PowerShell instead of guessing how to use it will teach you how to learn about PowerShell commands, parameters and their usage.  You cannot learn this from forums, blogs and guesswork.

    PS C:\scripts> help invoke-command -par computername

    -ComputerName <String[]>
        Specifies the computers on which the command runs. The default is the local computer.

        When you use the ComputerName parameter, Windows PowerShell creates a temporary connection that is used only to run the specified command and is then closed. If you
        need a persistent connection, use the Session parameter.

        Type the NETBIOS name, IP address, or fully qualified domain name of one or more computers in a comma-separated list. To specify the local computer, type the
        computer name, localhost, or a dot (.).

        To use an IP address in the value of ComputerName , the command must include the Credential parameter. Also, the computer must be configured for HTTPS transport or
        the IP address of the remote computer must be included in the WinRM TrustedHosts list on the local computer. For instructions for adding a computer name to the
        TrustedHosts list, see "How to Add a Computer to the Trusted Host List" in about_Remote_Troubleshooting.

        On Windows Vista and later versions of the Windows operating system, to include the local computer in the value of ComputerName , you must open Windows PowerShell
        by using the Run as administrator option.

        Required?                    false
        Position?                    0
        Default value                None
        Accept pipeline input?       False
        Accept wildcard characters?  false


    \_(ツ)_/

    Yes, the type for that parameter is an array of string, but the computers' names must be passed as a comma separated list.

    Bye.


    Luigi Bruno
    MCP, MOS, MTA, MCTS, MCSA, MCSE

    Thursday, September 26, 2019 7:18 PM
  • Yes, the type for that parameter is an array of string, but the computers' names must be passed as a comma separated list.

    Bye.


    Luigi Bruno
    MCP, MOS, MTA, MCTS, MCSA, MCSE

    Not true.  The parameter states it takes an array.  When typing at a command line comma separated values will always be passed as an array and qu0et are optional.  This is part of the basics of PowerShell which you need to learn.  It is someth9ng you learn in the first chapter or the first video of a tutorial.  Not knowing basic PowerShell will lead you to make hundreds of bad assumptions and , as you are doing here, you will further spread incorrect technical information.

    Please take the time to learn PowerShell correctly and don't guess.


    \_(ツ)_/

    Thursday, September 26, 2019 8:24 PM
  • Thank you guys for you suggestions but i am still having issues, please keep in mind that i am not trying to get the process i have another working script that i will need to run after the scriptBlock i just substituted get-process for simplicity. 

    first let see what we have in the file ?

    PS G:\> $Computers = Get-Content -Path "c:\scripts\workstations.txt"
    $Computers
    
    dev
    testpc   

    Now i try this approach 


    PS G:\> $Computers = Get-Content -Path "c:\scripts\workstations.txt" Invoke-Command -ComputerName $Computers -ScriptBlock{get-process} Invoke-Command : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again. At line:2 char:30 + Invoke-Command -ComputerName $Computers -ScriptBlock{get-process} + ~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Invoke-Command], ParameterBindingValidation Exception + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Command s.InvokeCommandCommand

    OK Lets try another approach 

    PS G:\> $Computers = Get-Content -Path "c:\scripts\workstations.txt"
    ForEach ($machine In $computers)
    {Invoke-Command -ComputerName $machine -ScriptBlock {get-process}}
    
    Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName       PSComputerName  
    -------  ------    -----      -----     ------     --  -- -----------       --------------  
         35       5     1800       4420       0.05    240     conhost           dev             
        409      11     1872       4332       0.17    320     csrss             dev             
         71       8    15988      11108       0.11    380     csrss             dev             
        195       9     1736       5088       0.16   1768     csrss             dev             
        189      15     4068      11504       0.33   1796     dllhost           dev             
        106       8     1672       5848       0.02   2836     dllhost           dev             
         66       7     1176       4180       0.00   2260     dwm               dev             
        580      36    23588      42944       2.01   2324     explorer          dev             
          0       0        0         24                 0     Idle              dev             
        164      23    12256      19968       0.17    744     LogonUI           dev             
       1068      29     6504      15392       2.18    488     lsass             dev             
        226      10     2684       6464       0.02    496     lsm               dev             
        515      44    52860      27152       2.96   2848     mmc               dev             
        148      17     3192       8260       0.02   1896     msdtc             dev             
        693      27    55492      65592       1.79   2880     powershell        dev             
        175      10     1916       6852       0.06   2204     rdpclip           dev             
        210      13     3804       7912       0.55    472     services          dev             
         32       1      376       1196       0.06    228     smss              dev             
        319      21     6676      13512       0.11   1036     spoolsv           dev             
        167       8     2896       9708       2.65   2760     sppsvc            dev             
        293      32     9636      12940       0.51    384     svchost           dev             
        361      14     3492       9420       0.86    592     svchost           dev             
        268      16     3460       8036       0.33    668     svchost           dev             
        310      16    10036      13152      10.09    756     svchost           dev             
       1575     677   369804     210836       7.22    796     svchost           dev             
        545      23     6236      11936       0.90    856     svchost           dev             
        254      17     6964      14660       3.49    900     svchost           dev             
        445      37    12196      19104       2.17    940     svchost           dev             
        148      11     3580       7204       0.00   1072     svchost           dev             
         46       4      780       2848       0.00   1132     svchost           dev             
        284      15     2768       8952       0.12   1500     svchost           dev             
        100      13     1580       5856       0.02   2092     svchost           dev             
        527       0      128        304                 4     System            dev             
        145      13     3000       7924       0.05   2124     taskhost          dev             
        124      13    13528      19288     253.02   2824     TrustedInstaller  dev             
         94      12     3104       8848       0.06   1164     VGAuthService     dev             
        262      23     9304      20012       2.59   1316     vmtoolsd          dev             
         79       9     1356       4580       0.03    372     wininit           dev             
         95       7     1480       4932       0.05    264     winlogon          dev             
         75       6     1256       4336       0.03    416     winlogon          dev             
        202      15     7664      13804       3.14   1672     WmiPrvSE          dev             
        222      34    45692      51940       1.00   2040     wsmprovhost       dev             
    Invoke-Command : One or more computer names are not valid. If you are trying to pass a URI, 
    use the -ConnectionUri parameter, or pass URI objects instead of strings.
    At line:3 char:2
    + {Invoke-Command -ComputerName $machine -ScriptBlock {get-process}}
    +  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (System.String[]:String[]) [Invoke-Command], 
        ArgumentException
        + FullyQualifiedErrorId : PSSessionInvalidComputerName,Microsoft.PowerShell.Commands.In 
       vokeCommandCommand
     
    Invoke-Command : One or more computer names are not valid. If you are trying to pass a URI, 
    use the -ConnectionUri parameter, or pass URI objects instead of strings.
    At line:3 char:2
    + {Invoke-Command -ComputerName $machine -ScriptBlock {get-process}}
    +  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (System.String[]:String[]) [Invoke-Command], 
        ArgumentException
        + FullyQualifiedErrorId : PSSessionInvalidComputerName,Microsoft.PowerShell.Commands.In 
       vokeCommandCommand
     
    Invoke-Command : Cannot validate argument on parameter 'ComputerName'. The argument is null 
    or empty. Provide an argument that is not null or empty, and then try the command again.
    At line:3 char:31
    + {Invoke-Command -ComputerName $machine -ScriptBlock {get-process}}
    +                               ~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Invoke-Command], ParameterBindingValidation 
       Exception
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Command 
       s.InvokeCommandCommand
     
    Invoke-Command : Cannot validate argument on parameter 'ComputerName'. The argument is null 
    or empty. Provide an argument that is not null or empty, and then try the command again.
    At line:3 char:31
    + {Invoke-Command -ComputerName $machine -ScriptBlock {get-process}}
    +                               ~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Invoke-Command], ParameterBindingValidation 
       Exception
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Command 
       s.InvokeCommandCommand

    Ok this one seemed to work on the first PC but had issues running on the second PC. 

    this command works just fine and gets me the result but i need to get the names from a list

    Invoke-Command -ComputerName dev,testpc -ScriptBlock {get-process}

    Friday, September 27, 2019 12:33 AM
  • You can't have blank lines in your file. Remove all blank lines and try again.


    \_(ツ)_/

    • Marked as answer by Beno19 Friday, September 27, 2019 1:55 AM
    Friday, September 27, 2019 12:58 AM
  • Yes that was it ! Now everything works. thank you for your help. i really appreciate it !
    Friday, September 27, 2019 1:54 AM