Detection Script
$key = 'HKLM:\KeyPath'
$name = 'KeyName'
$val = '0'
if (Test-Path $key) {
if ((Get-ItemProperty $key).$name -contains $val) {
Write-Output "Key and value exist"
Exit 0
}
Else {
Write-Output "Value not exist"
Exit 1
}
}
Else {
Write-Output "Key and value not exist"
Exit 1
}
Remediation Script
$key = 'HKLM:\KeyPath'
$name = 'KeyName'
$val = '0'
if (Test-Path $key) {
Write-Output "Key exist"
if ((Get-ItemProperty $key).$name -contains $val) {
Write-Output "Key and value exist"
Exit 0
}
Else {
Write-Output "Value not exist"
New-ItemProperty -Path $key -Name $name -value $val -PropertyType DWord -Force
Exit 0
}
}
Else {
Write-Output "Key and Value not exist"
New-Item -Path $key -Force
New-ItemProperty -Path $key -Name $name -value $val -PropertyType DWord -Force
Exit 0
}