2011-04-13 75 views
25

在我的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

回答

21

就個人而言,我不喜歡隨地吐痰錯誤的機會測試功能,所以這裏是我會做什麼。該函數還可以用作過濾器,您可以使用該過濾器來過濾註冊表項列表以僅保留具有某個鍵的項。

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

0

-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

4

我會與功能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

0

這個工作對我來說:

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

可能具有空白字符串的問題。下面是對我工作的清理版本:

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

10

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 
    } 

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

我的版本:

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

我的版本,匹配從捕獲的異常的確切文本。 如果它是一個不同的例外,它將返回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} 
} 
0

我把方法從碳以上,而精簡的代碼到一個較小的功能,這個工作對我非常好。

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

一行代碼:

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

最好的方式來測試,如果註冊表值存在是能夠做到這一點 - 它的存在測試。這是一個單行,即使它有點難以閱讀。

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

如果你確實查找其數據,那麼你將遇到的PowerShell如何解釋併發症0