Match computer IP Address for a reference

Tagged: 

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Leroy D'Souza Leroy D’Souza 1 year, 2 months ago.

  • Author
    Posts
  • #30402
    Profile photo of Leroy D'Souza
    Leroy D’Souza
    Participant

    Hi,
    I want to be able to run a script that will load up a CSV that contains IP ranges and their corresponding servers.
    When the table is loaded, the computer needs to lookup it's own IP, refrence it to the range and update a registry with the corresponding server.
    I've given it a real good go for a while now however I can't seem to get it to compare/reference correctly.
    Would appreciate any help

    ##Sample CSV
    netaddress,netmask,SourceServer
    10.233.10.0,255.255.255.0,\\Server1
    172.16.254.48,255.255.255.252,\\Server2

    ##Myscript

    $IPComputerHashTabledata = $null
    $IpAddress = $null
    $IPComputerHashTabledata = @{}

    $IPComputerHashTabledata = Import-CSV ".\IP_Subnet_Details v2.csv" | ForEach {
    New-Object PSObject -prop @{
    IPvAddress = [IPAddress]::Parse($_.netipaddress);
    SourceServer = $_.SourceServer;
    Company = $_.company
    }
    } | Sort-Object IPvaddress -Descending
    #-Header IPvAddress,Source

    #Convert to Hashtable
    $lookup = $IPComputerHashTabledata | Group-Object -AsHashTable -AsString -Property IPvAddress
    $lookup = $lookup | Sort-Object IPvAddress -Descending

    $IpAddress=$null
    [IPADDRESS]$IpAddress = (gwmi Win32_NetworkAdapterconfiguration -Property * -Filter "ipENABLED = 'True'" | Select-Object -First 1 ipaddress).ipaddress[0]
    #$IpAddress = (gwmi Win32_NetworkAdapterconfiguration -Property * -Filter "ipENABLED = 'True'" | Select-Object -First 1 ipaddress).ipaddress[0]
    $IpAddress

    $IPComputerHashTabledata.Where({$PSItem.netipaddress -le $IpAddress}).SourceServer[0]

    $Source = $lookup[$IpAddress]

    ##Set Registry entry
    Set-ItemProperty HKLM:\SOFTWARE\Vendor\Software -name Source -Value "$source" -Force

  • #30418
    Profile photo of Brian B
    Brian B
    Participant

    I made some changes to your WMI query to only retrieve the NIC information for the NIC that contains a default gateway and then also filtered out any IPV6 addresses by excluding IPs that match two colons "::", and your hash table had netIPaddress in your Parse section but your CSV was just Netaddress. This appears to work. HOWEVER, make darn sure to place some code to trap any errors at this line instead of trying to write to the registry with invalid/not-populated data:

    $IPComputerHashTabledata.Where({$PSItem.netaddress -le $IpAddress}).SourceServer[0]
    
    $IPComputerHashTabledata = $null
    $IpAddress = $null
    $IPComputerHashTabledata = @{}
    
    $IPComputerHashTabledata = Import-CSV "D:\temp\PowerShell_ORG\Leroy\IP_Subnet_Details_v2.csv" | ForEach {
    New-Object PSObject -prop @{
    IPvAddress = [IPAddress]::Parse($_.netaddress);
    SourceServer = $_.SourceServer;
    Company = $_.company
    }
    } | Sort-Object IPvaddress -Descending
    #-Header IPvAddress,Source
    
    #Convert to Hashtable
    $lookup = $IPComputerHashTabledata | Group-Object -AsHashTable -AsString -Property IPvAddress
    $lookup = $lookup | Sort-Object IPvAddress -Descending
    
    $IpAddress=$null
    [IPADDRESS]$IpAddress = (gwmi Win32_NetworkAdapterconfiguration -Property * | where {$_.DefaultIPGateway -ne $null} | Select-Object  -First 1 -expand ipaddress | where {$_ -notmatch "::"})
    #$IpAddress = (gwmi Win32_NetworkAdapterconfiguration -Property * -Filter "ipENABLED = 'True'" | Select-Object -First 1 ipaddress).ipaddress[0]
    $IpAddress
    
    $IPComputerHashTabledata.Where({$PSItem.netipaddress -le $IpAddress}).SourceServer[0]
    
    $Source = $lookup[$IpAddress]
    
  • #30443
    Profile photo of Leroy D'Souza
    Leroy D’Souza
    Participant

    Thanks for that Brian. However that doesn't work.
    I've cleanedup my script a litle more

    ##Sample CSV
    netipaddress,netmask,SourceServer
    10.233.10.0,255.255.255.0,\\Server1
    172.16.254.48,255.255.255.252,\\Server2

    ##Myscript
    
    
    Remove-Variable IpAddress
    Remove-Variable IPComputerHashTabledata
    Remove-Variable lookup
    $filepath = "C:\TEMP\IP_Subnet_Details v2.csv"
    
    $IPComputerHashTabledata = $null
    $IpAddress = $null
    $IPComputerHashTabledata = @{}
    
    $IPComputerHashTabledata = Import-CSV $filepath | ForEach {
        New-Object PSObject -prop @{
        IPvAddress = [IPAddress]::Parse($_.netipaddress);
        SourceServer = $_.SourceServer ;
        }
      } #-Header IPvAddress,Source
      
    $IPComputerHashTabledata = $IPComputerHashTabledata.GetEnumerator() | Sort-Object name 
    
    
    #Convert to Hashtable
    $lookup = $IPComputerHashTabledata | Group-Object -AsHashTable -AsString -Property IPvAddress
    $lookup =  $lookup.GetEnumerator() | Sort-Object name
    
    
    $IpAddress=$null
    [IPADDRESS]$IpAddress = (gwmi Win32_NetworkAdapterconfiguration -Property * -Filter "ipENABLED = 'True'" | Select-Object -First 1 ipaddress).ipaddress[0]
    $IpAddress
    
    
    $IPComputerHashTabledata.Where({$PSItem.netipaddress -le $IpAddress}).SourceServer[0]
    
    $lookup[$IpAddress]
    
    
    
    
    $Source = $IPComputerHashTabledata.Where({$PSItem.netipaddress -match $IpAddress}).SourceServer[0] 
    
    
    
    ##Set Registry entry
    Set-ItemProperty HKLM:\SOFTWARE\Microsoft\Office\15.0\ClickToRun\Configuration -name Updateurl -Value "$source" -Force 
    

    I can see that the reference is occurring however the results are wrong.
    If my IP is in the 10.233.10.0 range , it should reference \\Server1 as the source. Many thanks.

  • #30444
    Profile photo of Craig Duff
    Craig Duff
    Participant

    See If this does it. Seems to me you have to do some type of comparison with the netmask in order to determine if the address is in range. How do you know the first IP address is the one that you are after?

    $data = Import-Csv c:\in.csv |
    Select-Object *,@{
        'Name' = 'NetIPAddress_obj'
        'Expression' = { [IPAddress]$_.NetIPAddress }
    }, @{
        'Name' = 'NetMask_obj'
        'Expression' = { [IPAddress]$_.netmask }
    }
    
    $ipaddress = Get-WmiObject Win32_NetworkAdapterconfiguration -Property ipaddress -Filter "ipENABLED = 'True'" |
    Select -First 1 | 
    Select -ExpandProperty IPAddress |
    Select -First 1 |
    ForEach-Object {
        [IPAddress]$_
    }
    
    $found = ForEach($datum in $data)
    {
        If ($datum.NetIPAddress_obj.Address -eq (
                $ipaddress.Address -band $datum.NetMask_obj.Address
            )){
            $datum
            Break
        }
    }
    
    If($found)
    {
        Write-Output $datum.SourceServer
    }

  • #31698
    Profile photo of Leroy D'Souza
    Leroy D’Souza
    Participant

    Craig that worked absolutely brilliantly!
    I've modified the script to check first if the network profile matches the domain set in NLA. That way it won't modify if not in our company network.

You must be logged in to reply to this topic.