测试是否存在注册表值

问题描述:

在我的powershell脚本中,我为每个运行脚本的元素创建一个注册表项,并且希望存储关于注册表中每个元素的其他信息(如果您指定了一个可选参数,那么默认情况下将来使用这些参数)。测试是否存在注册表值

我遇到的问题是我需要执行Test-RegistryValue(如here),但似乎没有办法(即使条目存在,它也会返回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 
    } 
} 

,不幸的是还没有做什么,我需要,因为它似乎总是从选择一些(第一?)值注册表项。

任何人有想法如何做到这一点? 这似乎只是太多为此编写托管代码......如果属性不存在

+1

'''(GET-项目-Path $ PATH).GetValue($值)如果存在值,则-ne $ null'''返回true。 – dezza 2016-06-07 13:57:32

就个人而言,我不喜欢随地吐痰错误的机会测试功能,所以这里是我会做什么。该函数还可以用作过滤器,您可以使用该过滤器来过滤注册表项列表以仅保留具有某个键的项。

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 
     } 
    } 
} 
+2

一个错误:'$ Key.GetValue($ Name,$ null))'可以得到0或一个空字符串,即一个存在的值,但是if ($ Key.GetValue($ Name,$ null)))'获取false并且脚本返回false,就像缺少一个值一样。另外,我建议在每个地方都使用'-LiteralPath'而不是'-Path'。这项任务是关于单一的价值测试。请注意'*'和'?'是注册表名称的罕见但有效的字符。 – 2011-04-13 17:42:28

+0

好,点数,固定了一下。 – JasonMArcher 2011-04-13 22:30:29

-not测试应该火:

$prop = (Get-ItemProperty $regkey).$name 
if (-not $prop) 
{ 
    New-ItemProperty -Path $regkey -Name $name -Value "X" 
} 
+1

它实际上似乎链接的解决方案确实是正确的,但由于某种原因,我调用函数使用C类语法,而不是传递命名参数,因此两个$ regkey初始化字符串连接$ regkey和$名称:( – Newanja 2011-04-13 12:58:36

我会与功能Get-RegistryValue去。实际上它获取了所需的值(以便它不仅可用于测试)。至于注册表值不能为空,我们可以使用空结果作为缺失值的符号。还提供了纯粹的测试功能Test-RegistryValue

# 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 } 

OUTPUT:

True 
54 
False 
missing value 
+0

A关于错误的单词(甚至用'-ErrorAction SilentlyContinue'错误实际上被添加到'$ Error'列表中)假设我们检查可能存在的*键的潜在缺失值,在实践中不应该有太多错误。这样的错误是不必要的,那么代码应该看起来像'如果(测试路径-LiteralPath $路径){...}其他{...} – 2011-04-13 17:56:24

+0

我投票,然后测试:) 它简单的测试示例失败: $ regkey =“HKCU:\ Software \ Microsoft” $ name =“myName1” $ key = Get-Item -LiteralPath $ path -ErrorAction无提示继续 错误: Get-Item:无法将参数绑定到参数'LiteralPath',因为它为空。 – Newanja 2011-04-14 07:43:07

+0

也许你已经找到你的错字。您在'Get-Item'中发送'$ path'变量。但它没有在你的代码段中定义:你定义了'$ regkey'。所以你应该做'Get-Item -LiteralPath $ regkey'。 – 2011-04-14 10:02:35

这个工作对我来说:

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($regkey, $name) { 
    $exists = Get-ItemProperty -Path "$regkey" -Name "$name" -ErrorAction SilentlyContinue 
    If (($exists -ne $null) -and ($exists.Length -ne 0)) { 
     Return $true 
    } 
    Return $false 
} 
+0

示例用于任何人阅读:'Test-RegistryValue“HKLM:\ SOFTWARE \ Classes \ Installer \ Dependencies \ {f65db027-aff3-4070-886a-0d87064aabb1}”“DisplayName”' – Luke 2017-04-07 10:27:45

+0

'($ exists -ne $ null) - 和($ exists.Length -ne 0)'当属性存在时检查失败。我发现最好使用'(-not $ exists)'而不是 – Luke 2017-04-07 11:59:48

Carbon PowerShell module具有Test-RegistryKeyValue功能会为你做这项检查。 (披露:我是碳的拥有者/维护者。)

您必须首先检查注册表项是否存在。如果注册表项没有值,则必须处理。这里的大多数例子实际上是测试值本身,而不是值的存在。如果值为空或空值,这将返回错误的否定结果。相反,您必须测试Get-ItemProperty返回的对象上的值的属性是否实际存在。

下面的代码,因为它代表今天,从碳模块:

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 
    } 

} 

$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" 
} 

我的版本:

Function Test-RegistryValue($Key, $Name) 
{ 
    (Get-ChildItem (Split-Path -Parent -Path $Key) | Where-Object {$_.PSChildName -eq (Split-Path -Leaf $Key)}).Property -contains $Name 
} 

我的版本,匹配从捕获的异常的确切文本。 如果它是一个不同的例外,它将返回true,但适用于这个简单的情况。还可以获得-ItemPropertyValue是新的PS 5.0

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} 
} 

我把方法从碳以上,而精简的代码到一个较小的功能,这个工作对我非常好。

Function Test-RegistryValue($key,$name) 
    { 
     if(Get-Member -InputObject (Get-ItemProperty -Path $key) -Name $name) 
     { 
       return $true 
     } 
     return $false 
    } 

一行代码:

$valueExists = (Get-Item $regKeyPath -EA Ignore).Property -contains $regValueName 

最好的方式来测试,如果注册表值存在是能够做到这一点 - 它的存在测试。这是一个单行,即使它有点难以阅读。

PS C:> (Get-ItemProperty $regkey).PSObject.Properties.Name -contains $name

如果你确实查找其数据,那么你将遇到的PowerShell如何解释并发症0