I'm quite new to PowerShell, so please don't blame me, I'm in complete darkness... I have a text file containing hundreds of IP ranges. What I want to achieve is to create a firewall rule which should block all outbound connections to these IP ranges.
I want one single firewall rule, having all IP ranges under "Remote IP-Address -> This range" in the firewall rule.
The following script works, but it has created 500 firewall rules, one for each IP range. I want to have all IP ranges in one single rule.
What command should I use instead of New-NetFirewallRule
?
$Contents = Get-Content "C:\Temp\fb.txt"
foreach($Line in $Contents) {
New-NetFirewallRule -DisplayName "Block FB" -Direction Outbound -LocalPort Any -Protocol TCP -action Block -RemoteAddress $Line
}
I also tried Set-NetFirewallRule
, but all it does is replacing the IP Range, instead of adding a new range.
From the docs, RemoteAddress accepts an array of strings as input (Indicated by the [] in [-RemoteAddress <String[]>]).
Does this work for what you need? Unrelated to your problem - I splatted the parameters since they were mostly offscreen for me.
$FirewallParams = @{
DisplayName = 'Block FB'
Direction = 'Outbound'
LocalPort = 'Any'
Protocol = 'TCP'
Action = 'Block'
RemoteAddress = (Get-Content 'C:\Temp\fb.txt')
}
New-NetFirewallRule @FirewallParams
That did it, thank you very much. Works great!
Nice. If you mark as answered it should drop lower on the queue for other people to answer.