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

I get this to work:

Invoke-Command -ComputerName DC1.domain.com  { 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 }
}</pre>

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…

what does $dcs and $dcs.count output?

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

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!

IsPublic IsSerial Name                                     BaseType                                                                                                                                                            
-------- -------- ----                                     --------                                                                                                                                                            
True     True     Object[]                                 System.Array 

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

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

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

Name

host1.example.com
host2.example.com
host3.example.com

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 DC=DC1.company.com

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