Skip to content

Instantly share code, notes, and snippets.

@dieseltravis
Created October 14, 2021 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dieseltravis/0f614ed9eac8c494982604fb42511602 to your computer and use it in GitHub Desktop.
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
# 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