Invoke-Command to get Services set to Automatic on all DCs

I tried this code:

$DCs = [DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest() |
    Select-Object -ExpandProperty Sites |
        Select-Object -ExpandProperty Servers |
            Select-Object -ExpandProperty Name

    Invoke-Command -ComputerName $DCs {
        Get-Service | Select-Object -Property Name,Status,StartType | Where-Object {$_.Status -eq "Stopped" -and $_.StartType -eq "Automatic"}  |
                Export-Csv -Path C:\Temp\AllDCs_RemoteRegistry.csv -NoTypeInformation
    }

…but nothing in csv.

Alternatively, how can I get a specific Service status running on all DCs in that variable?

Probably something like:

Invoke-Command -ComputerName $DCs {
        Get-Service | Select-Object -Property Name,Status,StartType | Where-Object {$_.Name -eq "RemoteRegistry"}  |
                Export-Csv -Path C:\Temp\AllDCs_SingleService.csv -NoTypeInformation
    }

…but not until I can get the first part working.

Couple of things. Is DCs$ populated? Export-Csv is inside your Invoke-Command script block so it is saving it on the remote machine not the one you are running from. Are you looking for it on each DC? If not move your pipe / Export-Csv to after your closing } for the Invoke-Command script block.

Yes, plenty of DCs there. I don’t need the export then to troubleshoot. Yes, I want all DCs

Revised:

Invoke-Command -ComputerName $DCs 
        Get-Service | Select-Object -Property Name,Status,StartType | Where-Object {$_.Status -eq "Stopped" -and $_.StartType -eq "Automatic"}


Only getting the host I'm on returned. That would surprise me that it's the only one.

Name            Status StartType
----            ------ ---------
RemoteRegistry Stopped Automatic
sppsvc         Stopped Automatic

Could have sworn I posted a reply, but trying again.

Yes, got all DCs in the variable. Don’t need csv then but do want all services meeting the criteria.

If you don’t need the csv, then what are you trying to do? You are retrieving data from remote machines, where do you want it to go?

Actually, I’m only returning one DC but want a report from all

Name Status StartType


RemoteRegistry Stopped Automatic
sppsvc Stopped Automatic

You are creating a CSV on every DC rather than getting all the information from the DC’s and creating a CSV:

$DCs = [DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest() |
    Select-Object -ExpandProperty Sites |
        Select-Object -ExpandProperty Servers |
            Select-Object -ExpandProperty Name
 
$results = Invoke-Command -ComputerName $DCs {
    Get-Service | 
        Select-Object -Property Name,Status,StartType | 
             Where-Object {$_.Status -eq "Stopped" -and $_.StartType -eq "Automatic"}
                
}

$results | Export-Csv -Path C:\Temp\AllDCs_RemoteRegistry.csv -NoTypeInformation

Rob, my apologies for not responding sooner, Thank you very much for this observation.