Warm tip: This article is reproduced from serverfault.com, please click

Passing multiple values to PowerShell script from text file

发布于 2020-12-12 10:51:10

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.

Questioner
Daniel D.
Viewed
0
OwlsSleeping 2020-12-12 19:35:13

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