How best to retrieve IPv4 (only) addresses from remote computers

65 views
Skip to first unread message

Mike Leone

unread,
Feb 21, 2023, 12:49:47 PM2/21/23
to NTPowershell Mailing List
I have a need to write a script that queries certain computers in an OU for IP addresses (among other things). The problem I have is - some of these computers have multiple IPv4 addresses, and I want only the ones in a certain range ...

Previously, I've used a WMI query like this:

$WMI = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -computer $ComputerName -ErrorAction Stop | Where-Object {$_.IPEnabled -eq $TRUE -and $_.DHCPEnabled -eq $False} | SELECT -Expand IPAddress

This works fine, when there's only 1 NIC with an IPv4 address. However, I have a few computers that have multiple NICs, with IPv4 addresses.  (which can make my WMI query return values for 2 adapters, and so look like this)

192.168.95.1
fe80::8134:7934:9dae:77ce
10.64.125.34
fe80::991a:6e40:ec9b:9c32

Now, in my case, I want only the network adapter with the IPv4 address like "10.64.x.x", because I want to re-assign the DNS settings for that adapter. so I want only the 2nd adapter above ...

But I'm struggling to find a clean and easy way to pull out *just* the adapter with the IP address range I want, in a single query. I thought I could execute the above, with an extra WHERE clause
 "| Where {$_.IPAddress -like "10.64.*.*"}.

But that's not working, and I haven't been able to figure out a way to query just the NIC that has that IP address range, so that I can set the DNS entries on it.

Clues, anyone? I tried to construct a Get-CimInstance, since I read I should try moving away from WMI and using CIM, but that's not getting me anywhere fast, either ....

--

Mike. Leone, <mailto:tur...@mike-leone.com>

PGP Fingerprint: 0AA8 DC47 CB63 AE3F C739 6BF9 9AB4 1EF6 5AA5 BCDF
Photo Gallery: <http://www.flickr.com/photos/mikeleonephotos>

Wright, John M

unread,
Feb 21, 2023, 1:02:50 PM2/21/23
to ntpowe...@googlegroups.com

This should work.

 

foreach ($w in $wmi) { if ($w -Like "10.64*") { $w } }

 

--

John Wright

IT Support Specialist

1800 Old Bluegrass Avenue, Louisville, KY 40215

502-708-9953

Please submit IT requests to Hazelwoo...@newvista.org

24 Hour Helpline 1.800.928.8000

 

CONFIDENTIALITY NOTICE: This message contains confidential information and is intended only for the individual(s) addressed in the message. If you are not the named addressee, you should not disseminate, distribute, or copy this e-mail. If you are not the intended recipient, you are notified that disclosing, distributing, or copying this e-mail is strictly prohibited.

From: ntpowe...@googlegroups.com <ntpowe...@googlegroups.com> On Behalf Of Mike Leone
Sent: Tuesday, February 21, 2023 12:50 PM
To: NTPowershell Mailing List <ntpowe...@googlegroups.com>
Subject: [ntpowershell] How best to retrieve IPv4 (only) addresses from remote computers

 

This message is from an external sender.

--
You received this message because you are subscribed to the Google Groups "ntpowershell" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ntpowershell...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ntpowershell/CAHBr%2B%2BhYae%3DutzJ7W7mW7KCfUNiiOo_uu2QJteLCt4uWZOJT3A%40mail.gmail.com.

[CAUTION] Do not click on links or open attachments unless you recognize the sender and know the content is safe.
If you believe this is a malicious email, please forward it the local IT team and click the Report Message button in Outlook.

 

Gordon Pegue

unread,
Feb 21, 2023, 1:08:29 PM2/21/23
to ntpowe...@googlegroups.com

Try this:

 

$NAAdptr = Get-NetIPAddress -AddressFamily IPv4 | Where-Object {$_.IPAddress -like '10.64.*'}

 

Gordon

 

From: ntpowe...@googlegroups.com <ntpowe...@googlegroups.com> On Behalf Of Mike Leone
Sent: Tuesday, February 21, 2023 10:50
To: NTPowershell Mailing List <ntpowe...@googlegroups.com>
Subject: [ntpowershell] How best to retrieve IPv4 (only) addresses from remote computers

 

  [EXTERNAL]

--

Mike Leone

unread,
Feb 21, 2023, 2:05:48 PM2/21/23
to ntpowe...@googlegroups.com
On Tue, Feb 21, 2023 at 1:08 PM Gordon Pegue <gpe...@unm.edu> wrote:

Try this:

 

$NAAdptr = Get-NetIPAddress -AddressFamily IPv4 | Where-Object {$_.IPAddress -like '10.64.*'}


AH HA!

PS O:\software\PHA Scripts\DNS> Get-NetIPAddress -CimSession xxxx -AddressFamily IPv4 |  where { $_.InterfaceAlias -notmatch 'Loopback'} | Where {$_.IPAddress -like "10.64.*"} | SELECT IPAddress

IPAddress
---------
10.64.125.36

A good start. Now I just need to set DNS on the adapter with that address ...


Kurt Buff

unread,
Feb 21, 2023, 2:33:22 PM2/21/23
to ntpowe...@googlegroups.com
I think you'll want to grab the interfacealias or interfaceindex, then
use that with Set-DnsClientServerAddress

Kurt
> To view this discussion on the web visit https://groups.google.com/d/msgid/ntpowershell/CAHBr%2B%2Bi9SOUQ%3DANHYeSve%2BQtG43sfrDOmUTicBn7sq28He0tcA%40mail.gmail.com.

Mike Leone

unread,
Feb 21, 2023, 3:15:16 PM2/21/23
to ntpowe...@googlegroups.com
This seems to be working out well so far, to query:
(yes, I know it's inefficient and ugly, but it's clear - to me, anyway .... ...)


$IP = Get-NetIPAddress -CimSession $ComputerName -AddressFamily IPv4 |  where { $_.InterfaceAlias -notmatch 'Loopback'} | Where {$_.IPAddress -like "10.64.*"} 
$IPAddress = $IP.IPAddress
$InterfaceIndex = $IP.InterfaceIndex
$DNS_Client                     = Get-DnsClient -CimSession $ComputerName -InterfaceIndex $InterfaceIndex
$DNS_Client_Settings            = Get-DnsClientServerAddress  -CimSession $ComputerName -InterfaceIndex $InterfaceIndex -AddressFamily IPv4
$DNS_Client_Global_Setting      = Get-DnsClientGlobalSetting -CimSession $ComputerName
$DNSSuffix                      = $DNS_Client.ConnectionSpecificSuffix
$DNSDomainSuffixSearchOrder     = $DNS_Client_Global_Setting.SuffixSearchList
$DomainDNSRegistrationEnabled   = $DNS_Client.RegisterThisConnectionsAddress
$DNSServerSearchOrder           = $DNS_Client_Settings.ServerAddresses



Mike Leone

unread,
Feb 21, 2023, 3:32:48 PM2/21/23
to ntpowe...@googlegroups.com
I'll tell you the part that's aggravating me now ... looks like I can't use the below commands with some of my hosts, and  those arwe still running Win 2008 R2 (don't ask, I've complained for years, and you see where that's gotten me ...)

They are at least running PS v5.1 ....

Get-NetIPAddress : The WS-Management service cannot process the request. The CIM namespace root/standardcimv2 is invalid.  

Any ideas on how to get that to work when querying Win 2008 R2 hosts?

Kurt Buff

unread,
Feb 21, 2023, 3:42:02 PM2/21/23
to ntpowe...@googlegroups.com

Michael B. Smith

unread,
Feb 21, 2023, 3:44:56 PM2/21/23
to ntpowe...@googlegroups.com

You can’t, directly. The required namespace was added in the “great expansion” in Server 2012.

 

Prior to then you used netsh (which still works):

 

               netsh int ipv4 show ipaddresses

               netsh int ipv4 show dnsservers

 

I wrote a CoreConfigurator script for PS v1/v2 and server core (before MSFT released scmd). It does this. But it’s an enterprise class script (that is, it’s full of documentation and logging and etc.) and won’t be trivial to pull the small pieces out you need. But if you want it, I’ll post it.

 

Thanks.

 

Regards,

Michael B. Smith

Managing Consultant

Smith Consulting, LLC

 

Sent: Tuesday, February 21, 2023 3:33 PM
To: ntpowe...@googlegroups.com
Subject: Re: [ntpowershell] How best to retrieve IPv4 (only) addresses from remote computers

 

I'll tell you the part that's aggravating me now .. looks like I can't use the below commands with some of my hosts, and  those arwe still running Win 2008 R2 (don't ask, I've complained for years, and you see where that's gotten me ...)

Mike Leone

unread,
Feb 21, 2023, 3:45:20 PM2/21/23
to ntpowe...@googlegroups.com
On Tue, Feb 21, 2023 at 3:42 PM Kurt Buff <kurt...@gmail.com> wrote:
Parsing/using netsh?

If you're asking if am I using netsh, the answer is no. If you're asking  something more, I am not following. :-)

Mike Leone

unread,
Feb 21, 2023, 3:54:32 PM2/21/23
to ntpowe...@googlegroups.com
On Tue, Feb 21, 2023 at 3:44 PM Michael B. Smith <mic...@smithcons.com> wrote:

You can’t, directly. The required namespace was added in the “great expansion” in Server 2012.

 

Prior to then you used netsh (which still works):

 

               netsh int ipv4 show ipaddresses

               netsh int ipv4 show dnsservers



Hmmm ... well, I am querying  AD for OS info first (don't want the script to try setting DNS on non-MS AD computer objects, such as vCenters, Data Domains, etc). So I guess I can do a section for Win 2008 R2 hosts only, and use the "netsh in ipv4" to query/set settings ...

Goodie, more fun for tomorrow! LOL

I wrote a CoreConfigurator script for PS v1/v2 and server core (before MSFT released scmd). It does this. But it’s an enterprise class script (that is, it’s full of documentation and logging and etc.) and won’t be trivial to pull the small pieces out you need. But if you want it, I’ll post it.


I'm sure it's massive overkill for what I need, but I always find your scripts enlightening, if not always directly helpful. So if you want to post, I'll be happy to peruse it, and see how much of it I can understand ... LOL

Michael B. Smith

unread,
Feb 21, 2023, 5:31:04 PM2/21/23
to ntpowe...@googlegroups.com

Here ya go…

 

###

### Network-Settings.ps1

###

 

$L_Gateway_Title = "Gateway Settings"

$L_Gateway_Text  = "Do you wish to enter a Gateway Address"

 

$L_DNS_Primary_Title = "DNS Settings"

$L_DNS_Primary_Text  = "Do you wish to make this the Primary DNS Server"

 

$L_DNS_Title = "DNS Settings"

$L_DNS_Text  = "Do you wish to enter a DNS Server"

 

function Get-NIC( [string]$nic )

{

               foreach( $nicName in $Script:arrNicName )

               {

                              if( $nicName -eq $nic )

                              {

                                             return $true

                              }

               }

 

               Popup "An invalid network interface name was specified" "Find Network Interface Name" $vbError

 

               return $false

}

 

function NetworkSettings

{

               msg $L_Function

               msg "NetworkSettings: enter"

 

               $L_Network_Title = "Network Control Panel"

               $L_Network_Text  =

                              "Network Settings                            $nl" +

                              "********************************************$nl" +

                              "1 - Display Network Settings                $nl" +

                              "2 - Configure the Network Interface         $nl" +

                              "3 - Rename Network Interface                $nl" +

                              "4 - Enable | Disable Network Interface      $nl" +

                              $nl                                               +

                              "DNS Settings                                $nl" +

                              "********************************************$nl" +

                              "5 - Add | Remove DNS Servers                $nl" +

                              "6 - Flush | Register DNS Settings           $nl" +

                              $nl                                               +

                              "iSCSI Menu                                  $nl" +

                              "********************************************$nl" +

                              "7 - iSCSI Initiator Settings                $nl" +

                              $nl                                               +

                              "X - Exit to upper level menu"

 

               while( 1 )

               {

                              $userInput = InputBox $L_Network_Text $L_Network_Title '1'

                              switch ( $userInput )

                              {

                                             '1'

                                                            { DisplayNetwork;   }

                                             '2'

                                                            { ConfigureNetwork; }

                                             '3'

                                                            { RenameNIC;        }

                                             '4'

                                                            { EnableDisableNIC; }

                                             '5'

                                                            { ConfigureDNS;     }

                                             '6'

                                                            { FlushDNS;         }

                                             '7'

                                                            { iScsiMenu;        }

                                             'x'

                                                            { msg "NetworkSettings: exit"; return;           }

                                             ''

                                                            { msg "NetworkSettings: exit"; return;           }

                                             default

                                                            {

                                                                           Popup $L_Bad_Choice $L_Network_Title $vbError

                                                            }

                              }

               }

 

               msg "NetworkSettings: exit"

}

 

function DisplayNetwork

{

               msg $L_Function

               msg "DisplayNetwork: enter"

 

               [string] $strNetworkFile = tmpFileName

 

               $adapters = gwmi -Query "Select * from Win32_NetworkAdapterConfiguration"

 

               [string] $str = "Network Card Information$nl"

               foreach( $o in $adapters )

               {

                              ### retrieve the adapter name

                              ### Hive HKLM

                              $regName = "System\CurrentControlSet\Control\Network\" +

                                             "{4D36E972-E325-11CE-BFC1-08002BE10318}\" +

                                             $o.SettingID +

                                             "\Connection"

                              $valueName = "Name"

 

                              $name = ''

                              $result = RegRead $hklm $regName $valueName ([ref] $name)

                              if( $result -ne 0 )

                              {

                                             $name = '<unknown>'

                              }

                              msg "NIC name =" $name

 

                              if( $o.IPEnabled )

                              {

                                             ### retrieve and classify all available IP addresses

                                             $ipv4 = @()

                                             $ipv6 = @()

                                             foreach( $ip in $o.IPAddress )

                                             {

                                                            if( $ip.IndexOf( ':' ) -ge 0 )

                                                            {

                                                                           $ipv6 += $ip

                                                            }

                                                            else

                                                            {

                                                                           $ipv4 += $ip

                                                            }

                                             }    

                                             $strIPv4 = $ipv4 -join ' '

                                             $strIPv6 = $ipv6 -join ' '

 

                                             ### retrieve and classify all available netmasks

                                             $nmv4 = @()

                                             $nmv6 = @()

                                             foreach( $nm in $o.IPSubNet )

                                             {

                                                            if( $nm.IndexOf( '.' ) -ge 0 )

                                                            {

                                                                           $nmv4 += $nm

                                                            }

                                                            else

                                                            {

                                                                           $nmv6 += $nm

                                                            }

                                             }

                                             $strNMv4 = $nmv4 -join ' '

                                             $strNMv6 = $nmv6 -join ' '

 

                                             $str += "==============================================================$nl"

                                             $str += "Adapter Name          : " + $name + $nl

                                             $str += "State                 : IPEnabled = True" + $nl

                                             $str += "Description           : " + $o.Description + $nl

                                             $str += "Index                 : " + $o.Index + $nl

                                             $str += "DHCP Enabled          : " + $o.DHCPEnabled + $nl

                                             $str += "MAC Address           : " + $o.MACAddress + $nl

 

                                             if( $strIPv4 -and ( $strIPv4.Length -gt 0 ) )

                                             {

                                             $str += "IP Addresses (v4)     : " + $strIPv4 + $nl

                                             }

                                             if( $strIPv6 -and ( $strIPv6.Length -gt 0 ) )

                                             {

                                             $str += "IP Addresses (v6)     : " + $strIPv6 + $nl

                                             }

 

                                             if( $strNMv4 -and ( $strNMv4.Length -gt 0 ) )

                                             {

                                             $str += "IPv4 netmask          : " + $strNMv4 + $nl

                                             }

                                             if( $strNMv6 -and ( $strNMv6.Length -gt 0 ) )

                                             {

                                             $str += "IPv6 netmask          : " + $strNMv6 + $nl

                                             }

 

                                             foreach( $gw in $o.DefaultIPGateway )

                                             {

                                             $str += "Default gateway       : " + $gw + $nl

                                             }

 

                                             foreach( $dns in $o.DNSServerSearchOrder )

                                             {

                                             $str += "DNS servers           : " + $dns + $nl

                                             }

 

                                             $nmv6 = $null

                                             $nmv4 = $null

                                             $ipv6 = $null

                                             $ipv4 = $null

                              }

                              else

                              {

                                             $str += "==============================================================$nl"

                                             $str += "Adapter Name          : " + $name + $nl

                                             $str += "State                 : IPEnabled = False" + $nl

                                             $str += "Description           : " + $o.Description + $nl

                                             $str += "Index                 : " + $o.Index + $nl

                                             $str += "MAC Address           : " + $o.MACAddress + $nl

                              }

                              $str += $nl

               }

 

               $str | Out-File -FilePath $strNetworkFile -Force

               Get-Exitcode notepad.exe $strNetworkFile

               rm $strNetworkFile -EA 0

 

               msg "DisplayNetwork: exit"

}

 

function RenameNIC

{

               msg $L_Function

               msg "RenameNIC: enter"

 

               $L_Rename_Title = "Rename Network Interface"

 

               $oldNIC = InputBox "Enter the old name of the Network Interface" $L_Rename_Title $script:arrNicName[ $script:defNicIndex ]

               if( !$oldNIC -or ( $oldNIC.Length -eq 0 ) )

               {

                              msg "RenameNIC: exit, no old network interface name was specified"

                              return

               }

 

               if( -not ( Get-NIC $oldNIC ) )

               {

                              msg "RenameNIC: exit, bad NIC name"

                              return

               }

 

               $newNIC = InputBox "Enter the new name of the Network Interface" $L_Rename_Title "Private Network"

               if( !$newNIC -or ( $newNIC.Length -eq 0 ) )

               {

                              msg "RenameNIC: exit, no new network interface name was specified"

                              return

               }

 

               $str = 'interface set interface name="' + $oldNIC + '" newname="' + $newNIC + '"'

               msg "RenameNIC = '$str'"

 

               $returnValue = 0

               if( $Unsafe )

               {

                              $returnValue = Get-Exitcode netsh.exe $str

                              msg "RenameNIC = $returnValue"

               }

               else

               {

                              msg "netsh.exe not executed, use Unsafe to make changes. Command would be:"

                              msg "Get-ExitCode netsh.exe $str"

               }

 

               if( $returnValue -eq 0 )

               {

                              Popup "Success: renamed $oldNIC to $newNIC" $L_Rename_Title $vbInformation

               }

               else

               {

                              Popup "Error: Could not rename NIC $oldNIC (error = $returnValue)" $L_Rename_TItle $vbError

               }

 

               msg "RenameNIC: exit"

               return

}

 

function ConfigureNetwork

{

               msg $L_Function

               msg "ConfigureNetwork: enter"

 

               $L_Configure_Title = "Configure Network Interface" 

               $L_Configure_Text =

                              "You can have IP configuration assigned automatically if your        $nl" +

                              "network interface supports this capability (called DHCP).           $nl" +

                              "Otherwise, you will need to assign the proper IP address and        $nl" +

                              "other settings.                                                     $nl" +

                              $nl                                                                       +

                              "1 = Obtain an IP address and other settings automatically using DHCP$nl" +

                              "2 = Use a specified static IP address                               $nl" +

                              $nl                                                                       +

                              "X - Exit to upper level menu"

 

               ### this is for IPv4 only

               #### [Regex] $ipRegex = "^(25[0-5]|2[0-4][0-9]|[01]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"

               [string] $ipRegex = "\b(([01]?\d?\d|2[0-4]\d|25[0-5])\.){3}([01]?\d?\d|2[0-4]\d|25[0-5])\b"

 

               $userInput = InputBox $L_Configure_Text $L_Configure_Title '1'

               switch ( $userInput )

               {

                              '1'

                                             {

                                                            $nic = InputBox "Enter the network interface name" $L_Configure_Title $script:arrNicName[ $script:defNicIndex ]

                                                            switch ( $nic )

                                                            {

                                                                           'x'

                                                                                          {

                                                                                                         msg "ConfigureNetwork: exit (X, NIC name 1)"

                                                                                                         return

                                                                                          }

                                                                           ''

                                                                                          {

                                                                                                         msg "ConfigureNetwork: exit (Cancel, NIC name 1)"

                                                                                                         return

                                                                                          }

                                                            }

 

                                                            if( -not ( Get-NIC $nic ) )

                                                            {

                                                                           return

                                                            }

 

                                                            $prefix = ' interface ipv4 set '

                                                            $suffix = ' name="' + $nic + '" source=dhcp'

 

                                                            $cmd1 = $prefix + 'address'   + $suffix

                                                            $cmd2 = $prefix + 'dnsserver' + $suffix

 

                                                            $returnValue = 0

                                                            if( $Unsafe )

                                                            {

                                                                           $returnValue = Get-ExitCode netsh.exe $cmd1

                                                            }

                                                            msg "netsh.exe $cmd1"

                                                            msg "returnValue = $returnValue"

 

                                                            if( $Unsafe )

                                                            {

                                                                           $returnValue = Get-ExitCode netsh.exe $cmd2

                                                            }

                                                            msg "netsh.exe $cmd2"

                                                            msg "returnValue = $returnValue"

 

                                                            if( $Unsafe )

                                                            {

                                                                           $returnValue = Get-ExitCode ifconfig.exe '/renew'

                                                            }

                                                            msg "ifconfig.exe /renew"

                                                            msg "returnValue = $returnValue"

 

                                                            ### FIXME: we should check the returnValue from each program.

                                                            ### The original source only checks the LAST value (from ifconfig).

 

                                                            msg "ConfigureNetwork: exit (input = 1)"

                                                            return

                                             }

                              '2'

                                             {

                                                            ### put nothing here, use the fall-thru. all other choices will do a return

                                                            break

                                             }

                              ''

                                             {

                                                            msg "ConfigureNetwork: exit (Cancel)"

                                                            return

                                             }

                              'x'

                                             {

                                                            msg "ConfigureNetwork: exit (X)"

                                                            return

                                             }

                              default

                                             {

                                                            msg "ConfigureNetwork: exit ($userInput)"

                                                            Popup "Error: Invalid choice ($userInput)" $L_Configure_Title $vbError

                                                            return

                                             }

               }

 

               ### fall-thru -- choice 2

 

               $nic = InputBox "Enter the network interface name" $L_Configure_Title $script:arrNicName[ $script:defNicIndex ]

               switch ( $nic )

               {

                              'x'

                                             {

                                                            msg "ConfigureNetwork: exit (X, NIC name 2)"

                                                            return

                                             }

                              ''

                                             {

                                                            msg "ConfigureNetwork: exit (Cancel, NIC name 2)"

                                                            return

                                             }

               }

 

               if( -not ( Get-NIC $nic ) )

               {

                              return

               }

 

               $ipAddress = ''

               $netMask   = ''

               $gateway   = ''

 

               $ipAddress = InputBox "Enter the IP address of the network interface" $L_Configure_Title '192.168.1.10'

               switch ( $ipAddress )

               {

                              'x'

                                             {

                                                            msg "ConfigureNetwork: exit (X, IP address)"

                                                            return

                                             }

                              ''

                                             {

                                                            msg "ConfigureNetwork: exit (Cancel, IP address)"

                                                            return

                                             }

               }

 

               [bool] $valid = $ipAddress -match $ipRegex

               if( -not $valid )

               {

                              Popup "Value entered for IP Address ($ipAddress) is not valid. Try again." $L_Configure_Title $vbError

                              return

               }

 

               $netMask = InputBox "Enter the netmask for the network interface" $L_Configure_Title '255.255.255.0'

               switch ( $netMask )

               {

                              ''

                                             {

                                                            msg "ConfigureNetwork: exit (Cancel, Netmask)"

                                                            return

                                             }

                              'X'

                                             {

                                                            msg "ConfigureNetwork: exit (X, Netmask)"

                                                            return

                                             }

               }

 

               [bool] $valid = $netMask -match $ipRegex

               if( -not $valid )

               {

                              Popup "Value entered for netmask is not valid. Try again." $L_Configure_Title $vbError

                              return

               }

 

               $str = ''

               $gateway = ''

 

               $returnValue = MsgBox $L_Gateway_Text $L_Gateway_Title ($vbQuestion + $vbYesNo)

               if( $returnValue -eq $vbYes )

               {

                              $gateway = InputBox "Enter the gateway for the network interface" $L_Configure_Title

                              switch ( $gateway )

                              {

                                             ''

                                                            {

                                                                           msg "ConfigureNetwork: exit (Cancel, gateway)"

                                                                           return

                                                            }

                                             'X'

                                                            {

                                                                           msg "ConfigureNetwork: exit (X, gateway)"

                                                                           return

                                                            }

                              }

 

                              [bool] $valid = $gateway -match $ipRegex

                              if( -not $valid )

                              {

                                             Popup "Value entered for gateway is not valid. Try again." $L_Configure_Title $vbError

                                             return

                              }

 

                              $str = ' interface ipv4 set address name="' + $nic + '" source=static address=' +

                                             $ipAddress +

                                             ' mask=' +

                                             $netMask +

                                             ' gateway=' +

                                             $gateway

               }

               else

               {

                              $str = ' interface ipv4 set address name="' + $nic + '" source=static address=' +

                                             $ipAddress +

                                             ' mask=' +

                                             $netMask

               }

 

               msg 'netsh.exe' $str

 

               $returnValue = 0

               if( $Unsafe )

               {

                              $returnValue = Get-ExitCode 'netsh.exe' $str

               }

               msg "command = netsh.exe $str"

               msg "returnValue = $returnValue"

 

               if( $returnValue -eq 0 )

               {

                              Popup ( "Success: network interface configured successfully: " + $nic ) $L_Configure_Title $vbInformation

               }

               else

               {

                              Popup ( "Error: Failed to configure network interface: " + $nic ) $L_Configure_Title $vbError

                              msg "ConfigureNetwork: exit (configuration failure, returnValue = $returnValue)"

                              return

               }

 

               $dns = ''

               $strConfirm = MsgBox $L_DNS_Text $L_Configure_Title ($vbQuestion + $vbYesNo)

               if( $strConfirm -eq $vbYes )

               {

                              $dns = InputBox 'Enter the IP address for the DNS server' $L_Configure_Title

                              if( !$dns -or ( $dns.Length -eq 0 ) -or ( $dns -eq 'x' ) )

                              {

                                             msg "ConfigureNetwork: exit (DNS)"

                                             return

                              }

 

                              [bool] $valid = $dns -match $ipRegex

                              if( -not $valid )

                              {

                                             Popup "Value entered for DNS server is not valid" $L_Configure_Title $vbError

                                             return

                              }

               }

               elseif( $strConfirm -eq $vbCancel )

               {

                              msg "ConfigureNetwork: exit (Cancel, DNS server)"

                              return

               }

 

               $str = ' interface ipv4 add dnsserver name="' + $nic + '"' + ' address="' + $dns + '" '

               $strConfirm = MsgBox $L_DNS_Primary_Text $L_Configure_Title ($vbQuestion + $vbYesNo)

               if( $strConfirm -eq $vbYes )

               {

                              $str += ' index=1'

               }

               elseif( $strConfirm -eq $vbCancel )

               {

                              msg "ConfigureNetwork: exit (Cancel, primary DNS server)"

                              return

               }

 

               msg 'netsh.exe' $str

 

               $returnValue = 0

               if( $Unsafe )

               {

                              $returnValue = Get-ExitCode netsh.exe $str

               }

               msg "returnValue = $returnValue"

 

               if( $returnValue -ne 0 )

               {

                              Popup "Error: Failed to add DNS server (returnValue = $returnValue)" $L_Configure_Title $vbError

               }

               else

               {

                              Popup "Success: DNS server was added successfully" $L_Configure_Title $vbInformation

               }

 

               msg "ConfigureNetwork: exit"

}

 

function EnableDisableNIC

{

               $L_EnableDisable_Title = "Enable/Disable Network Interface"

 

               msg $L_Function

               msg "EnableDisableNIC: enter"

 

               $menu =

                              "1 - Disable network interface     $nl" +

                              "2 - Enable network interface      $nl" +

                              $nl                                     +

                              "X - Exit to upper level menu      $nl"

 

               $userInput = InputBox $menu $L_EnableDisable_Title '1'

 

               switch ( $userInput )

               {

                              '1'

                                             {

                                                            break

                                             }

                              '2'

                                             {

                                                            break

                                             }

                              ''

                                             {

                                                            msg "EnableDisableNIC: exit (Cancel)"

                                                            return

                                             }

                              'X'

                                             {

                                                            msg "EnableDisableNIC: exit (X)"

                                                            return

                                             }

                              default

                                             {

                                                            Popup $L_Bad_Choice $L_EnableDisable_Title $vbError

 

                                                            msg "EnableDisableNIC: return"

                                                            return

                                             }

               }

 

               $value = 'disabled'

               if( $userInput -eq '2' )

               {

                              $value = 'enabled'

               }

 

               $nic = InputBox "Enter the network interface name to set $value" $L_EnableDisable_Title $script:arrNicName[ $script:defNicIndex ]

               switch ( $nic )

               {

                              'x'

                                             {

                                                            msg "EnableDisableNIC: exit (X, NIC name 1)"

                                                            return

                                             }

                              ''

                                             {

                                                            msg "EnableDisableNIC: exit (Cancel, NIC name 1)"

                                                            return

                                             }

               }

 

               if( -not ( Get-NIC $nic ) )

               {

                              return

               }

 

               $str = ' interface set interface "' + $nic + '" ' + $value

               msg "command: netsh.exe $str"

 

               $returnValue = 0

               if( $Unsafe )

               {

                              $returnValue = Get-ExitCode netsh.exe $str

               }

 

               msg "returnValue = $returnValue"

               if( $returnValue -eq 0 )

               {

                              Popup "Success: $value network interface" $L_EnableDisable_Title $vbInformation

               }

               else

               {

                              Popup "Error: Could not set network interface to $value ($returnValue)" $L_EnableDisable_Title $vbError

               }

 

               msg "EnableDisableNIC: return"

               return

}

 

function ConfigureDNS

{

               msg $L_Function

               msg "ConfigureDNS: enter"

 

               $L_ConfigureDNS_Query =

                              "Add or remove DNS servers to your network interface settings$nl" +

                              $nl                                                               +

                              "1 - Add DNS servers                                         $nl" +

                              "2 - Remove DNS servers                                      $nl" +

                              $nl                                                               +

                              "X - Exit to upper level menu"

 

               $L_ConfigureDNS_Title = "Add | Remove DNS server"

 

               $userInput = InputBox $L_ConfigureDNS_Query $L_ConfigureDNS_Title '1'

               switch ( $userInput )

               {

                              '1'

                                             {

                                                            break

                                             }

                              '2'

                                             {

                                                            break

                                             }

                              ''

                                             {

                                                            msg "ConfigureDNS: exit (cancel)"

                                                            return

                                             }

                              'X'

                                             {

                                                            msg "ConfigureDNS: exit (X)"

                                                            return

                                             }

                              default

                                             {

                                                            Popup $L_Bad_Choice $L_ConfigureDNS_Title $vbError

 

                                                            msg "ConfigureDNS: return"

                                                            return

                                             }

               }

 

               $nic = InputBox "Enter the network interface name" $L_ConfigureDNS_Title $script:arrNicName[ $script:defNicIndex ]

               switch ( $nic )

               {

                              'x'

                                             {

                                                            msg "ConfigureDNS: exit (X, NIC name 1)"

                                                            return

                                             }

                              ''

                                             {

                                                            msg "ConfigureDNS: exit (Cancel, NIC name 1)"

                                                            return

                                             }

               }

 

               if( -not ( Get-NIC $nic ) )

               {

                              return

               }

 

               [bool] $removeAll = $false

               if( $userInput -eq '2') ## delete

               {

                              $L_RemoveAll_DNS =

                                             "Warning: This will remove DNS servers from $nic$nl" +

                                             $nl                                                  +

                                             "1 - Remove a single DNS server                 $nl" +

                                             "2 - Remove ALL DNS servers                     $nl" +

                                             $nl                                                  +

                                             "X - Exit to upper level menu"

 

                              $val = InputBox $L_RemoveAll_DNS $L_ConfigureDNS_Title '1'

                              switch ( $val )

                              {

                                             'x'

                                                            {

                                                                           msg "ConfigureDNS: exit (X, All DNS)"

                                                                           return

                                                            }

                                             ''

                                                            {

                                                                           msg "ConfigureDNS: exit (Cancel, All DNS)"

                                                                           return

                                                            }

                                             '1'

                                                            {

                                                                           break

                                                            }

                                             '2'

                                                            {

                                                                           $removeAll = $true

                                                                           break

                                                            }

                                             default

                                                            {

                                                                           Popup $L_Bad_Choice $L_ConfigureDNS_Title $vbError

 

                                                                           msg "ConfigureDNS: return"

                                                                           return

                                                            }

                              }

               }

 

               if( $removeAll )

               {

                              $dns = "all"

               }

               else

               {

                              $dns = InputBox 'Enter the IP address for the DNS server' $L_ConfigureDNS_Title

                              if( !$dns -or ( $dns.Length -eq 0 ) -or ( $dns -eq 'x' ) )

                              {

                                             msg "ConfigureDNS: exit (DNS IP address)"

                                             return

                              }

 

                              [bool] $valid = $dns -match $ipRegex

                              if( -not $valid )

                              {

                                             Popup "Value entered for DNS server is not valid: $dns" $L_ConfigureDNS_Title $vbError

                                             msg "ConfigureDNS: exit (bad DNS IP address was specified)"

                                             return

                              }

               }

 

               [bool] $primary = $false

               if( $userInput -eq '1' ) ### add dns server

               {

                              $strConfirm = MsgBox $L_DNS_Primary_Text $L_ConfigureDNS_Title ( $vbQuestion + $vbYesNo )

                              if( $strConfirm -eq $vbYes )

                              {

                                             $primary = $true

                              }

                              elseif( $strConfirm -eq $vbCancel )

                              {

                                             msg "ConfigureNetwork: exit (Cancel, primary DNS server)"

                                             return

                              }

               }

 

               $value = "delete"

               if( $userInput -eq '1' )

               {

                              $value = "add"

               }

 

               $str = ' interface ipv4 ' + $value + ' dnsserver name="' + $nic + '"' + ' address=' + $dns

               if( $primary )

               {

                              $str += " index=1"

               }

 

               msg 'netsh.exe' $str

               $returnValue = 0

               if( $Unsafe )

               {

                              $returnValue = Get-ExitCode netsh.exe $str

               }

               msg "returnValue = $returnValue"

 

               if( $returnValue -ne 0 )

               {

                              Popup "Error: Failed to $value DNS server (returnValue = $returnValue)" $L_ConfigureDNS_Title $vbError

               }

               else

               {

                              $value = "deleted"

                              if( $userInput -eq '1' )

                              {

                                             $value = "added"

                              }

                              Popup "Success: DNS server was $value successfully" $L_ConfigureDNS_Title $vbInformation

               }

 

               msg "ConfigureDNS: exit, returnValue = $($returnValue.ToString())"

}

 

function FlushDNS

{

               msg $L_Function

               msg "FlushDNS: enter"

 

               $L_FlushDNS_Title = "Flush | Register DNS"

 

               $L_FlushDNS_Query =

                              "You can flush all current DNS settings from the client-side $nl" +

                              "cache or you can re-register all DNS settings for all NICs  $nl" +

                              "on this computer.                                           $nl" +

                              $nl                                                               +

                              "1 - Flush DNS settings                                      $nl" +

                              "2 - Register DNS settings                                   $nl" +

                              $nl                                                               +

                              "X - Exit to upper level menu"

 

               $userInput = InputBox $L_FlushDNS_Query $L_FlushDNS_Title '1'

               msg "FlushDNS: userInput = $userInput"

               switch ( $userInput )

               {

                              ''

                                             {

                                                            msg "FlushDNS: exit, cancel"

                                                            return

                                             }

                              'x'

                                             {

                                                            msg "FlushDNS: exit, X"

                                                            return

                                             }

                              default

                                             {

                                                            Popup $L_Bad_Choice $L_FlushDNS_Title $vbError

 

                                                            msg "FlushDNS: return"

                                                            return

                                             }

                              '1'

                                             {

                                                            $returnvalue = 0

                                                            msg "ipconfig.exe /flushdns"

                                                            if( $Unsafe )

                                                            {

                                                                           $returnValue = Get-ExitCode ipconfig.exe "/flushdns"

                                                            }

                                                            msg "returnValue = $returnValue"

                                             }

                              '2'

                                             {

                                                            $returnvalue = 0

                                                            msg "ipconfig.exe /registerdns"

                                                            if( $Unsafe )

                                                            {

                                                                           $returnValue = Get-ExitCode ipconfig.exe "/registerdns"

                                                            }

                                                            msg "returnValue = $returnValue"

                                             }

               }

 

               msg "FlushDNS: exit"

Reply all
Reply to author
Forward
0 new messages