Powershell-所有匹配项

Powershell-所有匹配项,powershell,Powershell,我遇到的问题是,即使值为24,也不会转到其他位置,任何帮助都将不胜感激 # Export current secuirty policies secedit /export /cfg $programdata\secu.inf /quiet $SecurityPolicyFile = $programdata + "\secu.inf" $FileContent = Get-Content $SecurityPolicyFile #CHECK COMPLIANCE #---------

我遇到的问题是,即使值为24,也不会转到其他位置,任何帮助都将不胜感激

# Export current secuirty policies
secedit /export /cfg  $programdata\secu.inf /quiet

$SecurityPolicyFile = $programdata + "\secu.inf"

$FileContent = Get-Content $SecurityPolicyFile

#CHECK COMPLIANCE

#----------------------------------------------------------------------------
#1.1.1 Enforce password history - 24 passwords remembered

$Matches = Select-String -InputObject $FileContent -Pattern "PasswordHistorySize = 24" -AllMatches

$IsAMatch = $Matches.Count

if ($IsAMatch -eq 24) {
  "1.1.1 Enforce password history - 24 passwords remembered .- In Compliance " + (Get-Date).ToString() >> $logfile
  $failed = $failed + 1
} else {
  "1.1.1 Enforce password history - 24 passwords remembered .- Not In Compliance " + (Get-Date).ToString() >> $logfile
}

$Matches
是一个。不要那样使用它。另外,
Select String
的输出没有属性
Count
。但是,
与该结果的
属性匹配。但即使这样,计数也会给你匹配的数量,而你实际上是在寻找记忆中的密码数量

更改此项:

$Matches = Select-String -InputObject $FileContent -Pattern "PasswordHistorySize = 24" -AllMatches

$IsAMatch = $Matches.Count

if ($IsAMatch -eq 24) {
为此:

$m = Select-String -InputObject $FileContent -Pattern "PasswordHistorySize = (\d+)" -AllMatches

$IsAMatch = -not [bool]($m.Matches | Where-Object { $_.Groups[1].Value -ne 24 })

if ($IsAMatch) {

谢谢,我试试看。