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.