Created
October 14, 2021 15:24
-
-
Save dieseltravis/0f614ed9eac8c494982604fb42511602 to your computer and use it in GitHub Desktop.
Powershell script to add an IP address to the list of IPs in Remote Desktop firewall rules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# From cmd: | |
# powershell.exe -file Add-Firewall-Ip.ps1 -newIp "8.8.8.8" | |
# From Powershell: | |
# & ./Add-Firewall-Ip.ps1 -newIp "8.8.8.8" | |
param($newIp = "") | |
$rules = Get-NetFirewallRule -DisplayName "Remote Desktop*" | |
foreach ($r in $rules) { | |
# get list of IPs as an array | |
$ips = @(($r | Get-NetFirewallAddressFilter).RemoteAddress) | |
Write-Host $r.DisplayName | |
Write-Host "Current IPs:" | |
Write-Host $ips | |
if ($newIp -ne "") { | |
#TODO: test for "Any" | |
$ips += $newIp | |
Set-NetFirewallRule -DisplayName $r.DisplayName -RemoteAddress $ips | |
Write-Host "New IPs:" | |
Write-Host ($r | Get-NetFirewallAddressFilter).RemoteAddress | |
} | |
Write-Host '' | |
} | |
if ($newIp -ne "") { | |
Write-Host "$newIp added" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment