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 = 80

ForEach ($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
}

Tagged with:
 

2 Responses to Remote Registry with Powershell (PLUS: Create-ListBox function)

  1. […] 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 […]

  2. […] 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 […]

Leave a Reply