none
Powershell - Get registry entry for multiple computers

    Domanda

  • 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"

    mercoledì 27 giugno 2012 17:42

Risposte

  • 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


    venerdì 29 giugno 2012 11:35

Tutte le risposte

  • 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

    • Modificato clayman2 mercoledì 27 giugno 2012 18:19
    mercoledì 27 giugno 2012 18:09
  • 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.
    mercoledì 27 giugno 2012 18:19
  • Thanks for the suggestion.

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

    mercoledì 27 giugno 2012 18:49
  • 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" }
    }



    • Modificato clayman2 mercoledì 27 giugno 2012 19:06 updated script
    mercoledì 27 giugno 2012 19:04
  • 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")
    }

    mercoledì 27 giugno 2012 21:30
  • 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

    giovedì 28 giugno 2012 21:39
  • 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


    venerdì 29 giugno 2012 11:35
  • 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$_"})

    venerdì 29 giugno 2012 19:45