m0b1l3us3r
m0b1l3us3r

Reputation: 95

Powershell avoid adding duplicate firewall rules windows 7

I created the following script, but for some reason it continues creating the rules every time I execute the script,

$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
}

Could you guys help on what I'm missing here?

EDIT: the solution is to modify the IF statement in this

"$Rule" -notmatch "No rules match"

Upvotes: 0

Views: 763

Answers (1)

colsw
colsw

Reputation: 3326

you probably want to replace $RULENAME1 -eq $Rule with something like:

"$Rule" -notmatch "No rules match"

inside your if statement, wrap $Rule with "s to stop -notmatch treating it as an array.

Upvotes: 1

Related Questions