my Invoke-Command won't take my for-each

This topic contains 9 replies, has 3 voices, and was last updated by  Richard Siddaway 6 months, 4 weeks ago.

  • Author
  • #82796

    Jeff Taylor

    I get this to work:

    Invoke-Command -ComputerName  { Get-WmiObject Win32_Service |
        Where-Object { $_.Name -like "NP*" } | Select-Object State }

    ...yet, this fails:

    $DCs=Import-Csv .\DCs.csv
    foreach ($DC in $DCs) {
        Invoke-Command -ComputerName $DC  { Get-WmiObject Win32_Service |
        Where-Object { $_.Name -like "NP*" } | Select-Object State }

    with error:

    Invoke-Command : One or more computer names are not valid.

    ... I do see a valid and pingable FQDN for the current DC in the variable though...

  • #82804

    Joe Acosta

    what does $dcs and $dcs.count output?

    • #82810

      Jeff Taylor

      all of my DC's and the correct count for those DCs

    • #82811

      Joe Acosta

      My best guess is your trying to feed an object into a parameter looking for strings. Its a guess because I don't know what is actually in $dcs. Totally understand if you don't want to post it tho. Maybe look at $dcs.gettype() and see if you are getting an object. Maybe insert a $dc.gettype() prior to the invoke-command line. Bonne chance!

    • #82813

      Jeff Taylor
      IsPublic IsSerial Name                                     BaseType                                                                                                                                                            
      -------- -------- ----                                     --------                                                                                                                                                            
      True     True     Object[]                                 System.Array 
    • #82816

      Joe Acosta

      So take the property (column) of $dcs that has the fqdn of the computer and use it. E.g., if it is Name, use $ in your invoke-command statement instead of just $dc.

    • #82817

      Jeff Taylor

      "name" is not an option for that variable. Only "ToString" and a few others

    • #82820

      Joe Acosta

      Yes, I had to guess that Name was the property value because you didn't post the output of $dcs. I've given you both the problem and the resolution. Your feeding an object to a parameter that wants strings. You need to use $dc.WhatEverItIsThatTheColumnIs that has the fqdn of your computer.

      So if the output of $dcs looks like the below, sub whatever your value is in place of Name. $dc.Name would work for the below.

      You could also take a look at $dc | get-member

      Good luck


    • #82823

      Jeff Taylor

      Thank you, that worked. It was "DC"

      PS F:\Jeff> $dcs | gm
         TypeName: System.Management.Automation.PSCustomObject
      Name        MemberType   Definition                            
      ----        ----------   ----------                            
      Equals      Method       bool Equals(System.Object obj)        
      GetHashCode Method       int GetHashCode()                     
      GetType     Method       type GetType()                        
      ToString    Method       string ToString()                     
      DC          NoteProperty string
  • #82870

    Richard Siddaway

    Change your code to

    Import-Csv .\DCs.csv |
    foreach  {
        Invoke-Command -ComputerName $_.DC  { Get-WmiObject Win32_Service |
        Where-Object { $_.Name -like "NP*" } | Select-Object State }

    and it should work.
    Import-CSV really works best if you pipe the results into your next commands.

    Also use Get-CimInstance. Get-WmiObject is so PowerShell v2

You must be logged in to reply to this topic.