我正在創建一個 PowerShell 腳本來列出機器上安裝的所有 AV。在進行檢查時,我想在“If”陳述句中排除 Windows Defender。下面是我的腳本:
default
{
$result1 = 'There are {0} AV products installed on this system' -f $AVList.Count
$result2 = 'DisplayNames = {0}' -f ($AVList.displayName -join ', ')
if (Select-String -Path $workingdirectory\AVList.txt -Pattern $AVList.displayName -Exclude 'Windows Defender' -SimpleMatch -Quiet){
$writeTxt4 = ("$(Get-Date) - [INFO]",'There are {0} AV products installed on this system.' -f $AVList.Count)
$writeTxt5 = ("$(Get-Date) - [INFO]",'Anti-Virus Names = {0}' -f ($AVList.displayName -join ', '))
Write-Output $writeTxt4 $writeTxt5
Add-Content -path $report $writeTxt4
Add-Content -path $report $writeTxt5
}else{
$writeTxt4 = ("$(Get-Date) - [INFO]",'There are {0} AV products installed on this system. Smile.' -f $AVList.Count)
$writeTxt5 = ("$(Get-Date) - [INFO]",'Anti-Virus Names = {0}' -f ($AVList.displayName -join ', '))
Write-Output $writeTxt4 $writeTxt5
Add-Content -path $report $writeTxt4
Add-Content -path $report $writeTxt5
}
我試圖排除它,但仍然無法使其作業。非常感謝你的幫助。
uj5u.com熱心網友回復:
從Select-String, 引數的參考-Exclude:
排除指定專案。此引數的值限定了 Path 引數。輸入路徑元素或模式,例如 *.txt。允許使用通配符。
所以這不是我們在這里需要的。
如評論中所述,您應該過濾$AVList:
# Get all AV's, excluding "Windows Defender"
$filteredAvList = @($AVList.displayName) -notlike '*Windows Defender*'
if (Select-String -Path $workingdirectory\AVList.txt -Pattern $filteredAvList -SimpleMatch -Quiet){
# TODO: Replace $avList by $filteredAvList
}
當將比較運算子like-notlike應用于集合時,它會有效地過濾集合,僅回傳與條件匹配的元素。為確保運算子始終應用于陣列,請將 LHS 運算元與陣列子運算式 operator @()括起來。否則,當 LHS 是單個物件時,您將獲得布爾結果。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/428281.html
標籤:电源外壳
上一篇:從陣列中提取PID
