A question came up on TechNet:
“I am new to PowerShell and need a wmi script to query all the servers on my network for there dns settings and dump it out to a text file. Would someone be able to help me get started?”
This was a fairly long script, so I decided to post it here.
*EDIT*
This script receives an inordinate amount of comments, mostly requesting that I send people a copy, so I have decided to place a copy on TechNet: http://gallery.technet.microsoft.com/Gather-DNS-settings-from-fec23eaa I am also pasting the new version below:
001
002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
<#
.SYNOPSIS Get-ServerDNS – Gathers DNS settings from computers. .DESCRIPTION This script gathers DNS settings from the specified computer(s) .NOTES File Name: Get-ServerDNS.ps1 Author: Karl Mitschke Requires: Powershell V2 Created: 05/12/2010 Modified: 12/23/2011 .LINK “https://unlockpowershell.wordpress.com/2010/05/12/powershell-wmi-gather-dns-settings-for-all-servers-2/” “http://gallery.technet.microsoft.com/Gather-DNS-settings-from-fec23eaa” .EXAMPLE [CmdletBinding(SupportsShouldProcess=$false, ConfirmImpact=‘Medium’)] [Parameter( Position = 1, Mandatory = $false )] $Credential ) BEGIN{ #region PSBoundParameters modification if ($Credential -ne $null -and $Credential.GetType().Name -eq “String”){ $PSBoundParameters.Remove(“Credential”) | Out-Null $PSBoundParameters.Add(“Credential”, (Get-Credential -Credential $Credential)) } #endregion $AllServers = @() $ServerObj = @() $Member = @{ MemberType = “NoteProperty” Force = $true } } PROCESS{ $PSBoundParameters.Remove(“ComputerName”) | Out-Null foreach ($StrComputer in $ComputerName){ $NetItems = $null Write-Progress -Status “Working on $StrComputer” -Activity “Gathering Data” $ServerObj = New-Object psObject $ServerObj | Add-Member @Member -Name “Hostname” -Value $StrComputer $NetItems = @(Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter “IPEnabled = ‘True'” -ComputerName $StrComputer @PSBoundParameters) $intRowNet = 0 $ServerObj | Add-Member -MemberType NoteProperty -Name “NIC’s” -Value $NetItems.Length -Force [STRING]$MACAddresses = @() [STRING]$IpAddresses = @() [STRING]$DNS = @() [STRING]$DNSSuffix = @() foreach ($objItem in $NetItems){ if ($objItem.IPAddress.Count -gt 1){ $TempIpAdderesses = [STRING]$objItem.IPAddress $TempIpAdderesses = $TempIpAdderesses.Trim().Replace(” “, ” ; “) $IpAddresses += $TempIpAdderesses } else{ $IpAddresses += $objItem.IPAddress +“; “ } if ($objItem.{MacAddress}.Count -gt 1){ $TempMACAddresses = [STRING]$objItem.MACAddress $TempMACAddresses = $TempMACAddresses.Replace(” “, ” ; “) $MACAddresses += $TempMACAddresses +“; “ } else{ $MACAddresses += $objItem.MACAddress +“; “ } if ($objItem.{DNSServerSearchOrder}.Count -gt 1){ $TempDNSAddresses = [STRING]$objItem.DNSServerSearchOrder $TempDNSAddresses = $TempDNSAddresses.Replace(” “, ” ; “) $DNS += $TempDNSAddresses +“; “ } else{ $DNS += $objItem.{DNSServerSearchOrder} +“; “ } if ($objItem.DNSDomainSuffixSearchOrder.Count -gt 1){ $TempDNSSuffixes = [STRING]$objItem.DNSDomainSuffixSearchOrder $TempDNSSuffixes = $TempDNSSuffixes.Replace(” “, ” ; “) $DNSSuffix += $TempDNSSuffixes +“; “ } else{ $DNSSuffix += $objItem.DNSDomainSuffixSearchOrder +“; “ } $SubNet = [STRING]$objItem.IPSubnet[0] $intRowNet = $intRowNet + 1 } $ServerObj | Add-Member @Member -Name “IP Address” -Value $IpAddresses.substring(0,$ipaddresses.LastIndexOf(“;”)) $ServerObj | Add-Member @Member -Name “IP Subnet” -Value $SubNet $ServerObj | Add-Member @Member -Name “MAC Address” -Value $MACAddresses.substring(0,$MACAddresses.LastIndexOf(“;”)) $ServerObj | Add-Member @Member -Name “DNS” -Value $DNS $ServerObj | Add-Member @Member -Name “DNS Suffix Search Order” -Value $DNSSuffix $ServerObj | Add-Member @Member -Name “DNS Enabled For Wins” -Value $objItem.DNSEnabledForWINSResolution $ServerObj | Add-Member @Member -Name “Domain DNS Registration Enabled” -Value $objItem.DomainDNSRegistrationEnabled $ServerObj | Add-Member @Member -Name “Full DNS Registration Enabled” -Value $objItem.FullDNSRegistrationEnabled $ServerObj | Add-Member @Member -Name “DHCP Enabled” -Value $objItem.DHCPEnabled $ServerObj | Add-Member @Member -Name “DHCP Lease Obtained” -Value $objItem.DHCPLeaseObtained $ServerObj | Add-Member @Member -Name “DHCP Lease Expires” -Value $objItem.DHCPLeaseExpires $AllServers += $ServerObj } } END{ Write-Output -InputObject $AllServers } |
Copy and save the script as Get-ServerDNS.ps1, or download it from TechNet.
#1 by Patrik on October 21, 2010 - 20:53
First of all, this is a really great script!
$DomainName = [ADSI]” the last ” should be ”
But i still don’t get it to work, it just create the .csv file with a list of all server within the domain as a LDAP entry.
If I rem out the error action it shows this error on every found server in the domain.
Working on LDAP://CN=SRV01,CN=Computers,DC=name,DC=domain,DC=com
Get-WmiObject : Invalid parameter
At C:\dns\DNS1.ps1:11 char:19
+ $NetItems = @(gwmi <<<< Win32_NetworkAdapterConfiguration -Comp $StrComputer | where{$_.IPEnabled -eq "True"} | sele
ct @{name='IP Address';expression={$_.IPAddress}},MACAddress,DNSServerSearchOrder)
+ CategoryInfo : InvalidOperation: (:) [Get-WmiObject], ManagementException
+ FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
Any ides?
#2 by karlmitschke on October 21, 2010 - 22:23
Patrik;
I sent you a copy of the script via email – there are some bad line wraps on the blog.
i will get to fixing that soon 😉
#3 by Shimron on October 28, 2010 - 14:48
Karl,
I’m also running into the same problem Patrik has. I thought I fixed the few line wrap problems but I guess now. If I could get a copy of the script as well it would be appreciated.
#4 by karlmitschke on October 28, 2010 - 15:59
I have sent you a copy of the script.
Karl
#5 by JV33 on November 3, 2010 - 16:35
Hi Karl –
I’m getting the following error when trying to run this:
Unexpected token ‘SubTree”
$objSearcher.PageSize = 1000
$objSearcher.PropertiesToLoad.Add(“adspath”)
$objSearcher.Filter = “‘ in expression or statement.
At C:\Users\jvaligore\Desktop\get-serverdns.ps1:71 char:25
+ $objSearcher.SearchScope <<<< = "SubTree"
+ CategoryInfo : ParserError: (SubTree"
$objS…her.Filter = ":String) [], ParseException
+ FullyQualifiedErrorId : UnexpectedToken
If you could send a working copy, that would be great.
Thanks!
#6 by JV33 on November 3, 2010 - 17:41
Nevermind Karl, I got it working. Thanks!
#7 by JA on May 27, 2011 - 09:22
I have received the same error as JV33, I am guessing it is something blindingly obvious as he didn’t mention what the fix was, any clues?
#8 by Karl Mitschke on June 17, 2011 - 08:16
I sent you the latest script.
Karl
#9 by JV33 on November 3, 2010 - 17:45
Sorry, spoke too soon! Getting the same results as Patrik above. CSV file with all LDAP for the servers, but no NICs and no DNS listed.
#10 by karlmitschke on November 4, 2010 - 13:53
On line 8:
Write-Output “Working on $StrComputer”
What does $StrComputer return?
Thanks
#11 by Patrik on November 7, 2010 - 20:28
It seems that the problem is that it cant resolve the output from the object search.
I change it from “distinguishedname” to “name” and now its working, i also had to rem the Home OU and Function objects when I made the change..
I hope this help you all.
#12 by damo on January 7, 2011 - 17:03
I can get the list though no information on DNS. What do you need to change in the script to output the dns settings of the servers? Help would be much appreciated.
thanks
Damo
#13 by Karl Mitschke on January 10, 2011 - 09:36
Damo;
I sent you a copy of the script. If it works, I will update the script on this post.
Karl
#14 by yanolezard on January 25, 2011 - 10:50
Hello Karl Mitschke !
This script about DNS server with Powershell seems to be great. In order to test it without error, can you send me a copy to my email address ?
Thanks a lot for this work.
Yano
#15 by Karl Mitschke on January 25, 2011 - 13:20
I have sent the script. Please let me know how it works for you.
Karl
#16 by Karl Mitschke on January 25, 2011 - 13:41
I have modified the ListAdServers functiuon as follows:
Function ListADServers
{
$Computers = @()
$DomainName = [ADSI]”
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $DomainName
$objSearcher.SearchScope = “SubTree”
$objSearcher.PageSize = 1000
$objSearcher.Filter = “(&(objectCategory=computer)(OperatingSystem=Windows*Server*))”
$colResults = $objSearcher.FindAll()
foreach ($objResult in $colResults)
{
$Computers += $objResult.Properties.dnshostname
}
}
Please try this versioj in place of the version in the post.
#17 by Mack on April 26, 2011 - 03:01
Hi – seem to be getting all the same errors as everyone else. Any chance you could email me the script too please. Would be greatly appreciated.
Mack
#18 by Karl Mitschke on August 9, 2011 - 11:02
Mack;
Sent
Karl
#19 by Frank on April 26, 2011 - 14:16
Could you please pass on a working version of this script – I think my formatting is breaking it somehow.
Thanks!
#20 by Karl Mitschke on August 9, 2011 - 11:01
Frank;
Sent
Karl
#21 by davetechsearch on May 11, 2011 - 13:04
getting error below running the script (with the modified ListADServers)
Unexpected token ‘SubTree”
$objSearcher.PageSize = 1000
$objSearcher.Filter = “‘ in expression or statement.
At line:87 char:24
+ $objSearcher.SearchScop <<<< e = "SubTree"
+ CategoryInfo : ParserError: (SubTree"
$objSe…cher.Filter = ":String) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
can you please send me a copy of your working script?
Thank you!
#22 by Karl Mitschke on June 17, 2011 - 08:17
Sent.
karl
#23 by Ryan on June 15, 2011 - 17:51
Karl,
Can you send me the script? I’d rather not try to sort out the wrap issues and would rather have the script sent.
Thanks!
Ryan.
#24 by Karl Mitschke on June 17, 2011 - 08:12
Sent.
I hope to have this post updated soon….
Karl
#25 by w00tm3 on July 15, 2011 - 15:32
Hi Karl. If I could get a copy of the script it would be greatly appreciated! Thanks!
#26 by Karl Mitschke on August 9, 2011 - 11:00
Sent.
Karl
#27 by Ty Mote on July 20, 2011 - 11:54
Karl, can I get a ZIP of the latest version? Thanks!
#28 by Karl Mitschke on August 9, 2011 - 10:58
Ty;
Sent
Karl
#29 by jkavanagh58 on July 31, 2011 - 06:09
Nice code. I have a similar script, the only difference is I do a test-connection in my server name collection process to save time from trying to query a record for a computer that was not removed from AD.
#30 by Karl Mitschke on August 1, 2011 - 08:30
That’s a great idea!
I will have to add that in my next revision
#31 by Nic on August 23, 2011 - 00:01
I got the script to work as follows:
* copying and pasting the script at the top into a text file and changed the extention to .ps1
* added in the new code from Karl’s post in #16
* completed a find and replace for all the speach marks (“) and appostrophes (‘).
* deleted the speech mark from Karl’s post #16 beside the text: [ADSI]
* ran the command from Windows Power Shell running as Administrator> .\script.ps1
#32 by Karl Mitschke on August 23, 2011 - 08:26
I am having trouble with quotes becoming smart quotes. I hoe to have that figured out soon.
In my comment 16, it’s [ADSI]’ ‘ Where ‘ ‘ are two single quotes.
#33 by Klaus on November 18, 2011 - 04:54
can you please also send me this script?
thank you!
#34 by Karl Mitschke on November 21, 2011 - 11:00
On its way.
Karl
#35 by Damian on November 24, 2011 - 08:52
Please, would you mind sending the script also to me?
Thank you in advance.
#36 by Karl Mitschke on November 28, 2011 - 11:12
Sent…
Karl
#37 by Nibbix on December 22, 2011 - 05:57
Hi Karl, Can you send me the script also ???
Thanks in advanced
#38 by Karl Mitschke on December 23, 2011 - 09:59
Sent
#39 by Jim Parent on March 21, 2013 - 12:05
Can I get a copy of this script?
#40 by Karl Mitschke on May 30, 2013 - 15:18
Can you please download it from TechNet?
http://gallery.technet.microsoft.com/Gather-DNS-settings-from-fec23eaa
Or, let me know of any issues you are having with the version posted here…