This Powershell script will audit your Active Directory Domain Controllers for a given domain and return a whole bunch of useful information via CSV or Excel:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 | ########################################################## # Adjustable variables (5) # Manual safe guard to ensure scope is limited # Set the name of the Active Directory domain to query, eg $DomainDNS = "danovich.com.au" # $DomainDNS value is ignored if the $SpecificDC value is set to anything after from null $DomainDNS = "danovich.com.au" # If this value is set, only a specific Domain Controller is queried # For example, to only query TESTDC01, set $SpecificDC = "TESTDC01" # Otherwise set $SpecificDC = $null and all Domain Controllers will be discovered and queried $SpecificDC = $null # Set the timeout in milliseconds for ping command, eg $Timeout = 500 # Some enviornments may need a larger value $Timeout = 5000 # Set the type of output required, either: # "Excel" - Nicely formatted Excel spreadsheet formatting - requires Excel installed on host # "CSV" - Basic text output to CSV file for further manipulation $OutputFormat = "CSV" # If using CSV, specify the name and location of the CSV file to be created, eg $FileLocation = "c:\temp\ExportDCs.csv" $FileLocation = "c:\temp\ExportDCs.csv" ########################################################## # AUTHOR: blog.danovich.com.au # DATE: 16/05/2013 # NAME: Domain_Controllers_Audit.ps1 # VERSION: 1.7 # PURPOSE: Audit Active Directory Domain Controllers # COMMENT: 1.0 Initial release after testing # 1.1 Adjusted $DomainControllers = Get-ADDomainController -Domain $DomainDNS query # 1.2 Added support for Windows 2003 Server queries - previously was only 2008 OS and above # Ability to query only one particular Domain Controller # 1.3 Fixed incorrect CPU core counting # 1.4 Added WMI connectivity check # 1.5 Added checks for disk space, SCCM & SCOM agents # 1.6 Added progress bar # 1.7 Fixed incorrect virtual machine query ########################################################## # REQUIREMENTS: # To run the PowerShell script, the correct Execution Policy level must be set (Set-ExecutionPolicy) # The user account running this script must have permission to: # - Remotely query WMI on Domain Controllers # - Query Active Directory attributes of Domain Contollers # Network connectivity from the host where script is running to all Domain Controllers in the domain including: # - ICMP (for ping) # - TCP & UDP ports for remote WMI queries # - Consider both hardware and software firewalls # Micorosft Office Excel must be installed on the host running this script if $OutputFormat = "Excel" # Run from within the Active Directory Module for PowerShell (or import the Active Directory PS module into the PS session) ########################################################## # Check if $SpecificDC value is not null if ($SpecificDC) { $DomainControllers = Get-ADDomainController $SpecificDC } else { # Domain Controller discovery clear write-host "" `r Write-host "Discovering all DCs in the domain $DomainDNS ....." `r Write-host "`n" $DomainControllers = Get-ADDomainController -Filter * -Server $DomainDNS } # Output list of Domain Controllers to screen ForEach ($DC in $DomainControllers) { Write-Host $DC.Name } ########################################################## # Start of Excel section ########################################################## if ($OutputFormat -eq "Excel") { # Set up Excel spreadsheet $erroractionpreference = "SilentlyContinue" $a = New-Object -comobject Excel.Application $a.visible = $True $b = $a.Workbooks.Add() $c = $b.Worksheets.Item(1) $c.Cells.Item(1,1) = "Name" $c.Cells.Item(1,2) = "Description" $c.Cells.Item(1,3) = "Ping Status" $c.Cells.Item(1,4) = "FQDN" $c.Cells.Item(1,5) = "IP Address" $c.Cells.Item(1,6) = "Operating System" $c.Cells.Item(1,7) = "Service Pack" $c.Cells.Item(1,8) = "Domain" $c.Cells.Item(1,9) = "GC" $c.Cells.Item(1,10) = "FSMO Roles" $c.Cells.Item(1,11) = "AD Site" $c.Cells.Item(1,12) = "Read Only" $c.Cells.Item(1,13) = "LDAP Port" $c.Cells.Item(1,14) = "SSL Port" $c.Cells.Item(1,15) = "Roles Installed" $c.Cells.Item(1,16) = "Last Boot Time" $c.Cells.Item(1,17) = "Virtual" $c.Cells.Item(1,18) = "DNS Servers" $c.Cells.Item(1,19) = "RAM (MB)" $c.Cells.Item(1,20) = "CPU Speed (MHz)" $c.Cells.Item(1,21) = "CPU Cores" $c.Cells.Item(1,22) = "Logical CPUs" $c.Cells.Item(1,23) = "Timezone" $c.Cells.Item(1,24) = "Free space (C: GB)" $c.Cells.Item(1,25) = "SCCM Client" $c.Cells.Item(1,26) = "SCOM Client" $c.Cells.Item(1,27) = "Query Time" $d = $c.UsedRange $d.Interior.ColorIndex = 19 $d.Font.ColorIndex = 11 $d.Font.Bold = $True $d.EntireColumn.AutoFit($True) $intRow = 2 # Start querying each Domain Controller ForEach ($DC in $DomainControllers) { # Output progress bar to the screen $i++ $numberofDCs = $DomainControllers.count $ProgressName = $DC.Name Write-Progress -Activity "Collecting Domain Controller information" -status "Contacting $ProgressName [$i out of $numberofDCs]. Overall percentage complete:" -percentComplete ($i / $DomainControllers.count*100) # Write Domain Controller name in capitals $c.Cells.Item($intRow, 1) = $DC.Name.ToUpper() # Test connectivity from host to each Domain Controller $ping = new-object System.Net.NetworkInformation.Ping $Reply = $ping.send($DC.Name,$Timeout) if ($Reply.status -eq "Success") { $DCPing = "Resolved & active" $Online = $true } elseif ($Reply.status -eq "TimedOut") { $DCPing = "Resolved host but timed out" $Online = $false } else { $DCPing = "Unable to resolve" $Online = $false } $Reply = "" # Write ping status $c.Cells.Item($intRow, 3) = $DCPing # Check WMI connectivity $wmi = $null $wmi = Get-WmiObject -class Win32_ComputerSystem -ComputerName $DC.Name -ErrorAction SilentlyContinue if ($wmi) { # Able to connect to WMI $ConnectViaWmi = $True } else { # Unable to connect to WMI $ConnectViaWmi = $False } # Query and write computer description retrieved from AD $DCDesc = (Get-ADComputer -Properties * -Filter {name -like $DC.Name}).Description $c.Cells.Item($intRow, 2) = $DCDesc # Query and write computer FQDN retrieved from AD $DCFQDN = (Get-ADComputer -Properties * -Filter {name -like $DC.Name}).DNSHostName $c.Cells.Item($intRow, 4) = $DCFQDN # Query and write computer IPv4 address retrieved from AD $DCIPv4 = (Get-ADComputer -Properties * -Filter {name -like $DC.Name}).IPv4Address $c.Cells.Item($intRow, 5) = $DCIPv4 # Query and write computer Operating System retrieved from AD $DCOS = (Get-ADComputer -Properties * -Filter {name -like $DC.Name}).OperatingSystem $c.Cells.Item($intRow, 6) = $DCOS # Query and write computer Operating System Service Pack retrieved from AD $DCOSSP = (Get-ADComputer -Properties * -Filter {name -like $DC.Name}).OperatingSystemServicePack $c.Cells.Item($intRow, 7) = $DCOSSP # Query and write computer domain retrieved from AD $DCDomain = (Get-ADDomainController -Filter {name -like $DC.Name}).Domain $c.Cells.Item($intRow, 8) = $DCDomain # Query and write computer global catalog boolean retrieved from AD $DCGC = (Get-ADDomainController -Filter {name -like $DC.Name}).IsGlobalCatalog $c.Cells.Item($intRow, 9) = $DCGC # Query and write computer FSMO roles retrieved from AD $DCFSMOOutput = ((Get-ADDomainController -Filter {name -like $DC.Name}).OperationMasterRoles | Out-String) $DCFSMO = ($DCFSMOOutput).Replace("`n",' ') $c.Cells.Item($intRow, 10) = $DCFSMO # Query and write computer AD site info retrieved from AD $DCSite = (Get-ADDomainController -Filter {name -like $DC.Name}).Site $c.Cells.Item($intRow, 11) = $DCSite # Query and write computer read-only domain controller info retrieved from AD $DCRO = (Get-ADDomainController -Filter {name -like $DC.Name}).IsReadOnly $c.Cells.Item($intRow, 12) = $DCRO # Query and write computer LDAP port retrieved from AD $DCLDAP = (Get-ADDomainController -Filter {name -like $DC.Name}).LdapPort $c.Cells.Item($intRow, 13) = $DCLDAP # Query and write computer SSL port retrieved from AD $DCSLDAP = (Get-ADDomainController -Filter {name -like $DC.Name}).SSLPort $c.Cells.Item($intRow, 14) = $DCSLDAP # Query the Server Roles that are installed on the Domain Controller (eg DNS, DHCP, ADDS) # Assuming role ID is less than 30 - <a href="http://msdn.microsoft.com/en-gb/library/windows/desktop/cc280268(v=vs.85).aspx">http://msdn.microsoft.com/en-gb/library/windows/desktop/cc280268(v=vs.85).aspx</a> if ($Online -eq $true -and $DCOS -notlike "*2003*" -and $ConnectViaWmi -eq $true) { $DCRole = (gwmi win32_ServerFeature -filter "ID<30" -computername $DC.Name | Select-Object "Name") $k = @() foreach ($j in $DCRole) {$k += $j.Name $c.Cells.Item($intRow, 15) = ($k -Join ', ')} } elseif ($Online -eq $false) { $c.Cells.Item($intRow, 15) = "Cannot query - Ping timeout" } elseif ($ConnectViaWmi -eq $false -and $DCOS -notlike "*2003*") { $c.Cells.Item($intRow, 15) = "Cannot connect to WMI" } elseif ($DCOS -like "*2003*") { $c.Cells.Item($intRow, 15) = "N/A - Windows 2003 Server OS" } # Query last boot time if ($Online -eq $true -and $ConnectViaWmi -eq $true) { $date = new-object -com WbemScripting.SWbemDateTime $z = get-wmiobject Win32_OperatingSystem -computername $DC.Name foreach ($k in $z) {$date.value = $k.lastBootupTime If ($k.Version -eq "*" ) {$c.Cells.Item($intRow, 16) = $Date.GetVarDate($True)} Else {$c.Cells.Item($intRow, 16) = $Date.GetVarDate($False)} } } elseif ($Online -eq $false) { $c.Cells.Item($intRow, 16) = "Cannot query - Ping timeout" } elseif ($ConnectViaWmi -eq $false) { $c.Cells.Item($intRow, 16) = "Cannot connect to WMI" } # Query if virtual machine / virtual hardware $DCVM = $null if ($Online -eq $true -and $ConnectViaWmi -eq $true) { $bios = gwmi Win32_BIOS -computername $DC.Name | Select-Object "version","serialnumber" $compsys = gwmi Win32_ComputerSystem -computername $DC.Name | Select-Object "model","manufacturer" if($bios.Version -match "VRTUAL") {$DCVM = "Virtual - Hyper-V"} elseif($bios.Version -match "A M I") {$DCVM = "Virtual - Virtual PC"} elseif($bios.Version -like "*Xen*") {$DCVM = "Virtual - Xen"} elseif($bios.SerialNumber -like "*VMware*") {$DCVM = "Virtual - VMWare"} elseif($compsys.manufacturer -like "*Microsoft*") {$DCVM = "Virtual - Hyper-V"} elseif($compsys.manufacturer -like "*VMWare*") {$DCVM = "Virtual - VMWare"} elseif($compsys.model -like "*Virtual*") {$DCVM = "Virtual"} else {$DCVM = "Physical"} } elseif ($Online -eq $false) { $DCVM = "Cannot query - Ping timeout" } elseif ($ConnectViaWmi -eq $false) { $DCVM = "Cannot connect to WMI" } # Query machine network cards to see which DNS Servers are used for queries if ($Online -eq $true -and $ConnectViaWmi -eq $true) { $Adapters = Get-Wmiobject Win32_NetworkAdapterConfiguration -Computername $DC.Name | Where-Object{$_.IPEnabled -eq $True} ForEach($Adapter In $Adapters) { [String]$DNSServers = "" $Adapters2 = Get-Wmiobject Win32_NetworkAdapter -Computername $DC.Name | Where-Object{$_.Caption -eq $Adapter.Caption} [String]$NetID = $Adapters2.NetConnectionID If($Adapter.DNSServerSearchOrder -ne $Null) {ForEach($Address In $Adapter.DNSServerSearchOrder) {$DNSServers += $Address + " "} } } } elseif ($Online -eq $false) { $DNSServers = "Cannot query - Ping timeout" } elseif ($ConnectViaWmi -eq $false) { $DNSServers = "Cannot connect to WMI" } # Output time of query $datetime = get-date -uformat "%d/%m/%Y %H:%M:%S" $UTC = get-date -uformat "%Z" $querytime = $datetime + " UTC " + $UTC # Check amount of installed RAM if ($Online -eq $true -and $ConnectViaWmi -eq $true) { $colItems = get-wmiobject -class "Win32_ComputerSystem" -namespace "root\CIMV2" -computername $DC.Name foreach ($objItem in $colItems) {$DCRAM = [math]::round($objItem.TotalPhysicalMemory/1024/1024, 0)} } elseif ($Online -eq $false) { $DCRAM = "Cannot query - Ping timeout" } elseif ($ConnectViaWmi -eq $false) { $DCRAM = "Cannot connect to WMI" } # Query CPU information if ($Online -eq $true -and $DCOS -notlike "*2003*" -and $ConnectViaWmi -eq $true) { $CPUproperty = "maxclockspeed", "numberOfCores", "NumberOfLogicalProcessors" $DCCPUSpeed = Get-WmiObject -class "win32_processor" -Property $CPUproperty -computername $DC.Name -filter "deviceid='CPU0'" | Select-Object -expand "maxclockspeed" $Win32_cpu = Get-WmiObject -class win32_processor -computername $DC.Name $DCCPULogical = ($Win32_cpu | measure-object).count $DCCPUCores = ($Win32_cpu | measure-object NumberOfCores -sum).sum } elseif ($Online -eq $true -and $DCOS -like "*2003*" -and $ConnectViaWmi -eq $true) { $CPUproperty = "maxclockspeed" $DCCPUSpeed = Get-WmiObject -class "win32_processor" -Property $CPUproperty -computername $DC.Name -filter "deviceid='CPU0'" | Select-Object -expand "maxclockspeed" $physCount = new-object hashtable $Win32_cpu = Get-WmiObject -class win32_processor -computername $DC.Name $Win32_cpu |%{$physCount[$_.SocketDesignation] = 1} $DCCPULogical = $physCount.count $DCCPUCores = ($Win32_cpu | measure-object).count } elseif ($Online -eq $false) { $DCCPUSpeed = "Cannot query - Ping timeout" $DCCPUCores = "Cannot query - Ping timeout" $DCCPULogical = "Cannot query - Ping timeout" } elseif ($ConnectViaWmi -eq $false) { $DCCPUSpeed = "Cannot connect to WMI" $DCCPUCores = "Cannot connect to WMI" $DCCPULogical = "Cannot connect to WMI" } # Query timezone information if ($Online -eq $true -and $ConnectViaWmi -eq $true) { $DCTZ = Get-WmiObject -class "Win32_TimeZone" -computername $DC.Name | Select-Object -expand "Caption" } elseif ($Online -eq $false) { $DCTZ = "Cannot query - Ping timeout" } elseif ($ConnectViaWmi -eq $false) { $DCTZ = "Cannot connect to WMI" } # Query free disk space on C drive if ($Online -eq $true -and $ConnectViaWmi -eq $true) { $Cdisk = Get-WmiObject Win32_LogicalDisk -ComputerName $DC.Name -Filter "DeviceID='C:'" | Select-Object FreeSpace $Cdisk.FreeSpace = $([Math]::Round($Cdisk.FreeSpace/1073741824,1)) $DCDiskFree = $Cdisk.FreeSpace } elseif ($Online -eq $false) { $DCDiskFree = "Cannot query - Ping timeout" } elseif ($ConnectViaWmi -eq $false) { $DCDiskFree = "Cannot connect to WMI" } # Check if SCCM client is installed if ($Online -eq $true -and $ConnectViaWmi -eq $true) { $SCCMCheck = get-wmiobject -namespace root\cimv2 -computername $DC.Name -class win32_process -filter 'Name="ccmexec.exe"' if (-not $SCCMCheck) { $DCSCCMClient = "No" } else { $siteCode = (Get-WmiObject -computername $DC.Name -namespace root\ccm\policy\machine -Class CCM_SystemHealthClientConfig).SiteCode $DCSCCMClient = "Yes - " +$siteCode } } elseif ($Online -eq $false) { $DCSCCMClient = "Cannot query - Ping timeout" } elseif ($ConnectViaWmi -eq $false) { $DCSCCMClient = "Cannot connect to WMI" } # Check if SCOM client is installed if ($Online -eq $true -and $ConnectViaWmi -eq $true) { $SCOMCheck = get-wmiobject -namespace root\cimv2 -computername $DC.Name -class win32_process -filter 'Name="HealthService.exe"' if (-not $SCOMCheck) { $DCSCOMClient = "No" } else { $Path = "hklm:\SOFTWARE\MICROSOFT\MICROSOFT OPERATIONS MANAGER\3.0\AGENT MANAGEMENT GROUPS" $baseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine", $DC.Name) $s = "" ## Open the key $key = $baseKey.OpenSubKey("SOFTWARE\MICROSOFT\MICROSOFT OPERATIONS MANAGER\3.0\AGENT MANAGEMENT GROUPS") ## Retrieve all of its children foreach($subkeyName in $key.GetSubKeyNames()) { ## Open the subkey $subkey = $key.OpenSubKey($subkeyName) $returnObject = [PsObject] $subKey $returnObject | Add-Member NoteProperty PsChildName $subkeyName | Select PSChildName ## Output the key $s += $returnObject.PsChildName + " " ## Close the child key $subkey.Close() } ## Close the key and base keys $key.Close() $baseKey.Close() $DCSCOMClient = "Yes - " +$s } } elseif ($Online -eq $false) { $DCSCOMClient = "Cannot query - Ping timeout" } elseif ($ConnectViaWmi -eq $false) { $DCSCOMClient = "Cannot connect to WMI" } # Write out Excel rows for direct machine queries $c.Cells.Item($intRow, 17) = $DCVM $c.Cells.Item($intRow, 18) = $DNSServers $c.Cells.Item($intRow, 19) = $DCRAM $c.Cells.Item($intRow, 20) = $DCCPUSpeed $c.Cells.Item($intRow, 21) = $DCCPUCores $c.Cells.Item($intRow, 22) = $DCCPULogical $c.Cells.Item($intRow, 23) = $DCTZ $c.Cells.Item($intRow, 24) = $DCDiskFree $c.Cells.Item($intRow, 25) = $DCSCCMClient $c.Cells.Item($intRow, 26) = $DCSCOMClient $c.Cells.Item($intRow, 27) = $querytime # Next Excel row $intRow = $intRow + 1 [array] $DomainDCs += $DC.HostName $Online = $false # End of foreach DC } # Configure Excel Autofit for rows and columns $d.EntireColumn.AutoFit()|out-null $d.EntireRow.AutoFit()|out-null # Configure Excel Filters - uncomment if required #$d.EntireColumn.AutoFilter() Write-host "`n" Write-Host "Excel file creation complete...." } ########################################################## # End of Excel section ########################################################## ########################################################## # Start of CSV section ########################################################## if ($OutputFormat -eq "CSV") { # Create empty array $report = @() $erroractionpreference = "SilentlyContinue" # Start querying each Domain Controller ForEach ($DC in $DomainControllers) { # Output progress bar to the screen $i++ $numberofDCs = $DomainControllers.count $ProgressName = $DC.Name Write-Progress -Activity "Collecting Domain Controller information" -status "Contacting $ProgressName [$i out of $numberofDCs]. Overall percentage complete:" -percentComplete ($i / $DomainControllers.count*100) # Test connectivity from host to each Domain Controller $ping = new-object System.Net.NetworkInformation.Ping $Reply = $ping.send($DC.Name,$Timeout) if ($Reply.status -eq "Success") { $DCPing = "Resolved & active" $Online = $true } elseif ($Reply.status -eq "TimedOut") { $DCPing = "Resolved host but timed out" $Online = $false } else { $DCPing = "Unable to resolve" $Online = $false } $Reply = "" # Check WMI connectivity $wmi = $null $wmi = Get-WmiObject -class Win32_ComputerSystem -ComputerName $DC.Name -ErrorAction SilentlyContinue if ($wmi) { # Able to connect to WMI $ConnectViaWmi = $True } else { # Unable to connect to WMI $ConnectViaWmi = $False } # Query computer description retrieved from AD $DCDesc = (Get-ADComputer -Properties * -Filter {name -like $DC.Name}).Description # Query computer FQDN retrieved from AD $DCFQDN = (Get-ADComputer -Properties * -Filter {name -like $DC.Name}).DNSHostName # Query IPv4 address retrieved from AD $DCIPv4 = (Get-ADComputer -Properties * -Filter {name -like $DC.Name}).IPv4Address # Query Operating System retrieved from AD $DCOS = (Get-ADComputer -Properties * -Filter {name -like $DC.Name}).OperatingSystem # Query Operating System Service Pack retrieved from AD $DCOSSP = (Get-ADComputer -Properties * -Filter {name -like $DC.Name}).OperatingSystemServicePack # Query computer domain retrieved from AD $DCDomain = (Get-ADDomainController -Filter {name -like $DC.Name}).Domain # Query computer global catalog boolean retrieved from AD $DCGC = (Get-ADDomainController -Filter {name -like $DC.Name}).IsGlobalCatalog # Query computer FSMO roles retrieved from AD $DCFSMOOutput = ((Get-ADDomainController -Filter {name -like $DC.Name}).OperationMasterRoles | Out-String) $DCFSMO = ($DCFSMOOutput).Replace("`n",' ') # Query computer AD site info retrieved from AD $DCSite = (Get-ADDomainController -Filter {name -like $DC.Name}).Site # Query read-only domain controller info retrieved from AD $DCRO = (Get-ADDomainController -Filter {name -like $DC.Name}).IsReadOnly # Query computer LDAP port retrieved from AD $DCLDAP = (Get-ADDomainController -Filter {name -like $DC.Name}).LdapPort # Query computer SSL port retrieved from AD $DCSLDAP = (Get-ADDomainController -Filter {name -like $DC.Name}).SSLPort # Query the Server Roles that are installed on the Domain Controller (eg DNS, DHCP, ADDS) # Assuming role ID is less than 30 - <a href="http://msdn.microsoft.com/en-gb/library/windows/desktop/cc280268(v=vs.85).aspx">http://msdn.microsoft.com/en-gb/library/windows/desktop/cc280268(v=vs.85).aspx</a> if ($Online -eq $true -and $DCOS -notlike "*2003*" -and $ConnectViaWmi -eq $true) { $DCRole = (gwmi win32_ServerFeature -filter "ID<30" -computername $DC.Name | Select-Object "Name") $k = @() foreach ($j in $DCRole) {$k += $j.Name $DCRoleOutput = ($k -Join ', ') } } elseif ($Online -eq $false) { $DCRoleOutput = "Cannot query - Ping timeout" } elseif ($DCOS -like "*2003*") { $DCRoleOutput = "N/A - Windows 2003 Server OS" } elseif ($ConnectViaWmi -eq $false) { $DCRoleOutput = "Cannot connect to WMI" } # Query last boot time if ($Online -eq $true -and $ConnectViaWmi -eq $true) { $date = new-object -com WbemScripting.SWbemDateTime $z = get-wmiobject Win32_OperatingSystem -computername $DC.Name foreach ($k in $z) { $date.value = $k.lastBootupTime If ($k.Version -eq "*" ) { $LastBoot = $Date.GetVarDate($True) } Else { $LastBoot = $Date.GetVarDate($False) } } } elseif ($Online -eq $false) { $LastBoot = "Cannot query - Ping timeout" } elseif ($ConnectViaWmi -eq $false) { $LastBoot = "Cannot connect to WMI" } # Query if virtual machine / virtual hardware $DCVM = $null if ($Online -eq $true -and $ConnectViaWmi -eq $true) { $bios = gwmi Win32_BIOS -computername $DC.Name | Select-Object "version","serialnumber" $compsys = gwmi Win32_ComputerSystem -computername $DC.Name | Select-Object "model","manufacturer" if($bios.Version -match "VRTUAL") {$DCVM = "Virtual - Hyper-V"} elseif($bios.Version -match "A M I") {$DCVM = "Virtual - Virtual PC"} elseif($bios.Version -like "*Xen*") {$DCVM = "Virtual - Xen"} elseif($bios.SerialNumber -like "*VMware*") {$DCVM = "Virtual - VMWare"} elseif($compsys.manufacturer -like "*Microsoft*") {$DCVM = "Virtual - Hyper-V"} elseif($compsys.manufacturer -like "*VMWare*") {$DCVM = "Virtual - VMWare"} elseif($compsys.model -like "*Virtual*") {$DCVM = "Virtual"} else {$DCVM = "Physical"} } elseif ($Online -eq $false) { $DCVM = "Cannot query - Ping timeout" } elseif ($ConnectViaWmi -eq $false) { $DCVM = "Cannot connect to WMI" } # Query machine network cards to see which DNS Servers are used for queries if ($Online -eq $true -and $ConnectViaWmi -eq $true) { $Adapters = Get-Wmiobject Win32_NetworkAdapterConfiguration -Computername $DC.Name | Where-Object{$_.IPEnabled -eq $True} ForEach($Adapter In $Adapters) { [String]$DNSServers = "" $Adapters2 = Get-Wmiobject Win32_NetworkAdapter -Computername $DC.Name | Where-Object{$_.Caption -eq $Adapter.Caption} [String]$NetID = $Adapters2.NetConnectionID If($Adapter.DNSServerSearchOrder -ne $Null) {ForEach($Address In $Adapter.DNSServerSearchOrder) {$DNSServers += $Address + " "} } } } elseif ($Online -eq $false) { $DNSServers = "Cannot query - Ping timeout" } elseif ($ConnectViaWmi -eq $false) { $DNSServers = "Cannot connect to WMI" } # Output time of query $datetime = get-date -uformat "%d/%m/%Y %H:%M:%S" $UTC = get-date -uformat "%Z" $querytime = $datetime + " UTC " + $UTC # Check amount of installed RAM if ($Online -eq $true -and $ConnectViaWmi -eq $true) { $colItems = get-wmiobject -class "Win32_ComputerSystem" -namespace "root\CIMV2" -computername $DC.Name foreach ($objItem in $colItems) { $DCRAM = [math]::round($objItem.TotalPhysicalMemory/1024/1024, 0) } } elseif ($Online -eq $false) { $DCRAM = "Cannot query - Ping timeout" } elseif ($ConnectViaWmi -eq $false) { $DCRAM = "Cannot connect to WMI" } # Query CPU information if ($Online -eq $true -and $DCOS -notlike "*2003*" -and $ConnectViaWmi -eq $true) { $CPUproperty = "maxclockspeed", "numberOfCores", "NumberOfLogicalProcessors" $DCCPUSpeed = Get-WmiObject -class "win32_processor" -Property $CPUproperty -computername $DC.Name -filter "deviceid='CPU0'" | Select-Object -expand "maxclockspeed" $Win32_cpu = Get-WmiObject -class win32_processor -computername $DC.Name $DCCPULogical = ($Win32_cpu | measure-object).count $DCCPUCores = ($Win32_cpu | measure-object NumberOfCores -sum).sum } elseif ($Online -eq $true -and $DCOS -like "*2003*" -and $ConnectViaWmi -eq $true) { $CPUproperty = "maxclockspeed" $DCCPUSpeed = Get-WmiObject -class "win32_processor" -Property $CPUproperty -computername $DC.Name -filter "deviceid='CPU0'" | Select-Object -expand "maxclockspeed" $physCount = new-object hashtable $Win32_cpu = Get-WmiObject -class win32_processor -computername $DC.Name $Win32_cpu |%{$physCount[$_.SocketDesignation] = 1} $DCCPULogical = $physCount.count $DCCPUCores = ($Win32_cpu | measure-object).count } elseif ($Online -eq $false) { $DCCPUSpeed = "Cannot query - Ping timeout" $DCCPUCores = "Cannot query - Ping timeout" $DCCPULogical = "Cannot query - Ping timeout" } elseif ($ConnectViaWmi -eq $false) { $DCCPUSpeed = "Cannot connect to WMI" $DCCPUCores = "Cannot connect to WMI" $DCCPULogical = "Cannot connect to WMI" } # Query timezone information if ($Online -eq $true -and $ConnectViaWmi -eq $true) { $DCTZ = Get-WmiObject -class "Win32_TimeZone" -computername $DC.Name | Select-Object -expand "Caption" } elseif ($Online -eq $false) { $DCTZ = "Cannot query - Ping timeout" } elseif ($ConnectViaWmi -eq $false) { $DCTZ = "Cannot connect to WMI" } # Query free disk space on C drive if ($Online -eq $true -and $ConnectViaWmi -eq $true) { $Cdisk = Get-WmiObject Win32_LogicalDisk -ComputerName $DC.Name -Filter "DeviceID='C:'" | Select-Object FreeSpace $Cdisk.FreeSpace = $([Math]::Round($Cdisk.FreeSpace/1073741824,1)) $DCDiskFree = $Cdisk.FreeSpace } elseif ($Online -eq $false) { $DCDiskFree = "Cannot query - Ping timeout" } elseif ($ConnectViaWmi -eq $false) { $DCDiskFree = "Cannot connect to WMI" } # Check if SCCM client is installed if ($Online -eq $true -and $ConnectViaWmi -eq $true) { $SCCMCheck = get-wmiobject -namespace root\cimv2 -computername $DC.Name -class win32_process -filter 'Name="ccmexec.exe"' if (-not $SCCMCheck) { $DCSCCMClient = "No" } else { $siteCode = (Get-WmiObject -computername $DC.Name -namespace root\ccm\policy\machine -Class CCM_SystemHealthClientConfig).SiteCode $DCSCCMClient = "Yes - " +$siteCode } } elseif ($Online -eq $false) { $DCSCCMClient = "Cannot query - Ping timeout" } elseif ($ConnectViaWmi -eq $false) { $DCSCCMClient = "Cannot connect to WMI" } # Check if SCOM client is installed if ($Online -eq $true -and $ConnectViaWmi -eq $true) { $SCOMCheck = get-wmiobject -namespace root\cimv2 -computername $DC.Name -class win32_process -filter 'Name="HealthService.exe"' if (-not $SCOMCheck) { $DCSCOMClient = "No" } else { $Path = "hklm:\SOFTWARE\MICROSOFT\MICROSOFT OPERATIONS MANAGER\3.0\AGENT MANAGEMENT GROUPS" $baseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine", $DC.Name) $s = "" ## Open the key $key = $baseKey.OpenSubKey("SOFTWARE\MICROSOFT\MICROSOFT OPERATIONS MANAGER\3.0\AGENT MANAGEMENT GROUPS") ## Retrieve all of its children foreach($subkeyName in $key.GetSubKeyNames()) { ## Open the subkey $subkey = $key.OpenSubKey($subkeyName) $returnObject = [PsObject] $subKey $returnObject | Add-Member NoteProperty PsChildName $subkeyName | Select PSChildName ## Output the key $s += $returnObject.PsChildName + " " ## Close the child key $subkey.Close() } ## Close the key and base keys $key.Close() $baseKey.Close() $DCSCOMClient = "Yes - " +$s } } elseif ($Online -eq $false) { $DCSCOMClient = "Cannot query - Ping timeout" } elseif ($ConnectViaWmi -eq $false) { $DCSCOMClient = "Cannot connect to WMI" } [array] $DomainDCs += $DC.HostName $Online = $false # Create object to collect elements $OutputObj = New-Object -TypeName PSObject -Property @{ "Name" = $DC.Name.ToUpper() "Description" = $DCDesc "Ping Status" = $DCPing "FQDN" = $DCFQDN "IP Address" = $DCIPv4 "Operating System" = $DCOS "Service Pack" = $DCOSSP "Domain" = $DCDomain "GC" = $DCGC "FSMO Roles" = $DCFSMO "AD Site" = $DCSite "Read Only" = $DCRO "LDAP Port" = $DCLDAP "SSL Port" = $DCSLDAP "Roles Installed" = $DCRoleOutput "Last Boot Time" = $LastBoot "Virtual" = $DCVM "DNS Servers" = $DNSServers "RAM (MB)" = $DCRAM "CPU Speed (MHz)" = $DCCPUSpeed "CPU Cores" = $DCCPUCores "Logical CPUs" = $DCCPULogical "Timezone" = $DCTZ "Free space (C: GB)" = $DCDiskFree "SCCM Client" = $DCSCCMClient "SCOM Client" = $DCSCOMClient "Query Time" = $querytime } | Select-Object "Name","Description","Ping Status","FQDN","IP Address","Operating System","Service Pack","Domain","GC","FSMO Roles","AD Site","Read Only","LDAP Port","SSL Port","Roles Installed","Last Boot Time","Virtual","DNS Servers","RAM (MB)","CPU Speed (MHz)","CPU Cores","Logical CPUs","Timezone","Free space (C: GB)","SCCM Client","SCOM Client","Query Time" # Add item to report $report += $OutputObj $Progress # End of for each Domain Controller } # Export information to specified CSV file $report | Export-Csv $FileLocation -Force -NoTypeInformation Write-host "`n" Write-Host "CSV file creation complete...." # End of if CSV section } ########################################################## # End of CSV section ########################################################## # Output domain controller count to screen if ($SpecificDC -ne $null) { Write-host "`n" $DCCount = $DomainDCs.Count $DomainDCs = $DomainDCs | sort Write-Host "Found $DCCount DCs in $DomainDNS. Displaying all DCs in the domain :" `r Write-host "`n" $DomainDCs Write-host "`n" } |

