Powershell for IP information of remote machines

First off, I must appologise, this Power shell script is not my own.  Worst, I cannot remember where I got the script from.  I will endeavour to credit the author as soon as I find them.

With that in mind...Some time ago I needed to find out what the DNS settings on our servers were, and as they are manually configured i.e. no DHCP this would mean jumping on each box (200+) and taking a peak.  This screamed Power Shell at me!

This little script will run off and return to you the IP configuration of any server you can access.  It does a nice little check to see if the server is on first before trying to get the IP information.

I added the write out to a file bit but the original just had the output sent to the console.

Here's the code.

[cmdletbinding()]
param (
    [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
    [string[]]$ComputerName = $env:computername
)            

begin {}
process {
    foreach ($Computer in $ComputerName) {
        if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {
            $Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer | ? {$_.IPEnabled}
            foreach ($Network in $Networks) {
                $IPAddress  = $Network.IpAddress[0]
                $SubnetMask  = $Network.IPSubnet[0]
                $DefaultGateway = $Network.DefaultIPGateway
                $DNSServers  = $Network.DNSServerSearchOrder
                $IsDHCPEnabled = $false
                
                If($network.DHCPEnabled) {
                    $IsDHCPEnabled = $true
                }
                $MACAddress  = $Network.MACAddress
                $OutputObj  = New-Object -Type PSObject
                $OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()
                $OutputObj | Add-Member -MemberType NoteProperty -Name IPAddress -Value $IPAddress
                $OutputObj | Add-Member -MemberType NoteProperty -Name SubnetMask -Value $SubnetMask
                $OutputObj | Add-Member -MemberType NoteProperty -Name Gateway -Value (@($DefaultGateway) -join, ":")
                $OutputObj | Add-Member -MemberType NoteProperty -Name IsDHCPEnabled -Value $IsDHCPEnabled
                $OutputObj | Add-Member -MemberType NoteProperty -Name DNSServers -Value (@($DNSServers) -join ,":")
                $OutputObj | Add-Member -MemberType NoteProperty -Name MACAddress -Value $MACAddress

                # Write out to a file...
                $OutputObj | Add-Content C:\temp\domaincontroller_dnsconf.txt
            }
        }
    }
}            

end {}

Comments

Popular posts from this blog

PXE booting, MDT and 802.1x

Intune installation requires a wire...or does it?

Security Policy 1001