测试注册表值是否存在

| 在我的Powershell脚本中,我为运行脚本的每个元素创建一个注册表项,我想在注册表中存储有关每个元素的一些其他信息(如果您指定一次可选参数,那么默认情况下,将来使用这些参数) 。 我遇到的问题是我需要执行Test-RegistryValue(如此处所示),但似乎并不能解决问题(即使存在条目,它也会返回false)。 我试图“在此基础上构建”,我唯一想到的是:
Function Test-RegistryValue($regkey, $name) 
{
    try
    {
        $exists = Get-ItemProperty $regkey $name -ErrorAction SilentlyContinue
        Write-Host \"Test-RegistryValue: $exists\"
        if (($exists -eq $null) -or ($exists.Length -eq 0))
        {
            return $false
        }
        else
        {
            return $true
        }
    }
    catch
    {
        return $false
    }
}
不幸的是,这也无法满足我的需要,因为它似乎总是从注册表项中选择一些(第一个?)值。 有人知道该怎么做吗? 为此编写托管代码似乎太多了……     
已邀请:
就我个人而言,我不喜欢测试功能可能会吐出错误,所以这就是我要做的。此功能还可以用作过滤器,可用于过滤注册表项列表,以仅保留具有特定项的注册表项。
Function Test-RegistryValue {
    param(
        [Alias(\"PSPath\")]
        [Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [String]$Path
        ,
        [Parameter(Position = 1, Mandatory = $true)]
        [String]$Name
        ,
        [Switch]$PassThru
    ) 

    process {
        if (Test-Path $Path) {
            $Key = Get-Item -LiteralPath $Path
            if ($Key.GetValue($Name, $null) -ne $null) {
                if ($PassThru) {
                    Get-ItemProperty $Path $Name
                } else {
                    $true
                }
            } else {
                $false
            }
        } else {
            $false
        }
    }
}
    
Carbon PowerShell模块具有Test-RegistryKeyValue函数,它将为您执行此检查。 (公开:我是Carbon的所有者/维护者。) 首先,您必须检查注册表项是否存在。然后,您必须处理注册表项是否没有值。这里的大多数示例实际上都是在测试值本身,而不是值的存在。如果值为空或为空,则将返回假阴性。相反,您必须测试由
Get-ItemProperty
返回的对象上是否确实存在该值的属性。 这是Carbon模块的当前代码:
function Test-RegistryKeyValue
{
    <#
    .SYNOPSIS
    Tests if a registry value exists.

    .DESCRIPTION
    The usual ways for checking if a registry value exists don\'t handle when a value simply has an empty or null value.  This function actually checks if a key has a value with a given name.

    .EXAMPLE
    Test-RegistryKeyValue -Path \'hklm:\\Software\\Carbon\\Test\' -Name \'Title\'

    Returns `True` if `hklm:\\Software\\Carbon\\Test` contains a value named \'Title\'.  `False` otherwise.
    #>
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]
        # The path to the registry key where the value should be set.  Will be created if it doesn\'t exist.
        $Path,

        [Parameter(Mandatory=$true)]
        [string]
        # The name of the value being set.
        $Name
    )

    if( -not (Test-Path -Path $Path -PathType Container) )
    {
        return $false
    }

    $properties = Get-ItemProperty -Path $Path 
    if( -not $properties )
    {
        return $false
    }

    $member = Get-Member -InputObject $properties -Name $Name
    if( $member )
    {
        return $true
    }
    else
    {
        return $false
    }

}
    
测试注册表值是否存在的最佳方法就是这样做-测试其存在。即使很难阅读,这也是单线。   PS C:>(获取项属性$ regkey).PSObject.Properties.Name-包含   $名称 如果您实际查找其数据,则会遇到Powershell如何解释0的麻烦。     
我将使用函数
Get-RegistryValue
。实际上,它获取请求的值(以便它不仅可以用于测试)。至于注册表值不能为空,我们可以使用空结果作为缺少值的标志。还提供了纯测试功能“ 5”。
# This function just gets $true or $false
function Test-RegistryValue($path, $name)
{
    $key = Get-Item -LiteralPath $path -ErrorAction SilentlyContinue
    $key -and $null -ne $key.GetValue($name, $null)
}

# Gets the specified registry value or $null if it is missing
function Get-RegistryValue($path, $name)
{
    $key = Get-Item -LiteralPath $path -ErrorAction SilentlyContinue
    if ($key) {
        $key.GetValue($name, $null)
    }
}

# Test existing value
Test-RegistryValue HKCU:\\Console FontFamily
$val = Get-RegistryValue HKCU:\\Console FontFamily
if ($val -eq $null) { \'missing value\' } else { $val }

# Test missing value
Test-RegistryValue HKCU:\\Console missing
$val = Get-RegistryValue HKCU:\\Console missing
if ($val -eq $null) { \'missing value\' } else { $val }
输出:
True
54
False
missing value
    
带有空格的字符串可能是一个问题。这是一个适用于我的清理版本:
Function Test-RegistryValue($regkey, $name) {
    $exists = Get-ItemProperty -Path \"$regkey\" -Name \"$name\" -ErrorAction SilentlyContinue
    If (($exists -ne $null) -and ($exists.Length -ne 0)) {
        Return $true
    }
    Return $false
}
    
单线:
$valueExists = (Get-Item $regKeyPath -EA Ignore).Property -contains $regValueName
    
我的版本,与捕获的异常中的确切文本匹配。 如果它是一个不同的异常,它将返回true,但适用于这种简单情况。 PS 5.0中还新增了Get-ItemPropertyValue
Function Test-RegValExists($Path, $Value){
$ee = @() # Exception catcher
try{
    Get-ItemPropertyValue -Path $Path -Name $Value | Out-Null
   }
catch{$ee += $_}

    if ($ee.Exception.Message -match \"Property $Value does not exist\"){return $false}
else {return $true}
}
    
$regkeypath= \"HKCU:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\" 
$value1 = (Get-ItemProperty $regkeypath -ErrorAction SilentlyContinue).Zoiper -eq $null 
If ($value1 -eq $False) {
Write-Host \"Value Exist\"
} Else {
Write-Host \"The value does not exist\"
}
    
如果不存在某个属性,则将触发
-not
测试:
$prop = (Get-ItemProperty $regkey).$name
if (-not $prop)
{
   New-ItemProperty -Path $regkey -Name $name -Value \"X\"
}
    
这对我有用:
Function Test-RegistryValue 
{
    param($regkey, $name)
    $exists = Get-ItemProperty \"$regkey\\$name\" -ErrorAction SilentlyContinue
    Write-Host \"Test-RegistryValue: $exists\"
    if (($exists -eq $null) -or ($exists.Length -eq 0))
    {
        return $false
    }
    else
    {
        return $true
    }
}
    
我的版本:
Function Test-RegistryValue($Key, $Name)
{
    (Get-ChildItem (Split-Path -Parent -Path $Key) | Where-Object {$_.PSChildName -eq (Split-Path -Leaf $Key)}).Property -contains $Name
}
    
我从上方的Carbon那里获取了Methodology,并将代码简化为一个较小的函数,这对我来说非常有效。
    Function Test-RegistryValue($key,$name)
    {
         if(Get-Member -InputObject (Get-ItemProperty -Path $key) -Name $name) 
         {
              return $true
         }
         return $false
    }
    

要回复问题请先登录注册