重复的Windows防火墙规则(Netsh AdvFirewall防火墙)

时间:2015-08-24 14:37:06

标签: windows firewall rules netsh idempotent

我注意到,当我们通过netsh advfirewall firewall创建防火墙规则时,它可以多次运行,创建多个相同的防火墙规则。

在尝试创建新规则之前,有没有办法检查防火墙规则是否存在?

4 个答案:

答案 0 :(得分:3)

检查规则“myrule”是否不存在

netsh advfirewall firewall show rule name="myrule" | findstr "no rules"

答案 1 :(得分:0)

为了扩展@Oleg的答案,这是我使用的。根据规则的标准替换...。

set name="my rule"
netsh advfirewall firewall show rule name=!name! >nul
if errorlevel 1 (
    echo Adding firewall rule !name!
    netsh advfirewall firewall add rule name=!name! ...
)

答案 2 :(得分:0)

[SessionOrderHolidayMapping]   [SessionHolidayMapping]   [cfgScheudle]
----------------------------   -----------------------   --------------
[SessionId]                     [SessionId]               [SessionId]
[HolidayName]                   [HolidayName]             [OrderDays]
                                                          [NoOrderDays]

IF OBJECT_ID('[dbo].[RemoveGlobalOrderDays]') IS NOT NULL
    DROP PROCEDURE [dbo].[RemoveGlobalOrderDays]
GO

CREATE PROCEDURE [dbo].[RemoveGlobalOrderDays]
    @SessionId INT,
    @SelectedHolidays NVARCHAR(50),
    @SelectedOrderHolidays NVARCHAR(50),
    @OrderDays NVARCHAR(50),
    @NoOrderDays NVARCHAR(50)
WITH ENCRYPTION
AS
BEGIN
    SET NOCOUNT ON;

    WHERE [cfgSchedule].[SessionId] = @SessionID

    DELETE FROM [cfgSchedule] (OrderDays, NoOrderDays)

    SELECT Item, @SessionId 
    FROM SplitString(@OrderDays, ',') AS OrderDays 
    WHERE Item <> ''

    SELECT Item, @SessionId 
    FROM SplitString(@NoOrderDays, ',') AS NoOrderDays 
    WHERE Item <> ''

    DELETE FROM [SessionHolidayMapping] 
    WHERE [SessionID] = @SessionID

    DELETE FROM [SessionHolidayMapping] (HolidayName, SessionId)

    SELECT Item, @SessionId 
    FROM SplitString(@SelectedHolidays, ',') AS Holidays 
    WHERE Item <> ''

    DELETE FROM [SessionOrderHolidayMapping] 
    WHERE [SessionID] = @SessionID

    DELETE FROM [SessionOrderHolidayMapping] (HolidayName, SessionId)

    SELECT Item, @SessionId 
    FROM SplitString(@SelectedOrderHolidays, ',') AS Holidays 
    WHERE Item <> ''
END
GO   

答案 3 :(得分:0)

为什么没有人提到 ||操作员批量失败? && 成功运算符的反面

:: (if command errors) || (exec this command)
netsh advfirewall firewall show rule name="myrule" >nul || netsh advfirewall firewall add rule name="myrule" ...