Hello,
I am trying to pull NTP values on all DC’s in a domain. I have this code:
$AllDCs = get-addomaincontroller -filter * -server domain.local
foreach($dc in $AllDCs){
Invoke-Command -computername $dc -ScriptBlock {Get-ItemProperty -path hklm:\System\CurrentControlSet\Services\W32Time\Parameters -Name "NtpServer" | Select-Object "NtpServer"}
}
I don’t get any error but I don’t get any values for all those DC’s either. What am I doing incorrectly?
The only change I recommend is not looping over them, simply pass all to invoke-command to have them run asynchronously (up to 32 by default)
$AllDCs = get-addomaincontroller -filter * -server domain.local
Invoke-Command -computername $AllDCs -ScriptBlock {
Get-ItemProperty -path hklm:\System\CurrentControlSet\Services\W32Time\Parameters -Name "NtpServer" | Select-Object "NtpServer"
}
You should see output like this
NtpServer PSComputerName RunspaceId
--------- -------------- ----------
time.windows.com,0x9 DC1 e3988161-79b4-4616-8ab4-99b2e9a0062f
time.windows.com,0x9 DC2 f6de969d-8363-43e0-8692-3f6c973c022e
time.windows.com,0x9 DC3 06ba4c4b-ebed-4b5b-a727-e7c8c8fc88fa
Thanks Doug. If I wanted to collect all of that output in an export? This only captures one row.
$AllDCs = get-addomaincontroller -filter * -server domain.local
Invoke-Command -computername $AllDCs -ScriptBlock {
Get-ItemProperty -path hklm:\System\CurrentControlSet\Services\W32Time\Parameters -Name "NtpServer" |
Select-Object "NtpServer" | export-csv -path "myPath" -NoTypeInfo
}
Simply move the export outside of the scriptblock
$AllDCs = get-addomaincontroller -filter * -server domain.local
Invoke-Command -computername $AllDCs -ScriptBlock {
Get-ItemProperty -path hklm:\System\CurrentControlSet\Services\W32Time\Parameters -Name "NtpServer" |
Select-Object "NtpServer"
} | export-csv -path "myPath" -NoTypeInfo