PowerShell

Retrieve the IP configuration of all Windows Servers with Get-NetIPServerInfo

In this blog post it’s all about documentation. Do you know the IP settings of your Windows servers? Do you have a documentation? If not, you’ve come to the right place. I will give you an advanced function in hand that enables you to obtain the IP configuration of all your servers. With PowerShell, what else?

The Output

Let’s first talk about the goal. We want to retrieve the IP address, the interface, the default gateway and last but not least the configured DNS servers. That DNS server settings could become crucial if you plan to replace your domain controllers with new ones and with different IP addresses than the old servers.

Note that all servers should be joined to the same domain. Here it is in action and in list format:


Get-NetIPServerInfo

1.png

… in table format


Get-NetIPServerInfo | Format-Table -AutoSize

2.png

… in html format


Get-NetIPServerInfo | ConvertTo-Html -As Table | Set-Content C:\servers.htm

3.png

Nice ones. So, what’s next? Sure, the code.

Get-NetIPServerInfo

# .SYNOPSIS
# Get-NetIPServerInfo gets the IP configuration of all domain joined Windows servers.

# .DESCRIPTION
# Uses Test-Connection to check if the server is powered on and reachable.

# .EXAMPLE
# Get-NetIPServerInfo | Format-Table -AutoSize

# .NOTES
# Author: Patrick Gruenauer
# Web: https://sid-500.com

function Get-NetIPServerInfo {

$getc=(Get-ADComputer -Filter 'operatingsystem -like "*server*"-and enabled -eq "true"').Name
$test=Test-Connection -Destination $getc -Count 1 `
-ErrorAction SilentlyContinue
$reach=$test | Select-Object -ExpandProperty Address
$result=@()

foreach ($c in $reach)

{
$i=Invoke-Command -ComputerName $c -ScriptBlock {

Get-NetIPConfiguration |
Select-Object `
-Property InterfaceAlias,Ipv4Address,DNSServer
Get-NetRoute -DestinationPrefix '0.0.0.0/0' |
Select-Object -ExpandProperty NextHop}

$result +=New-Object -TypeName PSCustomObject -Property ([ordered]@{
'Server'= $c
'Interface' = $i.InterfaceAlias -join ','
'IPv4Address' = $i.Ipv4Address.IPAddress -join ','
'Gateway' = $i | Select-Object -Last 1
'DNSServer' = ($i.DNSServer |
Select-Object -ExpandProperty ServerAddresses) -join ','

})

}
$result
}

How to use it

Copy the code into your PowerShell ISE session and run the code. Then type the command and have fun with it.

If you want to make the function permanent available, so that the function is there every time you start PowerShell, you have to create a folder in C:\Program Files\WindowsPowerShell\Modules. Name it Get-NetIPServerInfo. Then save the code as .psm1 file in that folder. The screenshot below will be a great help.

4.png

12 replies »

  1. I can’t get the Get-NetIPServerInfo command to work on my machine even when I have imported the nettcip module, please help

    Like

    • I see now that you had the same as me in mind – I made some changes and it now should work with one or more “computername” piped to it, or as arguments.


      # .SYNOPSIS
      # Get-NetIPServerInfo gets the IP configuration of all Computers passed as parameters
      # .DESCRIPTION
      # Uses Test-Connection to check if the server is powered on and reachable.
      # .EXAMPLE
      # Get-NetIPServerInfo -Computername server1, server2
      #.EXAMPLE
      # 'server1', 'server2' | Get-NetIPServerInfo | Format-Table
      # .NOTES
      # Author: Patrick Gruenauer
      # Web: https://sid-500.com
      #
      # Made to accept computernames as parameters by Pål Røtnes
      # gomibushi@gmail.com
      function Get-NetIPServerInfo {
      [CmdletBinding()]
      Param (
      [parameter(Mandatory=$true,ValueFromPipeline=$true)]
      [Array]$Computername
      )
      Process
      {
      foreach ($c in $Computername)
      {
      $i=Invoke-Command -ComputerName $c -ScriptBlock {
      Get-NetIPConfiguration |
      Select-Object `
      -Property InterfaceAlias,Ipv4Address,DNSServer
      Get-NetRoute -DestinationPrefix '0.0.0.0/0' |
      Select-Object -ExpandProperty NextHop
      }
      $result = $null
      $result +=New-Object -TypeName PSCustomObject -Property ([ordered]@{
      'Server'= $c
      'Interface' = $i.InterfaceAlias -join ','
      'IPv4Address' = $i.Ipv4Address.IPAddress -join ','
      'Gateway' = $i | Select-Object -Last 1
      'DNSServer' = ($i.DNSServer |
      Select-Object -ExpandProperty ServerAddresses) -join ','
      })
      $result
      }
      }
      }

      Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.