Ying Li at myITforum.com

PowerShell & System Center

December 2008 - Posts

PowerShell script to get remote registry key property

The first half of the below script is a modified version of my previous post, it just translate the BITS version to the popular format we know and remember. The second part of the script is to use a get-remoteregistrykeyproperty.ps1 written by Lee Holmes in his Windows PowerShell Cookbook. I believe that's so far the cleanest way to get key value from remote registry. Of course the hard work was taken care by Lee!

$erroractionpreference = "SilentlyContinue"

$a = New-Object -comobject Excel.Application
$a.visible = $True

$b = $a.Workbooks.Add()
$c = $b.Worksheets.Item(1)

$c.Cells.Item(1,1) = "Machine Name"
$c.Cells.Item(1,2) = "Ping Status"
$c.Cells.Item(1,3) = "Operating System"
$c.Cells.Item(1,4) = "Version"
$c.Cells.Item(1,5) = "Disk TimeOutValue"

$c.Cells.Item(1,6) = "Report Time Stamp"

$d = $c.UsedRange
$d.Interior.ColorIndex = 19
$d.Font.ColorIndex = 11
$d.Font.Bold = $True

$intRow = 2

$regPath= "HKLM:\System\CurrentControlSet\Services\Disk"

$colComputers = gc c:\myworkspace\ServerList.txt

foreach ($strComputer in $colComputers)
{
$c.Cells.Item($intRow,1) = $strComputer.ToUpper()

Function PingComputer
{
$ping = new-object System.Net.NetworkInformation.Ping
$Reply = $ping.send($strComputer)
if($Reply.status –eq “Success”)
{
$c.Cells.Item($intRow, 2) = “Online”

Function GetFileInfo
{
$OS = (gwmi -class Win32_OperatingSystem -computer $strComputer).Caption
$c.Cells.Item($intRow,3) = $OS

$OSVersion = (gwmi -class Win32_OperatingSystem -computer $strComputer).version
if ($OSVersion -le 5.1)
{
$Path = "\\"+ $strComputer + "\C$\Winnt\System32\qmgr.dll"
}
else
{
$Path = "\\"+ $strComputer + "\C$\Windows\System32\qmgr.dll"
}

$File = get-item $Path
$BitVer = $File.VersionInfo.Productversion
$BitVer = $BitVer.split('.')[0]+'.'+ $BitVer.split('.')[1]

Switch($BitVer)
{
7.0 {$BitVer = "BITS 3.0"}
6.7 {$BitVer = "BITS 2.5"}
6.6 {$BitVer = "BITS 2.0"}
6.5 {$BitVer = "BITS 1.5"}
}

$c.Cells.Item($intRow,4) = $BitVer
}

GetFileInfo

$DTV = .\get-remoteregistrykeyproperty.ps1 $strcomputer $regpath "TimeoutValue"

$c.Cells.Item($intRow,5) = "{0:X}" -f $DTV.TimeOutValue
}
else
{
$c.Cells.Item($intRow, 2).Interior.ColorIndex = 3
$c.Cells.Item($intRow, 2) = "Offline"
}
}
PingComputer

$c.Cells.Item($intRow,6) = Get-date

$ping.status = $null
$intRow = $intRow + 1
}

$d.EntireColumn.AutoFit()

You can modify the regpath and key value to suite your need. Also Below is the full script copied from Lee Holmes book(or web link):

##############################################################################
##
## Get-RemoteRegistryKeyProperty.ps1
##
## Get the value of a remote registry key property
##
## ie:
##
##  PS >$registryPath =
##       "HKLM:\software\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell"
##  PS >Get-RemoteRegistryKeyProperty LEE-DESK $registryPath "ExecutionPolicy"
##
##############################################################################

param(
  $computer = $(throw "Please specify a computer name."),
  $path = $(throw "Please specify a registry path"),
  $property = "*"
  )

## Validate and extract out the registry key
if($path -match "^HKLM:\\(.*)")
{
    $baseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey(
        "LocalMachine", $computer)
}
elseif($path -match "^HKCU:\\(.*)")
{
    $baseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey(
        "CurrentUser", $computer)
}
else
{
    Write-Error ("Please specify a fully-qualified registry path " +
        "(i.e.: HKLM:\Software) of the registry key to open.")
    return
}

## Open the key
$key = $baseKey.OpenSubKey($matches[1])
$returnObject = New-Object PsObject

## Go through each of the properties in the key
foreach($keyProperty in $key.GetValueNames())
{
    ## If the property matches the search term, add it as a
    ## property to the output
    if($keyProperty -like $property)
    {
        $returnObject |
            Add-Member NoteProperty $keyProperty $key.GetValue($keyProperty)
    }
}

## Return the resulting object
$returnObject

## Close the key and base keys
$key.Close()
$baseKey.Close()