PowerShell for Windows Admins

Mar 10 2012   6:03AM GMT

Migrating to CIM-part3

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

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

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: