Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Tiberriver256/e3d1cb9ce70d8450776bc00e735b369a to your computer and use it in GitHub Desktop.
Save Tiberriver256/e3d1cb9ce70d8450776bc00e735b369a to your computer and use it in GitHub Desktop.
Retrieves the DNS Servers from all IPEnabled Network Adapters.
<#
.SYNOPSIS
Retrives DNS Servers from a computer.
.DESCRIPTION
Retrives primary, secondary, tertiery DNS Servers from on online system with Windows Management Instrimentation.
.INPUTS
System.String.
.OUPUTS
System.Management.Automation.PSObject.
.PARAMETER ComputerName
The computer to retrieve information from.
.PARAMETER Credential
PSCredential object with rights to the re
.EXAMPLE
PS C:\> Get-DNSConfiguration
.EXAMPLE
PS C:\> Get-DNSConfiguration -ComputerName 'remotepc.my.org' -Credential (Get-Credential)
.NOTES
Remote access to WMI is required.
.LINK
http://dotps1.github.io
#>
Function Get-DNSConfiguration
{
[CmdletBinding()]
[OutputType([PSObject])]
Param
(
[Parameter(ValueFromPipeLine = $true)]
[String]
$ComputerName = $env:COMPUTERNAME,
[Parameter()]
[PSCredential]
$Credential
)
Begin { }
Process
{
try
{
$gwmiParams = @{
ComputerName = $ComputerName
Class = 'Win32_NetworkAdapterConfiguration'
Namespace = 'root\cimV2'
Filter = "IPEnabled='TRUE'"
ErrorAction = 'Stop'
}
if ($ComputerName -ne $env:COMPUTERNAME -and $Credential -ne $null)
{
$gwmiParams.Add('Credential', $Credential)
}
$networkAdapters = Get-WmiObject @gwmiParams
}
catch
{
Write-Error -Message $_.ToString()
break
}
foreach ($networkAdapater in $networkAdapters)
{
$dnsServers = $networkAdapater.DNSServerSearchOrder
return [PSObject]@{
PSComputerName = $ComputerName
PrimaryDNSServer = $dnsServers[0]
SecondaryDNSSserver = $dnsServers[1]
TertieryDNSServer = $dnsServers[2]
NetworkAdapter = $networkAdapater.Description
}
}
}
End { }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment