Wednesday, February 1, 2012

[Powershell] Playing with registry keys in powershell

I've had to read and write some registry keys from my management pack to implement a functionnality for debugging purpose - all events are created in in the Operations Manager Event log.

Here are the differents functions that can be usefull :
  • CheckIfRegistryKeyExists : create event 200 (Information or Error) depending of the result
  • ReadRegistryKey : Read the key value and write information event 200
  • WriteRegistryKey : Write the key value and write information event 200
  • Write-ErrorInfo : create error event 500
  • Write-DebugInfo : create Information event 100

Copy the code below in a  ReadRegistryKey.ps1 file for example

  1. param ($RegistryKeyPath, $KeyName, $KeyValue, $Debug)
  2. if ($debug -ne "true"){$debug = [bool]$false}else{$debug = [bool]$true}
  3. $Script:API             = new-object -comObject "MOM.ScriptAPI"
  4. $Script:Bag             = $Script:API.CreatePropertyBag()
  5. $Script:LOG_ERROR       = 1
  6. $Script:LOG_WARNING     = 2
  7. $Script:LOG_INFORMATION = 4
  8. $Script:ScriptName      = "ReadRegistryKey.ps1"
  9. $Script:Arguments       = "Received Arguments:`RegistryKeyPAth = $RegistryKeyPath `rKeyName = $KeyName `rDebug = $debug"
  10. #------------------------------------------------------------------------------------------
  11. Function CheckIfRegistryKeyExists($KeyPath,$KeyName)
  12. {
  13.  $KeytoRead = $KeyPath
  14.  $KeytoReadName = $KeyName
  15.  $a = (Get-ItemProperty -path registry::$KeytoRead).$KeytoReadName -eq $null
  16.  if ($a -eq "false")
  17.   {
  18.   $Script:API.LogScriptEvent("CheckIfRegictyryKeyExists",200,$Script:LOG_ERROR,"`r$KeyPath\$KeyName doesn't exist !")
  19.   }
  20.  else
  21.   {
  22.   $Script:API.LogScriptEvent("CheckIfRegictyryKeyExists",200,$Script:LOG_INFORMATION,"`r$KeyPath\$KeyName exists.")
  23.   }
  24. }
  25. #------------------------------------------------------------------------------------------
  26. function ReadRegistryKey($KeyPath,$KeyName)
  27. {
  28.     $Script:API.LogScriptEvent("$ScriptName",200,$Script:LOG_INFORMATION,"`r$KeyPath\$KeyName ")
  29.  $KeytoRead = $KeyPath
  30.  $KeytoReadValue = $KeyName
  31.  $a = Get-ItemProperty -path registry::$KeytoRead -name $KeytoReadValue
  32.  $ReadValue = $a.$KeytoReadValue
  33.  $ReadValue
  34. }
  35. #------------------------------------------------------------------------------------------
  36. function WriteRegistryKey($KeyPath,$KeyName,$KeyValue)
  37. {
  38.  $Script:API.LogScriptEvent("$ScriptName",200,$Script:LOG_INFORMATION,"`r$KeyPath\$KeyName with value $KeyValue is created")
  39.  $KeytoRead = $KeyPath
  40.  $KeytoReadValue = $KeyName
  41.  $Value = $KeyValue
  42.  $a = Set-ItemProperty -path registry::$KeytoRead -name $KeytoReadValue -Value $Value
  43.  $a.$KeytoReadValue
  44. }
  45. #------------------------------------------------------------------------------------------
  46. function Write-DebugInfo([string] $msg)
  47. {
  48.     if ($debug)
  49.     {
  50.         $Script:API.LogScriptEvent("$ScriptName",100,$Script:LOG_INFORMATION,"`r$Arguments`r`r$msg")
  51.     }
  52. }
  53. #------------------------------------------------------------------------------------------
  54. function Write-ErrorInfo([string] $msg)
  55. {
  56.     if ($debug)
  57.     {
  58.     $Script:API.LogScriptEvent("$ScriptName",500,$Script:LOG_ERROR,"`r$Arguments`r`r$msg")
  59.  }
  60. }
  61. #------------------------------------------------------------------------------------------
  62. # MAIN CODE START HERE
  63. #------------------------------------------------------------------------------------------
  64. # Use this line to Read the registry key
  65. ReadRegistryKey $RegistryKeyPath $KeyName
  66. # Use this line to Create the registry key with its value
  67. WriteRegistryKey $RegistryKeyPath $KeyName $KeyValue
  68. # Use this line to check if key exist
  69. CheckIfRegistryKeyExists $RegistryKeyPath $KeyName
Then is a powershell command shell, just launch the ReadRegistryKey.ps1 with the parameters like :
> C:\scripts\ReadRegistryKey.ps1 "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\
Microsoft Operations Manager\Debugging" "ForTestingPurpose" "ValueToSet" "true"

When the HKLM\SOFTWARE\Microsoft\Microsoft Operations Manager\Debugging\ForTestingPurpose key doens't exist :
  • ReadRegistryKey function will return
Case : key tested exists :

Case key doens't exist : To be implemented since the code return error in powershell or just use the CheckRegistryKey function before.

Get-ItemProperty : Property ForTestingPurpose1 does not exist at path HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft Operations Manager\Debugging.
At C:\scripts\ReadRegistryKey.ps1:35 char:23
+     $a = Get-ItemProperty  <<<< -path registry::$KeytoRead -name $KeytoReadValue

  • Write registry key will create the key and return :

  • Checking the registry key can return 2 states :
Key doens't exist


And also Key exists :


 Hope this will be helpful.





This posting is provided "AS IS" with no warranties.

No comments:

Post a Comment