CyberPolice

An epic windows securing and hardening script
Log | Files | Refs | README

GetReportsForAllGPOs.wsf (7008B)


      1 '////////////////////////////////////////////////////////////////////////////
      2 '// Copyright (c) Microsoft Corporation.  All rights reserved
      3 '//
      4 '// Title:		GetReportsForAllGPOs.wsf
      5 '// Author:		mtreit@microsoft.com
      6 '// Created:		8/29/2002
      7 '//
      8 '// Purpose:		Takes a domain name and gets reports for all GPOs in that
      9 '//			domain
     10 '// Revision:   	Ported from JScript->VBScript by dtsaltas (December 2002)
     11 '//
     12 '////////////////////////////////////////////////////////////////////////////
     13 
     14 '///////////////////////////////////////
     15 '// Initialization
     16 '///////////////////////////////////////
     17 <job>
     18 
     19 ' Include necessary libraries
     20 
     21 <script language="JScript" src="Lib_CommonGPMCFunctions.js"/>
     22 <script language="VBScript">
     23 
     24 ' Create global objects for use by the rest of the script
     25 Dim GPM       : Set GPM = CreateObject("GPMgmt.GPM")
     26 Dim Constants : Set Constants = GPM.GetConstants()
     27 
     28 '///////////////////////////////////////
     29 '// Main script
     30 '///////////////////////////////////////
     31 
     32 ' Handle command line arguments
     33 Dim ArgumentList     : Set ArgumentList = ProcessCommandLineArguments()
     34 Dim szDomainName     : szDomainName     = ArgumentList.Item("Domain")
     35 Dim szReportLocation : szReportLocation = ArgumentList.Item("ReportLocation")
     36 
     37 ' Validate the path given
     38 Dim bValidPath	: bValidPath = ValidatePath(szReportLocation)
     39 
     40 If bValidPath = false Then
     41 	WScript.Echo "The path '" & szReportLocation & "' could not be found."
     42 	WScript.Echo "Verify the path exists."
     43 	WScript.Quit
     44 End If
     45 
     46 ' Initialize the Domain object
     47 Dim GPMDomain : Set GPMDomain = GPM.GetDomain(szDomainName, "", Constants.UseAnyDC)
     48 
     49 ' Generate reports for all GPOs in the specified domain
     50 GetReportsForAllGPOs szReportLocation, GPMDomain
     51 
     52 '///////////////////////////////////////
     53 '// Function Definitions
     54 '///////////////////////////////////////
     55 
     56 ' Generates reports for all GPOs in the specified domain to a given file system location
     57 Function GetReportsForAllGPOs(szReportLocation, GPMDomain)
     58 
     59 	' Create the search criteria object
     60 	Set GPMSearchCriteria = GPM.CreateSearchCriteria()
     61 
     62 	' Get all of the GPOs by passing in the blank
     63 	' SearchCriteria
     64 
     65 	Set GPOList = GPMDomain.SearchGPOs(GPMSearchCriteria)
     66 
     67 	WScript.Echo "== Found " & GPOList.Count & " GPOs in " & GPMDomain.Domain & vbCrLf
     68 
     69 	' Loop through the list and print info for each GPO	
     70 	iSuccessCount = 0
     71 	iFailureCount = 0
     72 	szReportName = ""
     73 
     74 	For Each GPMGPO In GPOList
     75 		' Generate the XML report
     76 		On Error Resume Next
     77                 Err.Clear
     78 		szReportName = GPMGPO.DisplayName & ".xml"
     79 
     80     		' Get rid of any invalid file name characters
     81 	    	szReportName = GetValidFileName(szReportName)
     82 	    	
     83 		WScript.Echo vbCrLf & "Generating XML report for GPO '" & GPMGPO.DisplayName & "'"
     84 		Set GPMResult = GPMGPO.GenerateReportToFile(Constants.ReportXML, szReportLocation & "\\" & CStr(szReportName))
     85 
     86 		If Err.Number <> 0 Then
     87 			WScript.Echo vbCrLf & "The attempt to generate the XML report failed for GPO " & GPMGPO.ID
     88 			WScript.Echo Hex(Err.Number) & " - " & Err.Description
     89 			iFailureCount = iFailureCount + 1
     90 			bFailed = True
     91 		Else
     92 
     93 			' Call the OverallStatus method on the GPMResult. This will throw an exception if there
     94 			' were any errors during the actual operation.
     95 			GPMResult.OverallStatus
     96 		
     97 			If Err.Number <> 0 Then
     98 				' If we were able to get a GPMResult object, print any status message errors
     99 				If Not GPMResult Is Nothing Then
    100 					PrintStatusMessages GPMResult
    101 				End If
    102 
    103 				WScript.Echo vbCrLf & "The attempt to generate the XML report failed for GPO " & GPMGPO.ID
    104 				WScript.Echo Hex(Err.Number) & " - " & Err.Description
    105 				iFailureCount = iFailureCount + 1
    106 				bFailed = True
    107 			End If
    108 		End If
    109 
    110 		If Not bFailed = True Then
    111 			' Print any status message warnings
    112 			PrintStatusMessages GPMResult
    113 
    114 			iSuccessCount = iSuccessCount + 1
    115 
    116 			' Generate the HTML report
    117 			On Error Resume Next
    118 			Err.Clear
    119 			szReportName = GPMGPO.DisplayName & ".html"
    120 	    
    121 			' Get rid of any invalid file name characters
    122 			szReportName = GetValidFileName(szReportName)
    123 			
    124 			WScript.Echo "Generating HTML report for GPO '" & GPMGPO.DisplayName & "'"
    125 			Set GPMResult = GPMGPO.GenerateReportToFile(Constants.ReportHTML, szReportLocation & "\\" & szReportName)
    126 
    127 			If Err.Number <> 0 Then
    128 				WScript.Echo vbCrLf & "The attempt to generate the HTML report failed for GPO " & GPMGPO.ID
    129 				WScript.Echo Hex(Err.Number) & " - " & Err.Description
    130 				iFailureCount = iFailureCount + 1
    131 				bFailed = True
    132 			Else
    133 
    134 				' Call the OverallStatus method on the GPMResult. This will throw an exception if there
    135 				' were any errors during the actual operation.
    136 				GPMResult.OverallStatus
    137 		
    138 				If Err.Number <> 0 Then
    139 					'If we were able to get a GPMResult object, print any status message errors
    140 					If Not GPMResult Is Nothing Then
    141 						PrintStatusMessages GPMResult
    142 					End If
    143 	
    144 					WScript.Echo vbCrLf & "The attempt to generate the HTML report failed for GPO " & GPMGPO.ID
    145 					WScript.Echo Hex(Err.Number) & " - " & Err.Description
    146 					iFailureCount = iFailureCount + 1
    147 					bFailed = True
    148 				End If
    149 			End If	
    150 
    151 			' Print any status message warnings
    152 			If bFailed = False Then
    153 				PrintStatusMessages GPMResult
    154 				iSuccessCount = iSuccessCount + 1
    155 			End If
    156 		End If
    157 	Next
    158 
    159 	WScript.Echo vbCrLf & "Report generation succeeded for " & iSuccessCount & " reports."
    160 	WScript.Echo "Report generation failed for "             & iFailureCount & " reports."
    161 
    162 End Function
    163 
    164 
    165 ' Returns a dictionary object
    166 ' containing the named arguments and values that were passed in
    167 Function ProcessCommandLineArguments()
    168 
    169 	Dim szReportLocation : szReportLocation = ""
    170 	Dim szDomainName     : szDomainName     = ""
    171 
    172         ' Check if this is cscript. If not, print an error and bail out
    173 
    174 	If UCase(Right(WScript.FullName,11)) = "WSCRIPT.EXE" Then
    175 		WScript.Echo "You must use cscript.exe to execute this script."
    176 		WScript.Quit(-1)
    177 	End If
    178 
    179 	If WScript.Arguments.Length = 0 Then
    180 	
    181 		WScript.Arguments.ShowUsage()
    182 		WScript.Quit(-1)
    183 	End If
    184 	
    185 	Dim Result : Set Result = CreateObject("Scripting.Dictionary")
    186 
    187 	szReportLocation = WScript.Arguments(0)
    188 
    189 	If WScript.Arguments.Named.Exists("Domain") Then
    190 	
    191 		szDomainName = WScript.Arguments.Named("Domain")
    192 	End If
    193 
    194 	' Get the current domain if none was specified
    195 	If szDomainName = "" Then
    196 	
    197 		szDomainName = GetDNSDomainForCurrentUser()
    198 	End If
    199 
    200 	
    201 	Result.Add "Domain"         , szDomainName
    202         Result.Add "ReportLocation" , szReportLocation
    203 	
    204 
    205 	Set ProcessCommandLineArguments = Result
    206 
    207 End Function
    208 
    209 </script>
    210 
    211 <!-- Usage and command line argument information -->
    212 <runtime>
    213 
    214 <description>
    215 Generates reports for all GPOs in a given domain to the specified file system location.
    216 </description>
    217 
    218 <unnamed name="ReportLocation" helpstring="File system location to save reports to" type="string" required="true" />
    219 <named   name="Domain"         helpstring="DNS name of domain"                      type="string" required="false"/>
    220 
    221 <example>
    222 Example: GetReportsForAllGPOs.wsf c:\reports
    223 </example>
    224 
    225 </runtime>
    226 
    227 </job>