Import_GPO.wsf (In the book)

Jan
25
2013

 

<job>
<comment>
Script : Import_GPO.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!
</comment>
<runtime>
    <description>Restore backed up GPOs.</description>
    <named name="Folder"
        helpstring="Folder containing the backed up GPOs"
        type="string" required="true" />
    <named name="List"
        helpstring="List the GPOs in the backup folder."
        type="simple" required="false" />
    <named name="GPO"
        helpstring="Name of GPO to import."
        type="string" required="true" />
    <named name="OU"
        helpstring="Path to OU linking to new GPO."
        type="string" required="true" />
</runtime>
<script language="VBScript" src="GPMgmt.vbs" />
<script>
CheckCScript
CreateGPMObjects

If WSH.Arguments.Count > 0 Then
    If WSH.Arguments.Named.Exists("Folder") Then
        BackupFolder = WSH.Arguments.Named("Folder")
        If Not(Right(ReportFolder,1) = "\") Then
            BackupFolder = BackupFolder & "\"
        End If
    Else
        ShowUsage
    End If
   	If WSH.Arguments.Named.Exists("LIST") Then
   		ListGPOs
   		WScript.Quit
   	End If
   	If WSH.Arguments.Named.Exists("OU") Then
   	    OUName = WSH.Arguments.Named("OU")
   	Else
   	    ShowUsage
   	End If
   	If WSH.Arguments.Named.Exists("GPO") Then
        GPOName = WSH.Arguments.Named("GPO")
    Else
        ShowUsage
    End If
    If WSH.Arguments.Named.Exists("TOP") Then
        LinkPrecedence = 1
    Else
        LinkPrecedence = -1
    End If
End If

If Not(FolderExists(BackupFolder)) Then
    WScript.Echo BackupFolder & " does not exist."
    WScript.Quit
End If

Set OU = GetOU(OUName)
If OU Is Nothing Then
	WScript.Echo "Unable to bind to OU " & OUName
	WScript.Quit
End If

Set NewGPO = gpmDomain.CreateGPO()
NewGPO.DisplayName = GPOName
Set SOM = gpmDomain.GetSOM(OU.distinguishedName)

On Error Resume Next
Set SOM_Link = SOM.CreateGPOLink(LinkPrecedence, NewGPO)
If Err.Number = 0 Then
    WScript.Echo "Successfully linked " & NewGPO.DisplayName & " to " & SOM.Name & "."
Else
    WScript.Echo "An error occurred while linking to OU."
End If

Set gpmBackupDir = gpm.GetBackupDir(BackupFolder)
Set gpmBackup_List = GetGPOBackups(BackupFolder)
For i=1 To gpmBackup_List.Count
    If gpmBackup_List.Item(i).GPODisplayName = GPOName Then
        GPO_ID = gpmBackup_List.item(i).ID
    	Set GPOToImport = gpmBackupDir.GetBackup(GPO_ID)
        Exit For
    End If
Next

WScript.StdOut.Write "Are you sure you want to import this GPO? (y or n) "
rs = WScript.StdIn.ReadLine 
WScript.Echo VbCrLf

If StrComp(rs,"y",vbTextCompare) = 0 Then 
	WScript.Echo "Importing selected GPO..."
	Set gpmResult = NewGPO.Import(0,GPOToImport)
	Set gpmResult_Status = gpmResult.Status
	If gpmResult_Status.count <> 0 Then 
		For i=1 to gpmResult_Status.Count
			WScript.Echo gpmResult_Status.Item(i).Message
		Next
	gpmResult.OverallStatus()
	Else
		WScript.Echo "The GPO was successfully imported."
	End If
Else
	WScript.Echo "Operation aborted. No GPOs imported."
End If 

Sub ListGPOs
Set gpmBackup_List = GetGPOBackups(BackupFolder)
WScript.Echo "Here is a list of the most current GPO backups:"
For i=1 To gpmBackup_List.Count
	With gpmBackup_List.Item(i)
        WScript.Echo .GPODisplayName & ": " _
            & "Backed up On " & .Timestamp _
            & " (" & .Comment & ")"
    End With 
Next
End Sub

Sub ShowUsage
WScript.Echo "Usage:"
WScript.Echo "CSCRIPT.EXE //nologo /Folder:path /GPO:Name /OU:OU_Name [/TOP]"
WScript.Quit
End Sub
</script>
</job>
Copyright © GPanswers.com. All rights reserved. GPanswers.com is a service of PolicyPak Software