Adding functionality to not only edit Values, but also Keys.

This commit is contained in:
Adam Keech 2015-07-06 15:25:01 -04:00 committed by Matt Clay
parent 1710d1aa0c
commit b365b83906
2 changed files with 85 additions and 60 deletions

View file

@ -25,13 +25,22 @@ $params = Parse-Args $args;
$result = New-Object PSObject;
Set-Attr $result "changed" $false;
If ($params.name)
If ($params.key)
{
$registryValueName = $params.name
$registryKey = $params.key
}
Else
{
Fail-Json $result "missing required argument: name"
Fail-Json $result "missing required argument: key"
}
If ($params.value)
{
$registryValue = $params.value
}
Else
{
$registryValue = $null
}
If ($params.state)
@ -49,16 +58,16 @@ Else
If ($params.data)
{
$registryValueData = $params.data
$registryData = $params.data
}
ElseIf ($state -eq "present")
ElseIf ($state -eq "present" -and $registryValue -ne $null)
{
Fail-Json $result "missing required argument: data"
}
If ($params.type)
If ($params.datatype)
{
$registryDataType = $params.type.ToString().ToLower()
$registryDataType = $params.datatype.ToString().ToLower()
$validRegistryDataTypes = "binary", "dword", "expandstring", "multistring", "string", "qword"
If ($validRegistryDataTypes -notcontains $registryDataType)
{
@ -70,15 +79,6 @@ Else
$registryDataType = "string"
}
If ($params.path)
{
$registryValuePath = $params.path
}
Else
{
Fail-Json $result "missing required argument: path"
}
Function Test-RegistryValueData {
Param (
[parameter(Mandatory=$true)]
@ -96,16 +96,17 @@ Function Test-RegistryValueData {
}
if($state -eq "present") {
if (Test-Path $registryValuePath) {
if (Test-RegistryValueData -Path $registryValuePath -Value $registryValueName)
if ((Test-Path $registryKey) -and $registryValue -ne $null)
{
if (Test-RegistryValueData -Path $registryKey -Value $registryValue)
{
# Changes Type and Value
If ((Get-Item $registryValuePath).GetValueKind($registryValueName) -ne $registryDataType)
# Changes Data and DataType
if ((Get-Item $registryKey).GetValueKind($registryValue) -ne $registryDataType)
{
Try
{
Remove-ItemProperty -Path $registryValuePath -Name $registryValueName
New-ItemProperty -Path $registryValuePath -Name $registryValueName -Value $registryValueData -PropertyType $registryDataType
Remove-ItemProperty -Path $registryKey -Name $registryValue
New-ItemProperty -Path $registryKey -Name $registryValue -Value $registryData -PropertyType $registryDataType
$result.changed = $true
}
Catch
@ -113,11 +114,11 @@ if($state -eq "present") {
Fail-Json $result $_.Exception.Message
}
}
# Only Changes Value
ElseIf ((Get-ItemProperty -Path $registryValuePath | Select-Object -ExpandProperty $registryValueName) -ne $registryValueData)
# Changes Only Data
elseif ((Get-ItemProperty -Path $registryKey | Select-Object -ExpandProperty $registryValue) -ne $registryData)
{
Try {
Set-ItemProperty -Path $registryValuePath -Name $registryValueName -Value $registryValueData
Set-ItemProperty -Path $registryKey -Name $registryValue -Value $registryData
$result.changed = $true
}
Catch
@ -130,7 +131,7 @@ if($state -eq "present") {
{
Try
{
New-ItemProperty -Path $registryValuePath -Name $registryValueName -Value $registryValueData -PropertyType $registryDataType
New-ItemProperty -Path $registryKey -Name $registryValue -Value $registryData -PropertyType $registryDataType
$result.changed = $true
}
Catch
@ -139,12 +140,17 @@ if($state -eq "present") {
}
}
}
else
elseif(-not (Test-Path $registryKey))
{
Try
{
New-Item $registryValuePath -Force | New-ItemProperty -Name $registryValueName -Value $registryValueData -Force -PropertyType $registryDataType
$newRegistryKey = New-Item $registryKey -Force
$result.changed = $true
if($registryValue -ne $null) {
$newRegistryKey | New-ItemProperty -Name $registryValue -Value $registryData -Force -PropertyType $registryDataType
$result.changed = $true
}
}
Catch
{
@ -154,12 +160,23 @@ if($state -eq "present") {
}
else
{
if (Test-Path $registryValuePath)
if (Test-Path $registryKey)
{
if (Test-RegistryValueData -Path $registryValuePath -Value $registryValueName) {
if ($registryValue -eq $null) {
Try
{
Remove-ItemProperty -Path $registryValuePath -Name $registryValueName
Remove-Item -Path $registryKey -Recurse
$result.changed = $true
}
Catch
{
Fail-Json $result $_.Exception.Message
}
}
elseif (Test-RegistryValueData -Path $registryKey -Value $registryValue) {
Try
{
Remove-ItemProperty -Path $registryKey -Name $registryValue
$result.changed = $true
}
Catch
@ -171,4 +188,3 @@ else
}
Exit-Json $result

View file

@ -25,11 +25,17 @@ DOCUMENTATION = '''
---
module: win_regedit
version_added: "2.0"
short_description: Add, Edit, or Remove Registry Value
short_description: Add, Edit, or Remove Registry Keys and Values
description:
- Add, Edit, or Remove Registry Value using ItemProperties Cmdlets
- Add, Edit, or Remove Registry Keys and Values using ItemProperties Cmdlets
options:
name:
key:
description:
- Name of Registry Key
required: true
default: null
aliases: []
value:
description:
- Name of Registry Value
required: true
@ -41,7 +47,7 @@ options:
required: false
default: null
aliases: []
type:
datatype:
description:
- Registry Value Data Type
required: false
@ -54,12 +60,6 @@ options:
- qword
default: string
aliases: []
path:
description:
- Path of Registry Value
required: true
default: null
aliases: []
state:
description:
- State of Registry Value
@ -73,28 +73,37 @@ author: "Adam Keech (@smadam813), Josh Ludwig (@joshludwig)"
'''
EXAMPLES = '''
# Add Registry Value (Default is String)
# Creates Registry Key called MyCompany.
win_regedit:
name: testvalue
key: HKCU:\Software\MyCompany
# Creates Registry Key called MyCompany,
# a value within MyCompany Key called "hello", and
# data for the value "hello" containing "world".
win_regedit:
key: HKCU:\Software\MyCompany
value: hello
data: world
# Creates Registry Key called MyCompany,
# a value within MyCompany Key called "hello", and
# data for the value "hello" containing "1337" as type "dword".
win_regedit:
key: HKCU:\Software\MyCompany
value: hello
data: 1337
path: HKCU:\Software\MyCompany
datatype: dword
# Add Registry Value with Type DWord
# Delete Registry Key MyCompany
# NOTE: Not specifying a value will delete the root key which means
# all values will be deleted
win_regedit:
name: testvalue
data: 1337
type: dword
path: HKCU:\Software\MyCompany
key: HKCU:\Software\MyCompany
state: absent
# Edit Registry Value called testvalue
# Delete Registry Value "hello" from MyCompany Key
win_regedit:
name: testvalue
data: 8008
path: HKCU:\Software\MyCompany
# Remove Registry Value called testvalue
win_regedit:
name: testvalue
path: HKCU:\Software\MyCompany
key: HKCU:\Software\MyCompany
value: hello
state: absent
'''