我創建了下面的腳本, 但由於某種原因它繼續創造上我什麼我每次執行腳本時的規則,PowerShell的避免增加重複的防火牆規則的Windows 7
$RULENAME1 = 'Domain Controllers'
$Rule = netsh advfirewall firewall show rule name="$RULENAME1" $nul
if ($RULENAME1 -eq $Rule) {
echo "Rule "$RULENAME1" already exist."
echo "Hey, you already got a out rule by that name, you cannot put another one in!"
} else {
echo Rule "$RULENAME1" not exist. Creating...
netsh advfirewall firewall add rule name="$RULENAME1" dir=in action=allow remoteip=10.10.10.10
}
難道你們幫助在這裏失蹤?
編輯: 的解決方案是修改IF
聲明在此
"$Rule" -notmatch "No rules match"
'$ rule'包含規則的完整定義。它肯定不會與名稱相同...輸出輸出'| out-null「並檢查'$ lastexitcode'。如果沒有該名稱的規則,'netsh'將返回'1',否則返回'0'。 (當然,如果出現其他問題,它也會返回1。) –
嘗試過但不起作用 '$ Rule = netsh advfirewall firewall show rule name =「$ RULENAME1」| out-nul if($ lastexitcode -eq 1)' – m0b1l3us3r