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

**URL:** <https://forums.powershell.org/t/help-with-running-invoke-command-on-list-of-names-in-a-variable/21203>\
**Category:** PowerShell Help\
**Created:** [December 21, 2022, 9:40am UTC](https://forums.powershell.org/t/help-with-running-invoke-command-on-list-of-names-in-a-variable/21203 "2022-12-21T09:40:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AndyP](https://avatars.discourse-cdn.com/v4/letter/a/8baadc/32.png) [@AndyP](https://forums.powershell.org/u/AndyP)\
**Post date:** [December 21, 2022, 9:40am UTC](https://forums.powershell.org/t/help-with-running-invoke-command-on-list-of-names-in-a-variable/21203/1 "2022-12-21T09:40:22Z")

</div>

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

```auto
#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.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [December 21, 2022, 10:18am UTC](https://forums.powershell.org/t/help-with-running-invoke-command-on-list-of-names-in-a-variable/21203/2 "2022-12-21T10:18:20Z")

</div>

AndyP,  
Welcome to the forum. 👋🏽

You make you life much harder than necessary. 😉

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. 😉 And since `Invoke-Command` can use arrays for the parameter `-ComputerName` you don’t need a loop.

```auto
$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

> **[Export-Csv (Microsoft.PowerShell.Utility) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/export-csv?view=powershell-7.3)**
>
> The Export-CSV cmdlet creates a CSV file of the objects that you submit. Each object is a row that includes a character-separated list of the object's property values. You can use the Export-CSV cmdlet to create spreadsheets and share data with...

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.

---

<div class="post-metadata">

**Author:** ![AndyP](https://avatars.discourse-cdn.com/v4/letter/a/8baadc/32.png) [@AndyP](https://forums.powershell.org/u/AndyP)\
**Post date:** [December 21, 2022, 10:45am UTC](https://forums.powershell.org/t/help-with-running-invoke-command-on-list-of-names-in-a-variable/21203/3 "2022-12-21T10:45:23Z")

</div>

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 🙂

Thanks

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:16pm UTC](https://forums.powershell.org/t/help-with-running-invoke-command-on-list-of-names-in-a-variable/21203/4 "2024-05-16T20:16:10Z")

</div>

Closed on May 16, 2024, 8:16pm UTC


