简体   繁体   English

使用 PowerShell 验证注册表项

[英]Validate Registry Keys with PowerShell

Trying to create a function that validates values in the registry editor.尝试创建一个 function 来验证注册表编辑器中的值。 The script works fine unless I am trying to find values of 2 different properties with the same path.该脚本工作正常,除非我试图找到具有相同路径的 2 个不同属性的值。 For example, if you comment out the second HKLM path, the script works... but if you leave it in, the script will not work because there is a duplicate path ("HKLM:\SYSTEM\CurrentControlSet\Services\hyperkbd").例如,如果您注释掉第二个 HKLM 路径,则脚本可以工作......但如果您将其保留,则脚本将无法工作,因为存在重复的路径(“HKLM:\SYSTEM\CurrentControlSet\Services\hyperkbd”) . What is the workaround for this?解决方法是什么?

function validate-registry{

    $registrylist = 

    @{ 

    "HKLM:\SYSTEM\CurrentControlSet\Services\hyperkbd" = "ErrorControl","0";
    "HKLM:\SYSTEM\CurrentControlSet\Services\hyperkbd" = "Start","3";

     }

    foreach ($registry in $registrylist.Keys)
    { 
        $namevaluearray = $registrylist[$registry]
        $name = $namevaluearray[0]
        $value = $namevaluearray[1]

        if( 
        (Get-ItemProperty $registry -name $name).($name) -eq $value )

        {

        write-host $name "is set to the correct value."

        }
        else
        { 

        write-host "The value of" $namevaluearray "is INCORRECT."

        }

    } 
}

You could use nested hashtables to store 1-to-many relationships:您可以使用嵌套哈希表来存储一对多关系:

function Validate-Registry {

    $registrylist = @{
        "HKLM:\SYSTEM\CurrentControlSet\Services\hyperkbd" = @{
            "ErrorControl" = "0"
            "Start" = "3"
        }
    }

    foreach ($registry in $registrylist.Keys) { 
        foreach($valueName in $registrylist[$registry].Keys){
            $value = $registrylist[$registry][$valueName]

            if ((Get-ItemPropertyValue -LiteralPath $registry -Name $valueName) -eq $value)
            {
                write-host $valueName "is set to the correct value."
            }
            else { 
                write-host "The value of" $valueName "is INCORRECT."
            }
        }
    } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM