Notes of a scripter

Find Registry Keys on Remote Servers

I was tasked with writing a script to find a single registry key on all of the server for a domain that my team manages. So after getting the key that we need to know the value of, I put a script together. The script will first check to see if the server is online, and if it is then the scripts looks at the registry to find the key and records the value.  Then generates a report of the keys that were found.

$reportReg = @()
$Computers = Get-Content C:\scripts\logs\ServersToFindRegistryKeyOn.txt
Foreach ($Computer in $computers){
    If(Test-Connection -ComputerName $Computer -Count 1 -ErrorAction 0){
            Try{
                # This is were the registry key is looked for on the remote server
                $RegLine = "" | Select ComputerName, RegistryKey
                $objReg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $Computer)
                $objRegKey= $objReg.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\Lsa")
                $RegLine.ComputerName = $Computer
                $RegLine.Registrykey = $objRegkey.GetValue("lmcompatibilitylevel")
                $reportReg += $RegLine
            }
            Catch{
                Write-Warning "Unable to reach $Computer, adding to bad list to look at later."
                $Computer | Add-Content C:\scripts\logs\Unreachable.txt
                Continue
            }
    }
}
$reportReg | Export-Csv C:\scripts\Logs\RegistryValue.csv

The beauty of this script, is that it to took about as much time to find the information on the first server as it did to write this script.  So with needing to find this information on 40+ servers, it is well worth writing.  The rule of thumb, if you have to do it more than twice, script it.

 

– Stuart

Exit mobile version