Came across a unique request to get primary, secondary, and tertiary DNS values for multiple computers/servers across the domain. I started writing the script and got what I wanted.
Now this started off as just to query for DNS Server information, but then I thought to add other pieces to get myself a good Network Inventory of all the servers in the environment.
I am utilizing the Win32_NetworkAdapterConfiguration WMI Class to get the required information.
You can modify the script below to suit your needs. The complete list of settings that can be captured:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
string Caption; string Description; string SettingID; boolean ArpAlwaysSourceRoute; boolean ArpUseEtherSNAP; string DatabasePath; boolean DeadGWDetectEnabled; string DefaultIPGateway[]; uint8 DefaultTOS; uint8 DefaultTTL; boolean DHCPEnabled; datetime DHCPLeaseExpires; datetime DHCPLeaseObtained; string DHCPServer; string DNSDomain; string DNSDomainSuffixSearchOrder[]; boolean DNSEnabledForWINSResolution; string DNSHostName; string DNSServerSearchOrder[]; boolean DomainDNSRegistrationEnabled; uint32 ForwardBufferMemory; boolean FullDNSRegistrationEnabled; uint16 GatewayCostMetric[]; uint8 IGMPLevel; uint32 Index; uint32 InterfaceIndex; string IPAddress[]; uint32 IPConnectionMetric; boolean IPEnabled; boolean IPFilterSecurityEnabled; boolean IPPortSecurityEnabled; string IPSecPermitIPProtocols[]; string IPSecPermitTCPPorts[]; string IPSecPermitUDPPorts[]; string IPSubnet[]; boolean IPUseZeroBroadcast; string IPXAddress; boolean IPXEnabled; uint32 IPXFrameType[]; uint32 IPXMediaType; string IPXNetworkNumber[]; string IPXVirtualNetNumber; uint32 KeepAliveInterval; uint32 KeepAliveTime; string MACAddress; uint32 MTU; uint32 NumForwardPackets; boolean PMTUBHDetectEnabled; boolean PMTUDiscoveryEnabled; string ServiceName; uint32 TcpipNetbiosOptions; uint32 TcpMaxConnectRetransmissions; uint32 TcpMaxDataRetransmissions; uint32 TcpNumConnections; boolean TcpUseRFC1122UrgentPointer; uint16 TcpWindowSize; boolean WINSEnableLMHostsLookup; string WINSHostLookupFile; string WINSPrimaryServer; string WINSScopeID; string WINSSecondaryServer; |
Since the scripts are querying for information it is best if it runs from a DC or a privileged server with an account that has privileged access.
To get the results you need the following two scripts:
Get-NetworkInfo.ps1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
<# .SYNOPSIS Get Network Information for a list of servers .DESCRIPTION Get Network Information for a list of servers (Primary, Secondary, Tertiary DNS Values and more) .NOTES Needs to be run in PowerShell with elevated permissions (run as administrator). Version: 1.0 Author: Mohammed Wasay Email: [email protected] Web: www.mowasay.com Creation Date: 02/13/2020 .COMPONENT No components or modules are required to run this script. .Parameter ComputerName List of Computername or single computer name can be used as a parameter. If no value is specified it will just return results of the local computer. .EXAMPLE .\Get-NetworkInfo.ps1 -ComputerName $Server #> [cmdletbinding()] param ( [parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [string[]] $ComputerName = $env:computername ) begin { } process { foreach ($Computer in $ComputerName) { Write-Verbose "Working on $Computer" if (Test-Connection -ComputerName $Computer -Count 1 -ea 0) { try { $Networks = Get-WmiObject -Class Win32_NetworkAdapterConfiguration ` -Filter IPEnabled=TRUE ` -ComputerName $Computer ` -ErrorAction Stop } catch { Write-Verbose "Failed to Query $Computer. Error details: $_" continue } foreach ($Network in $Networks) { $IPv4 = $Network.IPAddress[0] $IPv6 = $Network.IPXAddress $MACAddress = $Network.MACAddress $Gateway = $Network.DefaultIPGateway[0] $DNSDomain = $Network.DNSDomain $DNSServers = $Network.DNSServerSearchOrder $PrimaryWINS = $Network.WINSPrimaryServer $SecondaryWINS = $Network.WINSSecondaryServer $NetworkName = $Network.Description $NetBios = $Network.TcpipNetbiosOptions If (!$DNSServers) { $PrimaryDNSServer = "Notset" $SecondaryDNSServer = "Notset" $TertiaryDNSServer = "Notset" } elseif ($DNSServers.count -eq 1) { $PrimaryDNSServer = $DNSServers[0] $SecondaryDNSServer = "Notset" $TertiaryDNSServer = "Notset" } elseif ($DNSServers.count -eq 2) { $PrimaryDNSServer = $DNSServers[0] $SecondaryDNSServer = $DNSServers[1] $TertiaryDNSServer = "Notset" } else { $PrimaryDNSServer = $DNSServers[0] $SecondaryDNSServer = $DNSServers[1] $TertiaryDNSServer = $DNSServers[2] } If (!$PrimaryWINS) { $PrimaryWINS = "Notset" } If (!$SecondaryWINS) { $SecondaryWINS = "Notset" } If ($network.DHCPEnabled) { $IsDHCPEnabled = $true } If ($Network.TcpipNetbiosOptions = 0) { $NetBios = "Default" } elseIf ($Network.TcpipNetbiosOptions = 1) { $NetBios = "Enabled" } elseIf ($Network.TcpipNetbiosOptions = 2) { $NetBios = "Disabled" } $OutputObj = New-Object -Type PSObject $OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper() $OutputObj | Add-Member -MemberType NoteProperty -Name NetBios -Value $NetBios $OutputObj | Add-Member -MemberType NoteProperty -Name IPv4 -Value $IPv4 $OutputObj | Add-Member -MemberType NoteProperty -Name IPv6 -Value $IPv6 $OutputObj | Add-Member -MemberType NoteProperty -Name Gateway -Value $Gateway $OutputObj | Add-Member -MemberType NoteProperty -Name MACAddress -Value $MACAddress $OutputObj | Add-Member -MemberType NoteProperty -Name DNSDomain -Value $DNSDomain $OutputObj | Add-Member -MemberType NoteProperty -Name PrimaryDNSServer -Value $PrimaryDNSServer $OutputObj | Add-Member -MemberType NoteProperty -Name SecondaryDNSServer -Value $SecondaryDNSServer $OutputObj | Add-Member -MemberType NoteProperty -Name TertiaryDNSServer -Value $TertiaryDNSServer $OutputObj | Add-Member -MemberType NoteProperty -Name PrimaryWINSServer -Value $PrimaryWINS $OutputObj | Add-Member -MemberType NoteProperty -Name SecondaryWINSServer -Value $SecondaryWINS $OutputObj | Add-Member -MemberType NoteProperty -Name IsDHCPEnabled -Value $IsDHCPEnabled $OutputObj | Add-Member -MemberType NoteProperty -Name NetworkName -Value $NetworkName $OutputObj } } else { Write-Verbose "$Computer not reachable" } } } end { } |
Get-Remote-NetworkInfo.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#Get-Remote-NetworkInfo.ps1 Import-Module ActiveDirectory #Get all the domain controllers in the domain $Servers = Get-ADDomainController -Filter * #Path to the output file $output = "C:\temp\$env:USERDOMAIN-Servers-NetworkInfo.csv" foreach ($Server in $Servers.Hostname) { #Make sure the path of the previous file is the same, or modify the below. Get-NetworkInfo.ps1 to the location where the file was copied. In my case (C:\temp) .\Get-NetworkInfo.ps1 -ComputerName $Server | Export-Csv -Path $output -NoTypeInformation -Append -ErrorAction SilentlyContinue } Write-Host -ForegroundColor Green "Done!" |