我正在嘗試創建一個用戶將運行的批處理檔案,它將添加一個防火墻規則,該腳本有效,但我想阻止用戶創建多個具有相同名稱的規則。
我知道如何使用 netsh -contains 檢查它,但不確定如何將其轉換為批處理腳本。
我現有的腳本
@Echo On
netsh advfirewall firewall add rule name= "Open Port 80-90" dir=in action=allow protocol=TCP localport=80-90
Exit
我想做什么
@Echo On
if netsh advfirewall firewall show rule name="Open Port 80-90" -contains "No rules match the specified criteria."
@Echo On
netsh advfirewall firewall add rule name= "Open Port 80-90" dir=in action=allow protocol=TCP localport=80-90
Exit
uj5u.com熱心網友回復:
netsh advfirewall firewall show rule name="Open Port 80-90" > NUL 2>&1
IF ERRORLEVEL 1 (
netsh advfirewall firewall add rule name= "Open Port 80-90" dir=in action=allow protocol=TCP localport=80-90
) ELSE (
ECHO Rule already exists
)
更簡單。
如果規則存在,則該netsh命令設定errorlevel為 0,否則設定為非零。
uj5u.com熱心網友回復:
這應該這樣做:
netsh advfirewall firewall show rule name="Open Port 80-90" | findstr /c:"No rules match the specified criteria." > NUL 2>&1
IF %ERRORLEVEL% EQU 0 (
netsh advfirewall firewall add rule name= "Open Port 80-90" dir=in action=allow protocol=TCP localport=80-90
) ELSE (
ECHO Rule already exists
)
應該是不言自明的。%ERRORLEVEL%在這種情況下是捕獲errorlevelof findstr,0如果找到指定的字串。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/478118.html
