Help with running Invoke command on list of names in a variable

Hi,

Sorry this is probably a really stupid question but i cannot get this to work or figure it out.

Background
I am trying to pull a list of certificates from our Active Directory servers but do not want to rely on a text file with a list of servers to run the command against. My idea was to create a variable that contains the list of servers then using a foreach loop run the Invoke-Command against that variable. No matter what i am trying i cannot get it to work.

Details

#Get List of ADs (except Azure ones) and put that into a variable called $dcs
$dc = Get-ADDomainController -filter * | Select-Object Name | Sort-Object Name | Where-Object {$_.name -notlike "*AZ*"} | ft -HideTableHeaders | Out-String
$dc = $dc.Trim()

#For Each Loop to run the Invoke-Command against in turn
foreach ($domaincont in $dc)
{
Invoke-Command -ScriptBlock {Get-ChildItem Cert:\LocalMachine\My -Recurse -ExpiringInDays 365| Select Subject, FriendlyName, NotBefore, NotAfter | Format-Table -Wrap} -ComputerName $domaincont > "c:\DCOut.txt"
}

What I’ve Tried
I know the initial creation of the variable $dc is working. If i add a line to echo $dc it produces a list of servers exactly as i would want. No column headers etc. Just a list of servers, one on each line. So, i thought all i would need to do is that create a foreach loop to run the Invoke-Command and it should work. Wrong. All i get are errors like this;

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:10 char:9

Not sure why it is saying one of more compute names are not valid. Been searching around the www and think just got to a point where i’ve confused myself over something which should be very simple.

Request
So could i ask anyone to just give me a pointer as to how i run the Invoke-Command detailed against a list of servers contained within the $dc variable created in line 1 & 2 of the script. It would save me from wishing i had never decided to try learn Powershell.

AndyP,
Welcome to the forum. :wave:t4:

You make you life much harder than necessary. :wink:

PowerShell does a lot of stuff implicitly for you without asking for it.

In PowerShell we work with objects and their properties. No need to use string acrobatics. :wink: And since Invoke-Command can use arrays for the parameter -ComputerName you don’t need a loop.

$dcList = Get-ADDomainController -Filter * 

Invoke-Command -ComputerName $dcList.Name -ScriptBlock {
    Get-ChildItem Cert:\LocalMachine\My -Recurse -ExpiringInDays 365 | 
        Select-Object -Property Subject, FriendlyName, NotBefore, NotAfter 
} 

If you like to have the result in a file you can pipe it to

Please always read the help for the cmdlets you’re about to use completely including the examples to learn how to use them.

In general: format cmdlets like Format-Table are ment to be used for console output only. In the vast majority of the cases it does not make any sense to pipe their output to the next cmdlet.

Hi Olaf,

That’s great, thank you. Been trying to learn this and got to a point of confusion i think. Thanks for recommendation and it works beautifully.

I’ll go find myself a powershell course to do i think now :slight_smile:

Thanks