Monthly Archive

Migrating to CIM–doh

Just had the blinding glimpse of the obvious and if I use a default parameter set my problem is solved

function get-CIMRegKeyName{             
param (             
 [ValidateSet("HKCR", "HKCU", "HKLM", "HKUS", "HKCC")]            
 [string]$key = "SYSTEM\CurrentControlSet\Services",            
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) {            
 "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            
Displays subkey names for a registry key

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

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

The registry key - without the hive name e.g.

.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

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

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

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

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




Leave a Reply