Migrating to CIM-part3

The last part of the jigsaw is to wrap the CIM functionality in a function

function get-CIMRegKeyName{             
[CmdletBinding()]             
param (             
 [ValidateSet("HKCR", "HKCU", "HKLM", "HKUS", "HKCC")]            
 [string]$hive="HKLM",            
            
 [string]$key = "SYSTEM\CurrentControlSet\Services",            
            
 [parameter(ParameterSetName="UseLocal")]             
 [switch]$local,            
              
 [parameter(ParameterSetName="UseComputer")]             
 [string]$computer,            
             
 [parameter(ParameterSetName="UseCIMSession")]             
 [Microsoft.Management.Infrastructure.CimSession]$cimsession            
             
)             
BEGIN{}#begin             
PROCESS{            
            
switch ($hive){            
"HKCR" { [uint32]$hdkey = 2147483648} #HKEY_CLASSES_ROOT            
"HKCU" { [uint32]$hdkey = 2147483649} #HKEY_CURRENT_USER            
"HKLM" { [uint32]$hdkey = 2147483650} #HKEY_LOCAL_MACHINE            
"HKUS" { [uint32]$hdkey = 2147483651} #HKEY_USERS            
"HKCC" { [uint32]$hdkey = 2147483653} #HKEY_CURRENT_CONFIG            
}            
            
switch ($psCmdlet.ParameterSetName) {            
 "UseLocal"    {$result = Invoke-CimMethod -Namespace "root\cimv2" -ClassName StdRegProv -MethodName EnumKey  -Arguments @{hDefKey=$hdkey; sSubKeyName =$key} }            
 "UseComputer"    {$result = Invoke-CimMethod -Namespace "root\cimv2" -ClassName StdRegProv -MethodName EnumKey  -Arguments @{hDefKey=$hdkey; sSubKeyName =$key} -ComputerName $computer}            
 "UseCIMSession"  {$result = Invoke-CimMethod -Namespace "root\cimv2" -ClassName StdRegProv -MethodName EnumKey  -Arguments @{hDefKey=$hdkey; sSubKeyName =$key} -CimSession $cimsession }            
 default {Write-Host "Error!!! Should not be here" }            
}            
 $result | select -ExpandProperty sNames            
            
}#process             
END{}#end            
            
<# 
.SYNOPSIS
Displays subkey names for a registry key

.DESCRIPTION
Displays subkey names for a registry key using WSMAN or DCOM 
to access remote machines 

.PARAMETER  hive
Hive Name. One of "HKCR", "HKCU", "HKLM", "HKUS" or "HKCC"
The name is validated against the set

.PARAMETER  key
The registry key - without the hive name e.g.
"SYSTEM\CurrentControlSet\Services"

.PARAMETER  computer
Indicates to use the local machine

.PARAMETER  computer
Name of a remote computer. Connectivity will be by WSMAN.

.PARAMETER  cimsession
An object representing a cimsession. Connectivity is controlled 
by the CIM session and can be WSMAN or DCOM

.EXAMPLE
get-CIMRegKeyName -hive HKLM -key "SOFTWARE\Microsoft" -local

.EXAMPLE                                                                                       
get-CIMRegKeyName -hive HKLM -key "SOFTWARE\Microsoft" -computer "."

.EXAMPLE
get-CIMRegKeyName -hive HKLM -key "SOFTWARE\Microsoft" -computer "server8beta"  

.EXAMPLE
$cs = New-CimSession -ComputerName server8beta   
get-CIMRegKeyName -hive HKLM -key "SOFTWARE\Microsoft" -cimsession $cs      

.EXAMPLE
$opt = New-CimSessionOption -Protocol Dcom                                                                                                          
$csd = New-CimSession -ComputerName server02 -SessionOption $opt                                                                                    
get-CIMRegKeyName -hive HKLM -key "SOFTWARE\Microsoft" -cimsession $csd  

.NOTES


.LINK

#>            
            
}

I’ve added comment based help just for completeness.

The function takes a hive and a key as parameters. I’ve given safe defaults that should be available on all machines – if they aren’t you are probably in trouble.

We can then use a switch to indicate the local machine, or give a computer name or a cim session object.

Depending on the hive the correct numeric value is set and then invoke-cimmethod is called using a computername or cim session as required

I’m not totally happy with having to use the –local switch so I’ll have another think on that

This entry was posted in PowerShell and WMI, PowerShell V3. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s