Restore_GPO.hta (In the book)

Jan
25
2013

 

<HTML>
<HEAD>
<!--
Script : List_GPMC_Information.wsf
From the book "Group Policy" by Jeremy Moskowitz

The contents of this script are provided "as is".  
No warranty of any kind, either express or implied, is made in relation 
to the availability, accuracy, reliability or content of this script.
ALWAYS test scripts before using in your production network!
-->
<TITLE>Restore GPO</TITLE>
  <HTA:APPLICATION ID="RestoreGPO" 
    APPLICATIONNAME="RestoreGPO" 
    BORDER="dialog"
    CAPTION="yes"
    CONTEXTMENU="yes"
    ICON="graphics/title.ico"
    INNERBORDER="yes"
    MINIMIZEBUTTON="yes"
    MAXIMIZEBUTTON="yes"
    SCROLLABLE="yes"
    SHOWINTASKBAR="yes"
    SINGLEINSTANCE="yes"
    SYSMENU="yes"
    WINDOWSTATE="maximize">
  <LINK href='GroupPolicy.css' rel="stylesheet" type="text/css">
  <SCRIPT src='GPMgmt.vbs' language="VBScript"></SCRIPT>
  <SCRIPT src='HTA_Help.vbs' language="VBScript"></SCRIPT>
</HEAD>
<BODY>
<SCRIPT language="VBScript">
Dim gpmBackup_List, gpmBackupDir

Sub Window_OnLoad
self.resizeTo 800,500
BackupFolder.Value = GetDefaultFolder(UseDate)
CreateGPMObjects
End Sub

Sub GetBackupFolder
BackupFolder.Value = BrowseForFolder
End Sub

Sub GetBackupList
Dim BackupList_HTML
Dim gpmSearchCriteria
set gpmSearchCriteria = gpm.CreateSearchCriteria()
gpmSearchCriteria.Add _
	gpmConstants.SearchPropertyBackupMostRecent, gpmConstants.SearchOPEquals, True

Set gpmBackupDir = gpm.GetBackupDir(BackupFolder.Value)
Set gpmBackup_List = gpmBackupDir.SearchBackups(gpmSearchCriteria)

For i=1 To gpmBackup_List.Count
	With gpmBackup_List.Item(i)
	    AddHTML BackupList_HTML, "<li>" & .GPODisplayName & _
	    	": " & "Backed up on " & .Timestamp & " (" & .Comment & ")"
    End With 
Next   
BackupList_Span.innerHTML = BackupList_HTML
SelectBackup_Div.style.display = "block"
End Sub

Sub RestoreGPO
Dim GPONumber
If IsNumeric(GPOToRestore.Value) Then
	GPONumber = CInt(GPOToRestore.Value)
	If GPONumber > 0 And GPONumber <= gpmBackup_List.Count Then
		StartRestore GPONumber
	Else
		MsgBox "Invalid GPO number, please try again", vbOKOnly+vbExclamation, "Error"
		GPOToRestore.Value = ""
	End If
Else
	MsgBox "Invalid GPO number, please try again", vbOKOnly+vbExclamation, "Error"
	GPOToRestore.Value = ""
End If
End Sub

Sub StartRestore(GPONumber)
Dim gpmRestoreGPO
Set gpmRestoreGPO = gpmBackupDir.GetBackup(gpmBackup_List.Item(GPONumber).ID)

Dim MsgText
With gpmRestoreGPO
MsgText = "About to restore the following GPO:" & VbCrLf & VbCrLf & _
		  "GPO FriendlyName: " & .GPODisplayName & VbCrLf & _
		  "Domain: " & .GPODomain & VbCrLf & _
		  "Comment: " & .Comment & VbCrLf & _
		  "GPO GUID: " & .GPOID & VbCrLf & _
		  "GPO Backup GUID: " & .ID & VbCrLf & _
		  "Backup Timestamp: " & .Timestamp & VbCrLf & VbCrLf & _
		  "Are you sure?"
End With

If MsgBox(MsgText,vbYesNo+vbQuestion,"Are you sure?") = vbYes Then
	Dim gpmResult, gpmResult_Status
	Set gpmResult = gpmDomain.RestoreGPO(gpmRestoreGPO,0)
	Set gpmResult_Status = gpmResult.Status
	If gpmResult_Status.Count <> 0 Then
		MsgText = ""
		For i = 1 To gpmResult_Status.Count
			MsgText = MsgText & gpmResult_Status.Item(i).Message & vbCrLf
		Next
		MsgBox MsgText, vbOKOnly + vbExclamation, "Errors occurred."
	Else
		MsgBox "GPO successfully restored!",vbOKOnly + vbExclamation,"Success"
	End If
End If
End Sub
</SCRIPT>
<div>Restore GPO</div>
<p>
<table border="0">
	<tr>
		<td>Backup Location</td>
		<td colspan="2"><input type="text" size="75" id="BackupFolder"></td>
	</tr>
	<tr>
		<td></td>
		<td align="left"><button id="BrowseButton" onclick="GetBackupFolder()">Change Location</button></td>
		<td align="right"><button id="GetListButton" onclick="GetBackupList()">Get GPO List</button></td>
	</tr>

</table>
<div id="SelectBackup_Div" style="display:none">
<table>
	<tr>
		<td>Enter GPO Number to Restore</td>
		<td align="left"><input type="text" size="2" id="GPOToRestore"></td>
		<td><button id="RestoreGPOButton" onclick="RestoreGPO()">Restore GPO</button></td>
	</tr>
</table>
</div>
<p>
<ol>
	<span id="BackupList_Span"></span>
</ol>


</BODY>
</HTML>