Categories

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{             
[CmdletBinding(DefaultParameterSetName="UseComputer")]             
            
param (             
 [ValidateSet("HKCR", "HKCU", "HKLM", "HKUS", "HKCC")]            
 [string]$hive="HKLM",            
            
 [string]$key = "SYSTEM\CurrentControlSet\Services",            
            
 [parameter(ParameterSetName="UseComputer")]             
 [string]$computer="$env:COMPUTERNAME",            
             
 [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) {            
 "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" -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

#>            
            
}

Leave a Reply