Get a Registry key value from txt files

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Uzair0229 Uzair0229 2 months, 3 weeks ago.

  • Author
    Posts
  • #69607
    Profile photo of Uzair0229
    Uzair0229
    Participant

    Hi,

    I am trying to get a registry value from the remote servers , i got the value but i want to know which computer has that value
    and i am not able to that , Can someone help to get a output like Computername1 : regsitry value , Computername12 : regsitry value ..

    $servers = Get-Content "C:\Users\servers.txt"
    $reg= "HKLM:\SYSTEM\CurrentControlSet\Control\Rsa"
    foreach ($server in $servers)
    {
    (Get-ItemProperty -path $Reg -name "Packages")."Packages"

    }

  • #69616
    Profile photo of Chris Cass
    Chris Cass
    Participant

    From what I understand, you can't get remote registry settings like that... This goes into detail:

    Here's a script I ran to get info from the SQL registry keys on remote servers:

    #Specify the file path to list of servers to run this script against
    
    $computers = gc "X:\Folder\Servers.txt"
    
    Clear-Host
    
    foreach ($Computer in $Computers) 
    {
    if ((Test-Path -Path \\$computer\C$)){
        $computerSystem = get-wmiobject Win32_ComputerSystem -Computer $Computer
    #Gather SQL Edition (Express or Standard) and SQL Version Number from Registry
     
        $machinename = $computer
        $key = "Software\\Microsoft\\Microsoft SQL Server\\Instance Names\\SQL"
        $valuename = "SQLServer"
    
        $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $machinename)
        $regkey = $reg.opensubkey($key)
        
            $p = $regkey.getvalue($valuename)
    	    $KeyEd = "SOFTWARE\\Microsoft\\Microsoft SQL Server\\$p\\Setup"
    	    $valueEd = "Edition"
    	    $valueVer = "Version"
    	    $regkeysetup = $reg.opensubkey($keyed)
    	    $e = $regkeysetup.getvalue($ValueEd)
    	    $v = $regkeysetup.getvalue($valueVer)
    
            write-host "System Information for: " $computerSystem.Name -BackgroundColor DarkCyan
            "-------------------------------------------------------"
            Write-Output 
            "SQL Version: " + $ValueVer
            "SQL Edition: " + $ValueEd
    }
    else
    {write-host "$computer is unreachable or does not exist" -BackgroundColor DarkCyan}
    }
    

    I'm fairly new to this, so there is probably a quicker way of doing this... But this has worked great for me and I've done it with multiple registry keys.

  • #69627
    Profile photo of Daniel Krebs
    Daniel Krebs
    Moderator

    Another way of doing it would be via PowerShell Remoting and to return a custom object.

    $servers = Get-Content "C:\Users\servers.txt"
    
    $result = Invoke-Command -ComputerName $servers -ScriptBlock {
      $reg= 'HKLM:\SYSTEM\CurrentControlSet\Control'
      [PSCustomObject] @{
        ComputerName = $env:COMPUTERNAME
        Value = (Get-ItemProperty -path $reg -name SystemBootDevice).SystemBootDevice
      }
    }
    $result
    
  • #69795
    Profile photo of Uzair0229
    Uzair0229
    Participant

    PS remoting is not enable i had try the scrip below and i am getting the error, Can anyone help on this

    $computers = gc "C:\Servers.txt"

    foreach ($Computer in $Computers)
    {
    $computerSystem = get-wmiobject Win32_ComputerSystem -Computer $Computer

    $machinename = $computer

    $key = 'HKLM:\SYSTEM\CurrentControlSet\Control\Lsa'

    $valuename = ("Packages").'Packages'

    $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $machinename)

    $regkey = $reg.opensubkey($key)

    $regkey.getvalue($valuename)
    }

    $Results = $MachineName , $keyValue

    Write-host $Results

    You cannot call a method on a null-valued expression.
    At line:8 char:14
    + $key.GetValue <<<< ($Value) + CategoryInfo : InvalidOperation: (GetValue:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull

You must be logged in to reply to this topic.