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
- param ($RegistryKeyPath, $KeyName, $KeyValue, $Debug)
- if ($debug -ne "true"){$debug = [bool]$false}else{$debug = [bool]$true}
- $Script:API = new-object -comObject "MOM.ScriptAPI"
- $Script:Bag = $Script:API.CreatePropertyBag()
- $Script:LOG_ERROR = 1
- $Script:LOG_WARNING = 2
- $Script:LOG_INFORMATION = 4
- $Script:ScriptName = "ReadRegistryKey.ps1"
- $Script:Arguments = "Received Arguments:`RegistryKeyPAth = $RegistryKeyPath `rKeyName = $KeyName `rDebug = $debug"
- #------------------------------------------------------------------------------------------
- Function CheckIfRegistryKeyExists($KeyPath,$KeyName)
- {
- $KeytoRead = $KeyPath
- $KeytoReadName = $KeyName
- $a = (Get-ItemProperty -path registry::$KeytoRead).$KeytoReadName -eq $null
- if ($a -eq "false")
- {
- $Script:API.LogScriptEvent("CheckIfRegictyryKeyExists",200,$Script:LOG_ERROR,"`r$KeyPath\$KeyName doesn't exist !")
- }
- else
- {
- $Script:API.LogScriptEvent("CheckIfRegictyryKeyExists",200,$Script:LOG_INFORMATION,"`r$KeyPath\$KeyName exists.")
- }
- }
- #------------------------------------------------------------------------------------------
- function ReadRegistryKey($KeyPath,$KeyName)
- {
- $Script:API.LogScriptEvent("$ScriptName",200,$Script:LOG_INFORMATION,"`r$KeyPath\$KeyName ")
- $KeytoRead = $KeyPath
- $KeytoReadValue = $KeyName
- $a = Get-ItemProperty -path registry::$KeytoRead -name $KeytoReadValue
- $ReadValue = $a.$KeytoReadValue
- $ReadValue
- }
- #------------------------------------------------------------------------------------------
- function WriteRegistryKey($KeyPath,$KeyName,$KeyValue)
- {
- $Script:API.LogScriptEvent("$ScriptName",200,$Script:LOG_INFORMATION,"`r$KeyPath\$KeyName with value $KeyValue is created")
- $KeytoRead = $KeyPath
- $KeytoReadValue = $KeyName
- $Value = $KeyValue
- $a = Set-ItemProperty -path registry::$KeytoRead -name $KeytoReadValue -Value $Value
- $a.$KeytoReadValue
- }
- #------------------------------------------------------------------------------------------
- function Write-DebugInfo([string] $msg)
- {
- if ($debug)
- {
- $Script:API.LogScriptEvent("$ScriptName",100,$Script:LOG_INFORMATION,"`r$Arguments`r`r$msg")
- }
- }
- #------------------------------------------------------------------------------------------
- function Write-ErrorInfo([string] $msg)
- {
- if ($debug)
- {
- $Script:API.LogScriptEvent("$ScriptName",500,$Script:LOG_ERROR,"`r$Arguments`r`r$msg")
- }
- }
- #------------------------------------------------------------------------------------------
- # MAIN CODE START HERE
- #------------------------------------------------------------------------------------------
- # Use this line to Read the registry key
- ReadRegistryKey $RegistryKeyPath $KeyName
- # Use this line to Create the registry key with its value
- WriteRegistryKey $RegistryKeyPath $KeyName $KeyValue
- # Use this line to check if key exist
- CheckIfRegistryKeyExists $RegistryKeyPath $KeyName
> 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 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
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 :
And also Key exists :
This posting is provided "AS IS" with no warranties.
No comments:
Post a Comment