none
Need help getting script to detect the subnet it is on and ping specific devices from a master list based on subnet RRS feed

  • General discussion

  • I have a script that will ping a list of devices. What I am trying to figure out is how to have the script detect the subnet it is on and then select those devices from the master list based on subnet and spit out a CSV file with ping results and device status based on the current subnet.


    • Edited by Natedgreat Thursday, June 1, 2017 9:18 PM
    • Changed type Bill_Stewart Friday, July 7, 2017 7:02 PM
    • Moved by Bill_Stewart Friday, July 7, 2017 7:02 PM Unanswerable drive-by question
    Thursday, June 1, 2017 9:00 PM

All replies

  • So what's your question?


    -- Bill Stewart [Bill_Stewart]

    Thursday, June 1, 2017 9:13 PM
  • To detect subnet you need the device address and its subnet mask.

    Posting all of that forms code has nothing to do with subnet allocation.  It doesn't help.

    Please post only the piece of code you are using to detect subnet.


    \_(ツ)_/

    Thursday, June 1, 2017 9:15 PM
  • How can I get the script to identify the subnet it is on and then ping devices from a master list based on that subnet?
    Thursday, June 1, 2017 9:17 PM
  • I do not know how to get it to do that so I have no code to enter that would attempt it. Thus the point of my question.
    • Edited by Natedgreat Thursday, June 1, 2017 9:19 PM
    Thursday, June 1, 2017 9:19 PM
  • This will get you the subnet ID using the mask.

    PS>$ip = [ipaddress]'10.0.0.20'
    PS>$mask = [ipaddress]'255.255.255.0'
    PS>[ipaddress]($ip.Address -band $mask.address).IPAddressToString
    PS>([ipaddress]($ip.Address -band $mask.address)).IPAddressToString
    10.0.0.0


    \_(ツ)_/

    Thursday, June 1, 2017 9:20 PM
  • How can I get the script to identify the subnet it is on

    A script isn't "on" a subnet (by this I assume you mean a network ID), but a computer is. Here's one way for a script to determine the current computer's network ID:


    Get-WmiObject Win32_NetworkAdapterConfiguration -Filter "IPEnabled=true" |
      Select-Object `
        @{
          Name="ID"
          Expression={([IPAddress] (([IPAddress] $_.IPAddress[0]).Address -band ([IPAddress] $_.IPSubnet[0]).Address)).IPAddressToString}
        }
    


    -- Bill Stewart [Bill_Stewart]



    Thursday, June 1, 2017 9:30 PM
  • Assuming a single NIC:

    $nic = Get-WmiObject Win32_NetworkAdapterConfiguration -Filter 'IpEnables=True'
    [ipaddress]$ipaddr = $nic.IPAddress[0]
    [ipaddress]$ipsubnet = $nic.IPSubnet[0]
    ([ipaddress]($ip.Address -band $ipsubnet.address)).IPAddressToString
    


    \_(ツ)_/

    Thursday, June 1, 2017 9:33 PM
  • The following also gives you the mask length:

    $nic = Get-WmiObject Win32_NetworkAdapterConfiguration -Filter 'IpEnables=True' [ipaddress]$ipaddr = $nic.IPAddress[0] [ipaddress]$ipsubnet = $nic.IPSubnet[0] ([ipaddress]($ip.Address -band $ipsubnet.address)).IPAddressToString # mask length [convert]::ToString($ipsubnet.Address, 2).Length
    # number of hosts
    [convert]::ToString($ipsubnet.Address, 2).Length - 2
    # start by adding to net address from 1.
    1..$numhosts | %{$netaddress + $_ }


    \_(ツ)_/



    • Edited by jrv Thursday, June 1, 2017 9:52 PM
    Thursday, June 1, 2017 9:47 PM