If Else Enumeration of Registry Value

Hello All,

I am trying to enumerate a registry key property value in an If Else statement with the following code but the enumeration does not seem to work for the conditions to be effective:

<code>

$RegistryKey = "hkcu:\software\microsoft\windows\currentversion\explorer\Advanced" #This is the registry path. Please replace the example registry path between the quotes.
$RegistryKeyProperty = "HideFileExt" #This is the registry key property whose value will be remediated if needed. Please replace the property between the quotes.
$RegistryKeyPropertyValue = Get-ItemProperty -Path $RegistryKey -Name $RegistryKeyProperty #This is the value to be remediated if needed. Please replace the example value between the quotes.
$Compliance = "" #The $Compliance variable is set to 'NonCompliant' by default.
#Set-Location $RegistryKey #Navigate to the registry path.
If ($RegistryKeyPropertyValue -eq 0) #Discovery for the registry key property value.
{
$Compliance = "Compliant" #If the discovery check above passes, set the $Compliance variable to 'Compliant'.
Write-Output $Compliance #And output the value of the $Compliance variable for access by SCCM.
}
else
{
Write-Output $Compliance #Otherwise, if the discovery check above fails, set the value of the $Compliance variable to 'NonCompliant' for access by SCCM.
}

</code>

Any idea on how to accomplish this would be great.

Thanks




  • Edited by techiegzz Saturday, February 07, 2015 4:51 PM
February 7th, 2015 7:40pm

if/else does not enumerate. for/while/foreach and the pipeline enumerate.  if/else test something.
Free Windows Admin Tool Kit Click here and download it now
February 7th, 2015 9:14pm

You are not a programmer so keep it simple.  Avoid all of those unnecessary comments and don't overuse variables.

This is all you need:

$HideFileExt=(Get-ItemProperty -Path hkcu:\software\microsoft\windows\currentversion\explorer\Advanced -Name HideFileExt).HideFileExt
If($HideFileExt -eq 0){
    $Compliance='Compliant'
}else{
    $Compliance='NonCompliant'
}
Write-Output $Compliance

Format your code for readability  Look at the Scripting Guys Blog for articles on formatting and commenting.

February 7th, 2015 9:24pm

Thanks guys for the help. jrv, genius solution. I leverage variables because this is simply going to be a template for many Baseline checks via SCCM, so it can be easy for those who need to use it to more cleanly swap out the registry key, property, and value of the variables instead of messing with anything else. I have successfully tested it and it works like a charm. You are right, PowerShell reads quite easily as natural language, so comments are overkill. I need to find the code formatting guide for the website, no luck yet.

  • Marked as answer by techiegzz Saturday, February 07, 2015 10:18 PM
  • Edited by techiegzz Sunday, February 08, 2015 10:03 AM
  • Unmarked as answer by techiegzz Sunday, February 08, 2015 10:03 AM
Free Windows Admin Tool Kit Click here and download it now
February 8th, 2015 1:10am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics