Author Posts

October 25, 2017 at 1:11 am

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 }
    }

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...

October 25, 2017 at 1:42 am

what does $dcs and $dcs.count output?

October 25, 2017 at 2:20 am

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

October 25, 2017 at 2:37 am

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!

October 25, 2017 at 2:45 am

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

October 25, 2017 at 2:54 am

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.

October 25, 2017 at 3:01 am

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

October 25, 2017 at 4:04 am

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

October 25, 2017 at 5:30 am

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

October 25, 2017 at 2:34 pm

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