Skip to content

Instantly share code, notes, and snippets.

@dotps1
Last active January 5, 2024 08:03
Show Gist options
  • Save dotps1/c2d51c498f99dae2bb73 to your computer and use it in GitHub Desktop.
Save dotps1/c2d51c498f99dae2bb73 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