Wednesday, June 18, 2014

Powershell script to read and validate Windows DNS servers

If you've ever had to administer a Windows DNS server you likely know that there are limited tools for cleanup.  I wrote this script to read the entire list of entries from a specific domain in DNS and then ping each address to see if it was live or not.  That way I can easily go back and investigate the ones are show up as dead.  It writes it all to an Excel spreadsheet.

Watch for word-wrapping...



#======================================================================================
#         File Name : DNS-Validator.ps1
#   Original Author : Kenneth C. Mazie (kcmjr AT kcmjr.com)
#                   :
#       Description : Used to read entries from Windows DNS and ping check each.
#                   :
#             Notes : Set the name of your DNS server and domain within the script.
#                   : Execute with a user ID with domain admin permissions.
#                   : Creates an Excel spreadsheet and populates it with IP addresses
#                   : and resolved names from DNS, then pings each.  Also
#                   : lists a reverse count of the total number of entries just because.
#                   :
#          Warnings : None
#                   :  
#             Legal : Public Domain. Modify and redistribute freely. No rights reserved.
#                   : SCRIPT PROVIDED "AS IS" WITHOUT WARRANTIES OR GUARANTEES OF
#                   : ANY KIND. USE AT YOUR OWN RISK. NO TECHNICAL SUPPORT PROVIDED.
#                   :
#           Credits : Code snippets and/or ideas came from many sources including but
#                   :   not limited to the following:
#                   :
#    Last Update by : Kenneth C. Mazie
#   Version History : v1.0 - 06-10-14 - Original
#    Change History : v1.1 -
#                   :
#=======================================================================================

clear-host
$ErrorActionPreference="silentlycontinue"

$DNS_Server = "my-dns-server"
$DomainName = "my-dns-domain"


$excel = New-Object -ComObject Excel.Application
$excel.Visible = $true
$workbook = $excel.Workbooks.Add()
$sheet = $workbook.ActiveSheet
$counter = 0

$dns_entries = Get-WmiObject -namespace "root\MicrosoftDNS" -class MicrosoftDNS_Atype -ComputerName $DNS_server -Filter $DomainName
$count = $dns_entries.count

$column = 1
$range = $Sheet.Range("a1","d1")
$range.Style = 'Title'
$range.font.bold = $True
$range.Interior.ColorIndex = 24
$sheet.cells.Item(1,$column++) = "IP Address"
$sheet.cells.Item(1,$column++) = "Hostname"
$sheet.cells.Item(1,$column++) = "Ping Result"
$sheet.cells.Item(1,$column++) = "Count"
$row=2

foreach ($dnsrecord in $dns_entries) {
  $Column = 1   
   $sheet.cells.Item($row,$column).activate()  
      $sheet.cells.Item($row,$column++) = $dnsrecord.IPAddress
      $sheet.cells.Item($row,$column++) = $dnsrecord.OwnerName.split(".")[0] 
     
      if (Test-connection -Count 1 -ComputerName $dnsrecord.IPAddress){
        $sheet.cells.item($row,$column).font.ColorIndex = 10
      $sheet.cells.Item($row,$column++) = "Successful"
      }else {
        $sheet.cells.item($row,$column).font.ColorIndex = 3
        $sheet.cells.item($row,$column).font.bold = $true
        $sheet.cells.Item($row,$column) = "FAILED"
        $column++
        $sheet.cells.item($row,$column).font.bold = $false
      }

      $sheet.cells.Item($row,$column++) = $count
     
     
      $WorkSheet = $Workbook.worksheets.Item(1)
    $UsedRange = $WorkSheet.UsedRange
    $UsedRange.EntireColumn.AutoFit() | Out-Null
    $dataRange = $Sheet.Range(("A{0}" -f $row),("D{0}" -f $row))
    $UsedCols = $UsedRange.Columns.Count 

    1..$UsedCols | ForEach {
      $dataRange.Borders.Item($_).LineStyle = 1
      $dataRange.Borders.Item($_).Weight = 12
    }

      $count --
      $row ++
}
$workbook.SaveAs("C:\DNS-Validator.xlsx")
$excel.Quit()

Write-Host COMPLETED

Powershell script that will set GVLK keys

Have you ever needed to programmatically set the KMS keys on a bunch of systems?  This PowerShell script allows you to do that.

Feed the script with the name of the KMS server, or you can hard-code it if you prefer.  

The script detects the OS version and sets the appropriate GVLK (generic) KMS key.  It then sets the  location of the KMS server itself and attempts to activate the system.  Lastly it pops up the result.  Everything is done by leveraging the existing SLMGR VB script on every Windows system.

Place the script on a common server and call it from the system to be activated.  That can be from a one-time run key, manually, or from a remote PowerShell session.

I also included the full list of GVLK keys and Windows OS version numbers at the end for reference.  Remember that by themselves these keys will not activate systems, you need to use them in conjunction with a fully licensed KMS server.



#======================================================================================
#         File Name : Register-GVLK.ps1
#   Original Author : Kenneth C. Mazie (kcmjr AT kcmjr.com)
#                   :
#       Description : Used to set generic KMS keys on target system.
#                   :
#             Notes : Can be used with command line option to specify KMS server or not.
#                   :
#                   :
#          Warnings :
#                   :  
#             Legal : Public Domain. Modify and redistribute freely. No rights reserved.
#                   : SCRIPT PROVIDED "AS IS" WITHOUT WARRANTIES OR GUARANTEES OF
#                   : ANY KIND. USE AT YOUR OWN RISK. NO TECHNICAL SUPPORT PROVIDED.
#                   :
#           Credits : Code snippets and/or ideas came from many sources including but
#                   :   not limited to the following:
#                   :
#    Last Update by : Kenneth C. Mazie
#   Version History : v1.0 - 06-15-14 - Original
#    Change History : v1.1 - 
#                   : 
#                   :
#=======================================================================================



Clear-Host


$computer = "localhost"
#$Server = "mykms-server"


Param(
  [string]$Server
)

$Rearm = $False  #--[ Change to $true if you want to rearm the registration.

#--[ Force to execute with admin priviledge ]--
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = new-object Security.Principal.WindowsPrincipal $identity
if ($principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)  -eq $false) {
  $Args = '-noprofile -nologo -executionpolicy bypass -file "{0}"' -f $MyInvocation.MyCommand.Path
  Start-Process -FilePath 'powershell.exe' -ArgumentList $Args -Verb RunAs
  exit
  }

If ($Rearm -eq $True){
  If(!(test-path "c:\Scripts\rearm.flag")){
    New-Item "c:\scripts\rearm.flag" -type file
    iex "slmgr -rearm"
    sleep -sec 3
    iex "shutdown -r -t 0"
  }
}

$os = Get-WmiObject -class Win32_OperatingSystem -computerName $computer

Switch ($os.Version)
  {
    "5.1.2600" { $ProdDesc = "xp" }
    "5.1.3790" { $ProdDesc = "2003" }
                 
  "6.0.6001"
    {
      If($os.ProductType -eq "1"){$ProdKey = "VKK3X-68KWM-X2YGT-QR4M6-4BWMV"; $ProdDesc = "Windows Vista" }
        If($os.ProductType -eq "2"){$ProdKey = "TM24T-X9RMF-VWXK6-X8JC9-BFGM2"; $ProdDesc = "Windows Server 2008"}
        If($os.ProductType -eq "3"){$ProdKey = "TM24T-X9RMF-VWXK6-X8JC9-BFGM2"; $ProdDesc = "Windows Server 2008"}
    }

  "6.1.7601"
    {
        If($os.ProductType -eq "1"){$ProdKey = "FJ82H-XT6CR-J8D7P-XQJJ2-GPDD4"; $ProdDesc = "Windows 7"}
        If($os.ProductType -eq "2"){$ProdKey = "YC6KT-GKW9T-YTKYR-T4X34-R7VHC"; $ProdDesc = "Windows Server 2008 R2 Domain Controller"}
        If($os.ProductType -eq "3"){$ProdKey = "YC6KT-GKW9T-YTKYR-T4X34-R7VHC"; $ProdDesc = "Windows Server 2008 R2"}
    }
     
  "6.2.9200"     
    {
        If($os.ProductType -eq "1"){$ProdKey = "NG4HW-VH26C-733KW-K6F98-J8CK4"; $ProdDesc = "Windows 8"}
        If($os.ProductType -eq "2"){$ProdKey = "48HP8-DN98B-MYWDG-T2DCC-8W83P"; $ProdDesc = "Windows Server 2012 Domain Controller"}
        If($os.ProductType -eq "3"){$ProdKey = "48HP8-DN98B-MYWDG-T2DCC-8W83P"; $ProdDesc = "Windows Server 2012"}
    }                  
 
  "6.3.9600"
    {
        If($os.ProductType -eq "1"){$ProdKey = "GCRJD-8NW9H-F2CDX-CCM8D-9D6T9"; $ProdDesc = "Windows 8.1"}
        If($os.ProductType -eq "2"){$ProdKey = "W3GGN-FT8W3-Y4M27-J84CP-Q3VJ9"; $ProdDesc = "Windows Server 2012 R2 Domain Controller"}
      If($os.ProductType -eq "3"){$ProdKey = "W3GGN-FT8W3-Y4M27-J84CP-Q3VJ9"; $ProdDesc = "Windows Server 2012 R2"}
    }
                    
  DEFAULT { "Version not listed" }

  }

$wShell = New-Object -ComObject Wscript.Shell
$Message = $wShell.Popup("Activating $ProdDesc",5,"Windows Activation Script",64)
If ($Server = "DNS"){
  iex "slmgr /ckms"
}Else{
  iex "slmgr /skms $Server:1688"
} 
sleep -Seconds 3
iex "slmgr /ipk $ProdKey"
sleep -Seconds 3
iex "slmgr /ato"
iex "slmgr /dlv"
 
#$ProdDesc
$wshell = $null

<#------ NOTES ------
Operating system edition                          KMS Client GVLK Setup Key
-------------------------                       -----------------------------
Windows 8.1 Professional                          GCRJD-8NW9H-F2CDX-CCM8D-9D6T9
Windows 8.1 Professional N                        HMCNV-VVBFX-7HMBH-CTY9B-B4FXY
Windows 8.1 Enterprise                          MHF9N-XY6XB-WVXMC-BTDCT-MKKG7
Windows 8.1 Enterprise N                          TT4HM-HN7YT-62K67-RGRQJ-JFFXW
Windows 8 Professional                          NG4HW-VH26C-733KW-K6F98-J8CK4
Windows 8 Professional N                          XCVCF-2NXM9-723PB-MHCB7-2RYQQ
Windows 8 Enterprise                            32JNW-9KQ84-P47T8-D8GGY-CWCK7
Windows 8 Enterprise N                          JMNMF-RHW7P-DMY6X-RF3DR-X2BQT
Windows Server 2012 R2 Server Standard            D2N9P-3P6X9-2R39C-7RTCD-MDVJX
Windows Server 2012 R2 Datacenter               W3GGN-FT8W3-Y4M27-J84CP-Q3VJ9
Windows Server 2012 R2 Essentials               KNC87-3J2TX-XB4WP-VCPJV-M4FWM
Windows Server 2012 Core                          BN3D2-R7TKB-3YPBD-8DRP2-27GG4
Windows Server 2012 Core N                        8N2M2-HWPGY-7PGT9-HGDD8-GVGGY
Windows Server 2012 Core Single Language        2WN2H-YGCQR-KFX6K-CD6TF-84YXQ
Windows Server 2012 Core Country Specific       4K36P-JN4VD-GDC6V-KDT89-DYFKP
Windows Server 2012 Server Standard             XC9B7-NBPP2-83J2H-RHMBY-92BT4
Windows Server 2012 Standard Core               XC9B7-NBPP2-83J2H-RHMBY-92BT4
Windows Server 2012 MultiPoint Standard           HM7DN-YVMH3-46JC3-XYTG7-CYQJJ
Windows Server 2012 MultiPoint Premium            XNH6W-2V9GX-RGJ4K-Y8X6F-QGJ2G
Windows Server 2012 Datacenter                      48HP8-DN98B-MYWDG-T2DCC-8W83P
Windows Server 2012 Datacenter Core             48HP8-DN98B-MYWDG-T2DCC-8W83P
Windows 7 Professional                          FJ82H-XT6CR-J8D7P-XQJJ2-GPDD4
Windows 7 Professional N                        MRPKT-YTG23-K7D7T-X2JMM-QY7MG
Windows 7 Professional E                          W82YF-2Q76Y-63HXB-FGJG9-GF7QX
Windows 7 Enterprise                            33PXH-7Y6KF-2VJC9-XBBR8-HVTHH
Windows 7 Enterprise N                          YDRBP-3D83W-TY26F-D46B2-XCKRJ
Windows 7 Enterprise E                          C29WB-22CC8-VJ326-GHFJW-H9DH4
Windows Server 2008 R2 Web                        6TPJF-RBVHG-WBW2R-86QPH-6RTM4
Windows Server 2008 R2 HPC edition              TT8MH-CG224-D3D7Q-498W2-9QCTX
Windows Server 2008 R2 Standard                     YC6KT-GKW9T-YTKYR-T4X34-R7VHC
Windows Server 2008 R2 Enterprise               489J6-VHDMP-X63PK-3K798-CPX3Y
Windows Server 2008 R2 Datacenter               74YFP-3QFB3-KQT8W-PMXWJ-7M648
Windows Server 2008 R2 for Itanium              GT63C-RJFQ3-4GMB6-BRFB9-CB83V
Windows Vista Business                          YFKBB-PQJJV-G996G-VWGXY-2V3X8
Windows Vista Business N                          HMBQG-8H2RH-C77VX-27R82-VMQBT
Windows Vista Enterprise                          VKK3X-68KWM-X2YGT-QR4M6-4BWMV
Windows Vista Enterprise N                        VTC42-BM838-43QHV-84HX6-XJXKV
Windows Web Server 2008                        WYR28-R7TFJ-3X2YQ-YCY4H-M249D
Windows Server 2008 Standard                  TM24T-X9RMF-VWXK6-X8JC9-BFGM2
Windows Server 2008 Standard without Hyper-V    W7VD6-7JFBR-RX26B-YKQ3Y-6FFFJ
Windows Server 2008 Enterprise                      YQGMW-MPWTJ-34KDK-48M3W-X4Q6V
Windows Server 2008 Enterprise without Hyper-V  39BXF-X8Q23-P2WWT-38T2F-G3FPG
Windows Server 2008 HPC                         RCTX3-KWVHP-BR6TB-RB6DM-6X7HP
Windows Server 2008 Datacenter                      7M67G-PC374-GR742-YH8V4-TCBY3
Windows Server 2008 Datacenter without Hyper-V  22XQ2-VRXRG-P8D42-K34TD-G3QQC
Windows Server 2008 for Itanium Systems           4DWFP-JF3DJ-B7DTH-78FJB-PDRHK

ProductType 1 = Desktop OS
ProductType 2 = Server OS – Domain Controller
ProductType 3 = Server OS – Not a Domain Controller

Windows 1.0                                               1.04
Windows 2.0                                                   2.11
Windows 3.0                                                 3
Windows NT 3.1                                                3.10.528
Windows for Workgroups 3.11                             3.11
Windows NT Workstation 3.5                                    3.5.807
Windows NT Workstation 3.51                             3.51.1057
Windows 95                                                 4.0.950
Windows NT Workstation 4.0                                4.0.1381
Windows 98                                             4.1.1998
Windows 98 Second Edition                               4.1.2222
Windows Me                                                4.90.3000
Windows 2000 Professional                                     5.0.2195
Windows XP                                             5.1.2600
Windows Vista                                               6.0.6000
Windows 7                                             6.1.7600
Windows 95 OEM Service Release 1 (95A)                    4.00.950 A *)
Windows 95 OEM Service Release 2 (95B)                  4.00.1111 B *)
Windows 95 OEM Service Release 2.1                      4.03.1212-1214 B *)
Windows 95 OEM Service Release 2.5 C                    4.03.1214 C *)
Windows 98                                              4.10.1998
Windows 98 Second Edition (SE)                          4.10.2222 A
Windows Millenium Beta                                  4.90.2476
Windows Millenium                                       4.90.3000
Windows NT 3.1                                            3.10.528 (27.07.1993)
Windows NT 3.5                                            3.50.807 (21.09.1994)
Windows NT 3.51                                         3.51.1057 (30.05.1995)
Windows NT 4.00                                           4.00.1381 (24.08.1996)
Windows NT 5.00 (Beta 2)                                5.00.1515
Windows 2000 (Beta 3)                                   5.00.2031
Windows 2000 (Beta 3 RC2)                              5.00.2128
Windows 2000 (Beta 3)                                  5.00.2183
Windows 2000                                            5.00.2195 (17.02.2000)
Whistler Server Preview                                 2250
Whistler Server alpha                                   2257
Whistler Server interim release                        2267
Whistler Server interim release                        2410
Windows XP (RC 1)                                      5.1.2505
Windows XP                                              5.1.2600 (25.10.2001)
Windows XP, Service Pack 1                              5.1.2600.1105-1106
Windows XP, Service Pack 2                              5.1.2600.2180
Windows XP, Service Pack 3                              5.1.2600 (21.04.2008)
Windows .NET Server interim                             5.2.3541
Windows .NET Server Beta 3                              5.2.3590
Windows .NET Server Release Candidate 1 (RC1)           5.2.3660
Windows .NET Server 2003 RC2                            5.2.3718
Windows Server 2003 (Beta?)                             5.2.3763
Windows Server 2003                                     5.2.3790 (24.04.2003)
Windows Server 2003, Service Pack 1                     5.2.3790.1180
Windows Server 2003                                         5.2.3790.1218
Windows Home Server                                         5.2.3790 (16.06.2007)
Windows Longhorn                                      6.0.5048
Windows Vista, Beta 1                                   6.0.5112 (20.07.2005)
Windows Vista, Community Technology Preview (CTP)       6.0.5219 (30.08.2005)
Windows Vista, TAP Preview                              6.0.5259 (17.11.2005)
Windows Vista, CTP (Dezember)                           6.0.5270 (14.12.2005)
Windows Vista, CTP (Februar)                            6.0.5308 (17.02.2006)
Windows Vista, CTP (Refresh)                            6.0.5342 (21.03.2006)
Windows Vista, April EWD                                  6.0.5365 (19.04.2006)
Windows Vista, Beta 2 Previw                          6.0.5381 (01.05.2006)
Windows Vista, Beta 2                                       6.0.5384 (18.05.2006)
Windows Vista, Pre-RC1                                      6.0.5456 (20.06.2006)
Windows Vista, Pre-RC1, Build 5472                      6.0.5472 (13.07.2006)
Windows Vista, Pre-RC1, Build 5536                      6.0.5536 (21.08.2006)
Windows Vista, RC1                                        6.0.5600.16384 (29.08.2006)
Windows Vista, Pre-RC2                                  6.0.5700 (10.08.2006)
Windows Vista, Pre-RC2, Build 5728                      6.0.5728 (17.09.2006)
Windows Vista, RC2                                        6.0.5744.16384 (03.10.2006)
Windows Vista, Pre-RTM, Build 5808                      6.0.5808 (12.10.2006)
Windows Vista, Pre-RTM, Build 5824                      6.0.5824 (17.10.2006)
Windows Vista, Pre-RTM, Build 5840                      6.0.5840 (18.10.2006)
Windows Vista, RTM (Release to Manufacturing)           6.0.6000.16386 (01.11.2006)
Windows Vista                                             6.0.6000 (08.11.2006)
Windows Vista, Service Pack 2                         6.0.6002 (04.02.2008)
Windows Server 2008                                         6.0.6001 (27.02.2008)
Windows 7, RTM (Release to Manufacturing)               6.1.7600.16385 (22.10.2009)
Windows 7                                                 6.1.7601
Windows Server 2008 R2, RTM (Release to Manufacturing)      6.1.7600.16385 (22.10.2009)
Windows Server 2008 R2, SP1                               6.1.7601
Windows Home Server 2011                                  6.1.8400 (05.04.2011)
Windows Server 2012                                     6.2.9200 (04.09.2012)
Windows 8                                               6.2.9200 (04.09.2012)
Windows Phone 8                                           6.2.10211 (29.10.2012)
Windows Server 2012 R2                                  6.3.9200 (04.09.2012)
Windows 8.1                                             6.3.9200 (04.09.2012)
Windows Phone 8                                           6.3.10211 (29.10.2012)

The OperatingSystemSKU property identifies the SKU of the operating system.

$OperatingSystemSKU_ReturnValue =
@{
    0='Undefined'
    1='Ultimate Edition'
    2='Home Basic Edition'
    3='Home Premium Edition'
    4='Enterprise Edition'
    5='Home Basic N Edition'
    6='Business Edition'
    7='Standard Server Edition'
    8='Datacenter Server Edition'
    9='Small Business Server Edition'
   10='Enterprise Server Edition'
   11='Starter Edition'
   12='Datacenter Server Core Edition'
   13='Standard Server Core Edition'
   14='Enterprise Server Core Edition'
   15='Enterprise Server IA64 Edition'
   16='Business N Edition'
   17='Web Server Edition'
   18='Cluster Server Edition'
   19='Home Server Edition'
   20='Storage Express Server Edition'
   21='Storage Standard Server Edition'
   22='Storage Workgroup Server Edition'
   23='Storage Enterprise Server Edition'
   24='Server For Small Business Edition'
   25='Small Business Server Premium Edition'
   26='(unknown)'
}

The OSProductSuite property identifies installed and licensed system product additions to the operating system.

$SmallBusiness=1
$Enterprise=2
$BackOffice=4
$CommunicationServer=8
$TerminalServer=16
$SmallBusinessRestricted=32
$EmbeddedNT=64
$DataCenter=128

$OSProductSuite = $TerminalServer + $SmallBusinessRestricted

The ProductType property indicates additional information about the system. This member can be one of the following values:

$ProductType_ReturnValue =
@{
    1='Work Station'
    2='Domain Controller'
    3='Server'
}

http://www.codeguru.com/cpp/misc/misc/system/article.php/c8973/Determine-Windows-Version-and-Edition.htm

#>