BackupAllGPOs.wsf (6481B)
1 '//////////////////////////////////////////////////////////////////////////// 2 '// Copyright (c) Microsoft Corporation. All rights reserved 3 '// 4 '// Title: BackupAllGPOs.wsf 5 '// Author: mtreit@microsoft.com 6 '// Created: 1/3/2002 7 '// 8 '// Purpose: Takes a domain name and backs up all GPOs in that domain 9 '// to the specified backup location 10 '// Revision: Ported from JScript->VBScript by dtsaltas (October 20, 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 '/////////////////////////////////////// 30 '// Main script 31 '/////////////////////////////////////// 32 33 ' Handle command line arguments 34 35 Dim ArgumentList : Set ArgumentList = ProcessCommandLineArguments() 36 Dim szDomainName : szDomainName = ArgumentList.Item("Domain") 37 Dim szBackupLocation : szBackupLocation = ArgumentList.Item("BackupLocation") 38 Dim szComment : szComment = ArgumentList.Item("Comment") 39 40 ' Validate the path given 41 Dim bValidPath : bValidPath = ValidatePath(szBackupLocation) 42 43 If bValidPath = false Then 44 WScript.Echo "The path '" & szBackupLocation & "' could not be found." 45 WScript.Echo "Verify the path exists." 46 WScript.Quit 47 End If 48 49 ' Initialize the Domain object 50 Dim GPMDomain : Set GPMDomain = GPM.GetDomain(szDomainName, "", Constants.UseAnyDC) 51 52 ' Backup all GPOs in the specified domain 53 54 BackupAllGPOs szBackupLocation, szComment, GPMDomain 55 56 '/////////////////////////////////////// 57 '// Function Definitions 58 '/////////////////////////////////////// 59 60 ' Backs up all GPOs in the specified domain to a given file system location 61 Function BackupAllGPOs(strBackupLocation, strComment, GPMDomain) 62 63 ' Create the search criteria object 64 Dim GPMSearchCriteria : Set GPMSearchCriteria = GPM.CreateSearchCriteria() 65 66 ' Get all of the GPOs by passing in a default SearchCriteria object 67 68 Dim GPOList 69 Set GPOList = GPMDomain.SearchGPOs(GPMSearchCriteria) 70 71 ' Now process the list 72 Dim GPMGPO 73 74 WScript.Echo "== Found " & GPOList.Count & " GPOs in " & GPMDomain.Domain & " to backup ==" & vbCrLf 75 76 ' Loop through the list and print info for each GPO 77 Dim iSuccessCount : iSuccessCount = 0 78 Dim iFailureCount : iFailureCount = 0 79 Dim bFailure : bFailure = false 80 81 For Each GPMGPO in GPOList 82 On Error Resume Next 83 Dim GPMResult 84 Set GPMResult = GPMGPO.Backup(strBackupLocation,strComment) 85 ' call the overallstatus method to force an error if there were any problems 86 GPMResult.OverallStatus 87 88 89 ' FAILURE 90 ' There was a problem, let the user know the details and increment the failure counter 91 92 If Err.Number <> 0 Then 93 94 If Not IsNull(GPMResult) Then 95 PrintStatusMessages(GPMResult) 96 End If 97 WScript.Echo vbCrLf 98 WScript.Echo "The backup attempt failed for GPO " & GPMGPO.ID 99 WScript.Echo "Attempted to backup GPO " & GPMGPO.ID & " to location " & strBackupLocation 100 WScript.Echo Err.Number & " - " & Err.Description '[todo]wrap with ErrCode 101 iFailureCount = iFailureCount + 1 102 bFailure = true 103 104 End If 105 106 107 ' SUCCESS 108 ' Backup appears to have been successful. Print any status messages in GPMResult and 109 ' provide the user with some details about the backup 110 111 112 If Not bFailure = True Then 113 114 ' Print any status message warnings 115 PrintStatusMessages(GPMResult) 116 117 Dim GPMBackup : Set GPMBackup = GPMResult.Result 118 119 WScript.Echo vbCrLf & "Backed up GPO '" & GPMBackup.GPODisplayName & "' with the following properties:" 120 WScript.Echo vbCrLf 121 WScript.Echo "GPO ID:" & vbTab & vbTab & GPMBackup.GPOID 122 WScript.Echo "Timestamp:" & vbTab & GPMBackup.TimeStamp 123 WScript.Echo "Backup ID:" & vbTab & GPMBackup.ID 124 125 iSuccessCount = iSuccessCount + 1 126 Else 127 ' reset the failure flag 128 bFailure = False 129 End If 130 Next 131 132 WScript.Echo vbCrLf 133 WScript.Echo "Backup succeeded for " & iSuccessCount & " GPOs." 134 WScript.Echo "Backup failed for " & iFailureCount & " GPOs." 135 136 End Function 137 138 139 ' Takes a WScript.Arguments object and returns a dictionary object 140 ' containing the named arguments and values that were passed in 141 Function ProcessCommandLineArguments() 142 143 Dim szBackupLocation : szBackupLocation = "" 144 Dim szDomainName : szDomainName = "" 145 Dim szComment : szComment = "" 146 147 ' Check if this is cscript. If not, print an error and bail out 148 149 If UCase(Right(WScript.FullName,11)) = "WSCRIPT.EXE" Then 150 WScript.Echo "You must use cscript.exe to execute this script." 151 WScript.Quit(-1) 152 End If 153 154 If WScript.Arguments.Length = 0 Then 155 WScript.Arguments.ShowUsage() 156 WScript.Quit(-1) 157 End If 158 159 Dim Result : Set Result = CreateObject("Scripting.Dictionary") 160 161 szBackupLocation = WScript.Arguments(0) 162 163 ' Get the comment to use, if specified 164 If WScript.Arguments.Named.Exists("Comment") Then 165 szComment = WScript.Arguments.Named("Comment") 166 End If 167 168 If WScript.Arguments.Named.Exists("Domain") Then 169 szDomainName = WScript.Arguments.Named("Domain") 170 End If 171 172 ' Get the current domain if none was specified 173 If szDomainName = "" Then 174 szDomainName = GetDNSDomainForCurrentUser() 175 End If 176 177 Result.Add "Domain" , szDomainName 178 Result.Add "BackupLocation" , szBackupLocation 179 Result.Add "Comment" , szComment 180 181 Set ProcessCommandLineArguments = Result 182 End Function 183 184 185 </script> 186 187 <!-- Usage and command line argument information --> 188 <runtime> 189 190 <description> 191 Backs up all GPOs in the a given domain to the specified file system location. 192 </description> 193 194 <unnamed name="BackupLocation" helpstring="File system location to back up to" type="string" required="true" /> 195 <named name="Comment" helpstring="Optional comment for the backup" type="string" required="false" /> 196 <named name="Domain" helpstring="DNS name of domain" type="string" required="false" /> 197 198 <example> 199 Example: BackupAllGPOs.wsf c:\GPO-Backups /comment:"Weekly backup" 200 </example> 201 202 </runtime> 203 204 </job>