Saturday, 6 July 2013

Script for get the servers NIC Ip configuration

Import-Module ActiveDirectory
$Computer = "Target"

$ComputerInfo = Get-ADDomainController -Identity $Computer
$DCIP = $ComputerInfo.IPv4Address

#########################################
# Get the Server's NIC IP Configuration #
#########################################
Write-Verbose "Enumerate all connected network interfaces and Set the filter to only find Ethernet 802.3 NICs, that are enabled and conected `r "
Write-Verbose "This SHOULD only return the correct NIC. If not, IP Address data will not display correctly. `r "
Write-Verbose "There should only be 1 active NIC on a DC. `r "
Write-Output "Gathering $computer NIC configuration data from all active NICs on $computer... `r "

Write-Verbose "Gather the configured NICs on the computer and identify the one with a configured IP & DefaultGateway `r "
$NICConfig = get-wmiobject -Class win32_networkadapterconfiguration -computername $Computer -property *
$PrimaryNIC = $NICConfig | ? {$_.IPAddress -and $_.IPAddress -notcontains "0.0.0.0" -and $_.DefaultIPGateway }
$NICCount = $PrimaryNIC.Count
IF ($NICCount -gt 1)
 { ## OPEN IF there are multiple Primary NICs discovered
 $PrimaryNIC = $NICConfig | ? {$_.IPAddress -contains "$DCIP" -and $_.DefaultIPGateway }
 } ## CLOSE IF there are multiple Primary NICs discovered

$NICIndex = $PrimaryNIC.Index
$ActiveNIC = get-wmiobject -Class Win32_NetworkAdapter -computername $Computer -filter "Index=$NICindex"
$NICDescName = $ActiveNIC.Name
$NICNAME = $ActiveNIC.NetConnectionID
$NICConnStatus = $ActiveNIC.NetConnectionStatus

Write-Output "Found the NIC $NICDescName labeled [$NICNAME] ..." `r

$PrimaryNICconfig = get-wmiobject Win32_NetworkAdapterConfiguration -computername $Computer -filter "Index=$NICindex"
ForEach ($NetConfig in $PrimaryNICconfig)
 { ## OPEN Bracket ForEACH Loop for NIC Info
 #Get NIC info
 $MACAddress = $NetConfig.MACAddress

 $IPAddressArray = $NetConfig.IPAddress -split "`t "
 $IPAddressIPV4 = $IPAddressArray[0]
 $IPAddressIPv6 = $IPAddressArray[1]

 $SubnetMaskArray = $NetConfig.IPSubnet -split "`t "
 $SubnetMaskIPv4 = $SubnetMaskArray[0]
 $SubnetMaskIPv6 = $SubnetMaskArray[1]

 $DefaultGateway = $NetConfig.DefaultIPGateway

 $DNSServerArray = $NetConfig.DNSServerSearchOrder -split "`t "
 $PrimaryDNSServer = $DNSServerArray[0]
 $SecondaryDNSServer = $DNSServerArray[1]
 $TertiaryDNSServer = $DNSServerArray[2]

 [string]$PrimaryWINSServer = $NetConfig.winsprimaryserver
 [string]$SecondaryWINSServer = $NetConfig.winsSecondaryServer

 $DNSSuffixSearchArray= $NetConfig.DNSDomainSuffixSearchOrder -split "`t "
 $DNSSuffix1 = $DNSSuffixSearchArray[0]
 $DNSSuffix2 = $DNSSuffixSearchArray[1]
 $DNSSuffix3 = $DNSSuffixSearchArray[2]
 $DNSSuffix4 = $DNSSuffixSearchArray[3]
 } ## CLOSE Bracket ForEACH Loop for NIC Info

#######################################
# Display Gathered Configuration Data #
#######################################
#Display gathered data
Write-Output "Found the following network information for $computer..." `r
Write-Output "Server Name : " $Computer `r
Write-Output "NIC : " $NICDescName `r
Write-Output "NIC Name : " $NICName `r
Write-Output "NIC Status : " $NICConnStatus `r
Write-Output "MAC Address : " $MACAddress `r
Write-Output "IPAddress : " $IPAddressIPv4 `r
Write-Output "SubnetMask : " $SubnetMaskIPv4 `r
Write-Output "Default Gateway : " $DefaultGateway `r
Write-Output "DNS Servers : " $DNSServerList `r
Write-Output "DNS Primary Server : " $PrimaryDNSServer `r
Write-Output "DNS Secondary Server : " $SecondaryDNSServer `r
Write-Output "DNS Tertiary Server : " $TertiaryDNSServer `r
Write-Output "WINS Primary Server : " $PrimaryWINSServer `r
Write-Output "WINS Secondary Server : " $SecondaryWINSServer `r
Write-Output "DNS Search Suffixes : " $DNSSearchSuffixList `r
Write-Output "DNS Search Suffix 1 : " $DNSSearchSuffix1 `r
Write-Output "DNS Search Suffix 2 : " $DNSSearchSuffix2 `r
Write-Output "DNS Search Suffix 3 : " $DNSSearchSuffix3 `r

No comments:

Post a Comment