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