Q:

Script to change windows firewall remote address excluding rules with localsubnets

I am trying to expand a script I wrote (and got help on from users on SO). Its a Windows powershell script that adds IP's to the Remote Address list on the Windows firewall. 
I am trying to get it to exclude any firewall rules where the remote address is already specified as "LocalSubnet". 
Here is what I have so far, currently when I run it with write-host instead of the actual command, it displays nothing. 
#This script will update remote address rules with the specified IP scope(s)
$displayname = read-host "Enter firewall rule display name"
$name = Get-NetFirewallRule -DisplayName $displayname |Where-Object {$_.Direction -eq "Inbound"}
$exclusion =  Get-NetFirewallRule -DisplayName $displayname |Get-NetFirewallAddressFilter
$ips = @()
do {
 $input = (Read-Host "Please enter IP address")
 if ($input -ne '') {$ips += $input}
}
until ($input -eq '')

foreach ($r in $name)
{
    foreach ($e in $exclusion){
        if ($e.remoteaddress -NotMatch "LocalSubnet") {continue}
    #Set-NetFirewallRule -DisplayName $r.DisplayName -RemoteAddress $ips
    write-host $r.Displayname $ips
    }
}

I am lost on the nested loop...do I even need the nested loop? I tried some while and do loops as well to no avail. 
@Matt - Thanks! Here is the correct script. 
#This script will update remote address rules with the specified IP scope(s)
$displayname = read-host "Enter firewall rule display name"
$rules = Get-NetFirewallRule -DisplayName $displayname | Where-Object {$_.Direction -eq "Inbound" -and ($_ | Get-NetFirewallAddressFilter).RemoteAddress -ne "LocalSubnet" }
$ips = @()
do {
 $answer = (Read-Host "Please enter IP address")
 if ($answer -ne '') {$ips += $answer}
}
until ($answer -eq '')

foreach ($rule in $rules)
    {

    #Set-NetFirewallRule -DisplayName $r.DisplayName -RemoteAddress $ips
    write-host $rule.Displayname $ips

}

A:

I will remove this if I am wrong but code in comments is terrible. I am changing the names of the variables to have more meaning. The inner loop you have is not required and would generate extra output. All we do here is build the condition into the Where-Object. This removes the exclusions logic you were trying to do.
$rules = Get-NetFirewallRule -DisplayName $displayname | 
    Where-Object {$_.Direction -eq "Inbound" -and ($_ | Get-NetFirewallAddressFilter).RemoteAddress -ne "LocalSubnet" }

# get ips .....

foreach ($rule in $rules)
{
    # Do Stuff
}

$rules should now only be the ones you need to action.

Note: Don't use $input as a variable name. It is a reserved name in PowerShell. See about_automatic_variables for more information. $answer or $result would suffice in this case. 

