Create_and_Link_New_GPO.wsf (In the book)

Jan
24
2013

 

<job>
<comment>
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!
</comment>
<runtime>
    <description>Create a new GPO and link to a designated OU.</description>
    <named name="OU"
        helpstring="Distinguished Name of the OU to link to."
        type="string" required="true" />
    <named name="GPO"
        helpstring="Name of GPO to create."
        type="string" required="true" />
    <named name="TOP"
        helpstring="Set new GPO on top of precedence."
        type="simple" required="false" />
</runtime>

<script language="VBScript" src="GPMgmt.vbs" />
<script>
CheckCScript
CreateGPMObjects

If WSH.Arguments.Count > 0 Then
    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
Else
    ShowUsage
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

Sub ShowUsage
WScript.Echo "Usage:"
WScript.Echo "CSCRIPT.EXE //nologo /OU:Path_To_OU /GPO:GPO_Name [/TOP]"
WScript.Quit
End Sub
</script>
</job>