Powershell:当我检查的值为 0 时,为什么我的 RegKey 值检查使用 Get-ItemProperty 失败?

Powershell: Why are my RegKey value checks failing using Get-ItemProperty when the value I am checking for is 0?

我的任务是编写注册码检查脚本,以确保抽样的机器符合我们的安全状况。

该脚本在检查任何非零值(例如下面的示例输入)时效果很好。 但是,如果期望值为 0,我的代码会错误地报告检查失败,这是一个一致的主题。请参见下面的示例代码:

$failReview = "" #String to track compliance issues
$OutFile = "C:\local\file.text"


Function Test-STIG_SingleKey {
  
    #check for single acceptable key value
    #up to 3 additional acceptable key values may be checked for single key

    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string[]] $CV,     #Check Vulnerability number
        [string[]] $Path,   #RegKey Path
        [string[]] $Key,    #Key to check
        [int] $value,       #Expected Regkey value for compliance
        $FailReview,
        [Parameter(Mandatory=$false)]
        [int] $value1,      #optional acceptable value
        [int] $value2,      #optional acceptable value
        [int] $value3       #optional acceptable value
    )
    Begin {}

    Process {
        
        $KeyObject = Get-ItemProperty $Path                                 #Obtain RegKey object values using RegKey Path
        $Exist = ([bool]($KeyObject.PSObject.Properties.name -imatch $Key)) #Check for match of key value property name and cast to boolean
        If(!($Exist)){                                                                                      #If does NOT exist
            Write-Host "    !!! $CV is a finding !!!" -ForegroundColor "Red"; $Global:failReview += "$CV, "} #This IS a finding, append CV to list for review
        ElseIf (($KeyObject)."$Key" = $value){                              #checks relevant object property value and compares to Primary expected value
            Write-Host "    $CV is not a finding" -ForegroundColor "Green"
        } 
        ElseIf (((($KeyObject)."$Key" = $value1) -or (($KeyObject)."$Key" = $value2) -or (($KeyObject)."$Key" = $value3))) { #alternate acceptable value check
            Write-Host "    $CV is not a finding, alternate acceptable values used" -ForegroundColor "Green"    #not a finding, but indicates alt value present
        }
        else {Write-Host "    !!! $CV is a finding !!!" -ForegroundColor "Red"; $Global:FailReview += "$CV, "} #This IS a finding, append CV to list for review                                              

    }
    End {Return $CV, $Path, $Key, $value, $KeyObject, $failReview | Out-File $OutFile -Append}         #Pass all variable values to out-file for review if needed
}


#EXAMPLE INPUT:
Test-STIG_SingleKey -CV "V70955" `
-Path "HKLM:\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_HTTP_USERNAME_PASSWORD_DISABLE" `
-Key "excel.exe" `
-value 1

我可以通过查看 $KeyObject.Key 手动获取值,但如果我要查找 0,似乎无法让输出计算为真。

我认为这很可能是因为它在某个时刻评估 0 到 $false,但它是一个 int。 我已经尝试转换为 [int] 并将其转换为十六进制。

如有任何建议,我们将不胜感激,因为我几乎花了更多的时间来弄清楚这一点,然后只需要手动查看我们需要的采样键。

谢谢!

这是因为在您的 if 语句中,您将 ($KeyObject)."$Key" 设置为等于某个值,而不是检查它是否设置为特定值。

所以

... ElseIf (($KeyObject)."$Key" = $value){ ...

应该是

... ElseIf (($KeyObject)."$Key" -eq $value){ ...