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
… in table format
Get-NetIPServerInfo | Format-Table -AutoSize
… in html format
Get-NetIPServerInfo | ConvertTo-Html -As Table | Set-Content C:\servers.htm
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.
Categories: PowerShell, Windows Server
I can’t get the Get-NetIPServerInfo command to work on my machine even when I have imported the nettcip module, please help
LikeLike
Is there a way to specify an Active Directory OU to be queried?
LikeLike
Unfortunately not
LikeLike
Thank you for the quick response.
So if you wanted all servers in your domain you would have to specify them independently?
LikeLike
It querys all server
LikeLike
i want to know where we are defining the server name and where it will save the output
LikeLike
Hi, this is probably great for test-environments, but I wanted the same info for only one or a few computers in our pretty big domain, so I tweaked the script to accept the common “computername” parameter and only process those. Here is the script I made: https://gist.github.com/gomibushi/0c628d0adc9790e17052f04de96f39a8
LikeLike
Sorry, I don’t see how can I target a specific server. Should I put the name inside the code? At what line?
LikeLike
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Get-NetIPServerInfo.ps1
hosted with ❤ by GitHub
LikeLike