Powershell and remote registry

  • I'm running into an issue where I can't access the following registry key remotely, either via Powershell or xp_regread. I can see it in Regedit or with powershell when I'm logged onto the box. This isn't on all servers but it does happen on the ones I'm more interested in. If I back the key up to "Microsoft SQL Server" I can get sub keys but not all are listed and it lists a couple that aren't actually there. Any idea what's going on?

    $MachineName = "server"

    $type = [Microsoft.Win32.RegistryHive]::LocalMachine

    $regkey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($type, $MachineName)

    $basePath = "SOFTWARE\\Microsoft\\Microsoft SQL Server\\Instance Names\\SQL"

    $regKey = $regKey.OpenSubKey($basePath)

    $regKey.GetSubKeyNames()

    Foreach($val in $regKey.GetValueNames()){

    Write-Host $val.PadRight(30) -nonewline

    Write-Host $regKey.GetValue("$val")

    }

  • Check the "Remote Registry Service" on those machines you are not able to access, see if it was turned off / disabled.

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • It's started. It's not that I can't access them at all, it's that I'm not seeing all keys and that I'm seeing some keys that shouldn't be there. I've confirmed the path (by copying an pasting the key name from Regedit) so I know I'm in the right place.

  • I tried your code against a few of my servers (Server 2008R2 w/ SQL 2008 instances and Server 2003R2 w/ SQL 2005) and it worked fine (mostly, see below), although I found the call to GetSubKeyNames() to be extraneous.

    Shot in the dark here...I have one oddball server in my environment that the code failed on. It has 32-bit SQL Server running on 64-bit Windows and the Instance Names key is here instead of the usual location:

    HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Microsoft SQL Server\Instance Names\SQL

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • That was it. Kind of. When running powershell from my machine (32-bit) I was getting redirected to the WOW6432Node key. So it would appear I need to either convince my boss to get me an x64 desktop. Or find a way to access the x64 registry (which there is some info out there on) or just run the script from a 64-bit server. Thanks for the help.

  • Just to throw another wrinkle out there, I am on a 32-bit machine and I had no trouble accessing the Instance Names key on a 64-bit Windows server running 64-bit SQL Server using your exact code so I guess I was not being redirected.

    That said, I see there is an overload for OpenRemoteBaseKey that allows us to specify the RegistryView mode but it was introduced in .NET 4.0 so I could not access it from my script.

    RegistryKey.OpenRemoteBaseKey Method (RegistryHive, String, RegistryView)

    Go for a new workstation 😉

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply