Checking or comparing a multi-string registry value with a desired value

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of random commandline random commandline 9 months ago.

  • Author
    Posts
  • #35882
    Profile photo of Felix2000
    Felix2000
    Participant

    I'm trying to write a script to check or compare a multi-string registry value against a desired value.

    Example: Under HKLM:System\CurrentControlSet\Control\SecurePipeServers\Winreg\AllowedExactPaths

    subkey= Machine

    The value is:
    System\CurrentControlSet\Control\ProductOptions
    System\CurrentControlSet\Control\Server Applications
    Software\Microsoft\Windows NT\CurrentVersion

    How do I ensure that is the value.

    I've tried this, but haven't had any luck:

    $key = 'HKLM:\System\CurrentControlSet\Control\SecurePipeServers\Winreg\AllowedExactPaths'
    $Machine = @(
    'System\CurrentControlSet\Control\ProductOptions',
    'System\CurrentControlSet\Control\Server Applications',
    'Software\Microsoft\Windows NT\CurrentVersion'
    )

    if (Test-Path $key)
    {

    # Set the location for checking
    Set-Location $key

    $item = Get-ItemProperty .

    if($item.Machine -eq $Machine)
    {
    "Correct Settings" | Out-File -Append c:\temp\Checks.txt
    }
    else
    {
    "Open" | Out-File -Append c:\temp\Checks.txt
    }
    }
    else
    {
    "Open – Key does not exist" | Out-File -Append c:\temp\Checks.txt
    }

  • #35883
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    You can't directly compare two collections like you're trying to do. You need something like this
    $Machine = @(
    'System\CurrentControlSet\Control\ProductOptions',
    'System\CurrentControlSet\Control\Server Applications',
    'Software\Microsoft\Windows NT\CurrentVersion'
    )

    $p = Get-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\SecurePipeServers\Winreg\AllowedExactpaths' -Name Machine

    if ( -not (Compare-Object $machine $p.machine)){'yay'}else('nay'}

  • #35884
    Profile photo of random commandline
    random commandline
    Participant

    This will verify if any value in your array exist in the registry path. I added a few nonexistent values to your array as a test.

    # Desired Array to Compare
    $values = @"
    System\CurrentControlSet\Control\ProductOptions
    System\CurrentControlSet\Control\Server Applications
    Software\Microsoft\Windows NT\CurrentVersion
    Software\Microsoft\Windows NT\PlaceHolder1
    Software\Microsoft\Windows NT\PlaceHolder2
    "@ -split "`n"
    
    # Check Registry Key
    $regkey = 'HKLM:\SYSTEM\CurrentControlSet\Control\SecurePipeServers\winreg\AllowedExactPaths'
    $keyobj = Get-ItemProperty -Path $regkey
    $checks = 'c:\temp\Checks.txt'
    switch ($values){
    {$keyobj.Machine -contains $_}{"[$_] Is Correct" | Out-File  $checks -Append}
    Default {"[$_] Does Not Exist" | Out-File  $checks -Append}
    }
    
  • #35889
    Profile photo of Felix2000
    Felix2000
    Participant

    @Richard Siddaway
    That did it! Thanks!
    @random comandline
    I tried your method as well, but was getting the following when removing the nonexistent values :
    [System\CurrentControlSet\Control\ProductOptions
    ] Does Not Exist
    [System\CurrentControlSet\Control\Server Applications
    ] Does Not Exist
    [Software\Microsoft\Windows NT\CurrentVersion] Is Correct

    when removing the nonexistent values

  • #35902
    Profile photo of random commandline
    random commandline
    Participant

    Yes, it compares the registry values with the values you have listed.
    [Software\Microsoft\Windows NT\CurrentVersion] exists in registry, but the other two do not.
    Is this not the comparison you were looking for? I am confused.

You must be logged in to reply to this topic.