Remote Registry with Powershell (PLUS: Create-ListBox function)
Until the powers of remoting arrive in Powershell v2, accessing a remote registry is a bit more cumbersome than a local registry. As you no doubt know, Powershell allows you to browse through the local registry as if it was a filesystem. The folling function should help you with accessing a remote registry:
function Enumerate-RemoteRegistryValues
{
$ServerName = Read-Host “Server”
$Hives = `
“ClassesRoot”,”CurrentConfig”,”CurrentUser”,`
“DynData”,”LocalMachine”,”PerformanceData”,”Users”
$Hive = Create-ListBox -Items $Hives -Title “Select Registry Hive”`
-Text “Select a remote registry hive to connect to:”
$baseKeyName = Read-Host “Enter Key Name”
$registry = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey(`
[Microsoft.Win32.RegistryHive]::$Hive, $serverName)
$baseKey = $registry.OpenSubKey($baseKeyName)
$ValueNames = $baseKey.GetValueNames()
$myCol = @()
ForEach ($ValueName in $ValueNames)
{
$myObj = “” | Select Name, Value
$myObj.Name = $ValueName
$myObj.Value = $baseKey.GetValue($ValueName)
$myCol += $myObj
}
return $myCol
}
Oh yeah!
One thing though. The real fanatics should have spotted it by now: The cmdlet Create-ListBox is not a native Powershell cmdlet. If fact, it is a function I created. It allows the user of the script to select a value from a collection of values using a sleek List Box. You should load this function before using the one above. Here’s how I did it:
# Allows you to create a listbox to let a user choose one of several values
function Create-ListBox
{
param ([array]$Items,[string]$Title = “ListBox”,[string]$Text = “Please make your selection”)
If ($Items -eq $null){Write-Warning ‘Argument “Items” is mandatory.’;throw “Syntax error.”}[System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”) | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName(“System.Drawing”) | Out-Null
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = $Title
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = “CenterScreen”
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq “Enter”)
{$x=$objListBox.SelectedItem;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq “Escape”)
{$objForm.Close()}})
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = “OK”
$OKButton.Add_Click({$x=$objListBox.SelectedItem;$objForm.Close()})
$objForm.Controls.Add($OKButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = “Cancel”
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(280,20)
$objLabel.Text = $Text
$objForm.Controls.Add($objLabel)$objListBox = New-Object System.Windows.Forms.ListBox
$objListBox.Location = New-Object System.Drawing.Size(10,40)
$objListBox.Size = New-Object System.Drawing.Size(260,20)
$objListBox.Height = 80ForEach ($item in $Items)
{
[void] $objListBox.Items.Add($item)
}$objForm.Controls.Add($objListBox)
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()return $x
}
No related posts.
2 Responses to Remote Registry with Powershell (PLUS: Create-ListBox function)
Tags
Active Directory API bind order cleanup cluster CPU Custom Fields datastores description device management directory tree errors Event Log file name filter Fun function HA IT known issues License Server LUN multipath NIC objects Oneliner portgroups PowerCLI PowerShell profile recursive Registry Scripts security session share snapshots SQL Stat VI Toolkit VMware vSphere WMI WSUS ZenArchives
- July 2012
- July 2011
- February 2011
- January 2011
- December 2010
- May 2010
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008





[...] to add If you want to pursue this using some Powershell scripting there’s a sample script on this blog post by Hugo Peeters that shows how to connect to a remote registry but you will have to figure out how [...]
[...] to add If you want to pursue this using some Powershell scripting there’s a sample script on this blog post by Hugo Peeters that shows how to connect to a remote registry but you will have to figure out how [...]