Share via


Powershell - Get registry entry for multiple computers

Question

Wednesday, June 27, 2012 5:42 PM

Hello,

I'm trying to get a registry value for a group of computers out of Active Directory. Below is the script I had in mind, but that keeps failing. Can anyone give me an idea of how to accomplish this? Thanks

get-adcomputer -filter {name -like "us*"} | select dnshostname | Get-ItemProperty -path HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Servic
es\W32Time\Parameters -name "ntpserver"

All replies (10)

Friday, June 29, 2012 11:35 AM ✅Answered

You'll have to call the $NTPSRV variable (which I didn't do in the code above).  Everyone has their own preference on how they want the output but here are some quick examples: 

If you just want to write the output to console, add this to the end of the foreach loop:

"$($SRV): $($NTPSRV)"

Or you can add this instead (or in additon to if you want both) to save the results in a CSV file (I delimited with ";" since the registry key value contains commas):

"$($SRV);$($NTPSRV)" | Out-File SRVNTP.CSV -append


Wednesday, June 27, 2012 6:09 PM

What is the error message you are receiving?

Based on this post Get-ItemProperty I think that command is ran on localy on the machine, so you can modify your script based off thier suggestions


Wednesday, June 27, 2012 6:19 PM

The problem is Get-ItemProperty is not capable of querying a remote registry.  You must use the invoke-command cmdlet to run it against a remote machine.


Wednesday, June 27, 2012 6:49 PM

Thanks for the suggestion.

Can you give me an idea of what that command would look like?


Wednesday, June 27, 2012 7:04 PM

Give this a try, in order for it to work PSRemoting must be enalbed on the machines you are trying this on

Import-Module ActiveDirectory

get-adcomputer -filter {name -like "us*"}  | select -expandproperty dnshostname | ForEach-Object {
    Invoke-Command -computername $_ -ScriptBlock { Get-ItemProperty -path HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\Parameters -name "ntpserver" }
}

Wednesday, June 27, 2012 9:30 PM | 1 vote

My preference is to skip invoke-command and do it like this:

$SRVS = get-adcomputer -filter {name -like "us*"} | select dnshostname

foreach ($SRV in $SRVS) {
$REG = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $SRV.name)
$REGKEY = $REG.OpenSubKey("SYSTEM\CurrentControlSet\Services\W32Time\Parameters")
$NTPSRV = $REGKEY.GetValue("NTPSERVER")
}


Thursday, June 28, 2012 9:39 PM

Thanks for the suggestion. I'm not sure what to expect with this. I just ran it but nothing returned. No errors. How can I get the output of this?

Thanks,

Scott


Friday, June 29, 2012 7:45 PM

I'd use Shay Levy's Remote Registry PowerShell Module

Karl

When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer
My Blog: http://unlockpowershell.wordpress.com
My Book: Windows PowerShell 2.0 Bible
My E-mail: -join ("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})


Thursday, May 11, 2017 3:46 PM

I ran code very similar to this and it worked fine with a single computer. But when I try to run against the list that Get-adcomputer generates (which I ran by itself and the output looks clean) then I get nothing but OpenRemoteBaseKey errors. 

$computers = Get-ADComputer -Filter * | format-table name
foreach ($computer in $computers)
{
$Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',$computer)
$RegKey= $Reg.OpenSubKey("SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters")
$description = $RegKey.GetValue("srvcomment")
write-host $description
}

Exception calling "OpenRemoteBaseKey" with "2" argument(s): "The network path was not found.
"
At C:\Users\chucks\Desktop\Untitled2.ps1:6 char:1

  • $Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine ...

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : IOException
 
You cannot call a method on a null-valued expression.
At C:\Users\chucks\Desktop\Untitled2.ps1:7 char:1

  • $RegKey= $Reg.OpenSubKey("SYSTEM\CurrentControlSet\Services\LanmanSer ...

    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
You cannot call a method on a null-valued expression.
At C:\Users\chucks\Desktop\Untitled2.ps1:8 char:1

  • $description = $RegKey.GetValue("srvcomment")

    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull


Thursday, May 11, 2017 4:45 PM

$computers = Get-ADComputer -Filter * | select -expand name

\(ツ)_/