A.1 Backups Script

One of the most important tasks in any software environment is to perform backups. The following information shows you how to write a script or an application to do that task.

To write a Visual Basic script for performing backups:

  1. In Notepad, type the following script:

    dim x
    set x = CreateObject("GPExplorer.PolicyManager.1") 
    REM ***** BackupPolicy(GPO LDAP path, Backup folder path,
    REM ***** Domain, DC, comment, options)
    x.BackupPolicy "LDAP://DomainController/CN={GUID},CN=Policies,CN=System, DC=Domain","BackupFolder","DomainController","Domain","Comment", Reserved
  2. To obtain the LDAP path of the GPO (“LDAP://... DomainName”), use the ADSI Edit tool. If the ADSI Edit tool is not available, substitute the following information for the variables:

    DomainController

    Type the name of the primary domain controller of the domain. Provide the full computer name, which has the actual computer name along with the domain to which it belongs. You can find the full name on the Network Identification tab of the Property page of My Computer.

    GUID

    Type the GUID number that corresponds to the GPO you want to back up.

    Domain

    Type the name of the domain to which the GPO belongs.

    BackupFolder

    Type the path and the name of the folder to which you are going to back up the data.

    Comment

    Type a comment.

    Reserved

    Specify 0.

  3. When you are finished, your Visual Basic script should look similar to the following example:

    REM **** BACKUP *******************************************
    dim x
    set x = CreateObject("GPExplorer.PolicyManager.1")
    REM ***** BackupPolicy(GPO LDAP path, Backup folder path, 
    REM ***** Domain, DC, comment, options)
    x.BackupPolicy "LDAP://chld-dc-01.chld.prnt.net/CN={2FC79BD5-2288-4C37-9BAF-49E6F8D21A17},CN=Policies,CN=System,DC=chld,DC=prnt,DC=net", "C:\GPO Backups","chld-dc-01.chld.prnt.net","chld.prnt.net", "Backup from Scripts",0
    set x = nothing
    Wscript.Echo "Backup Completed!"
    REM *******************************************************
  4. To back up multiple GPOs, copy the BackupPolicy line as many times as necessary, modifying only the GUID for each GPO.

  5. Save the file with any suitable name, and an extension of .vbs for Visual Basic scripts, to the location of your choice (for example, backupGPOs.vbs).

To write a C# application for performing backups:

  1. In Microsoft Visual Studio, create a new project, then select Visual C# > Console Application.

  2. In Solution Explorer, select References, then right-click on Reference and select Add Reference.

  3. Select the Browse tab, navigate to GPA install location/Tools, and add the Interop.FULLARMORGPRSCRIPTOBJLib.dll assembly.

  4. Repeat Step 2, then select the Browse tab, navigate to GPA install location/Bin, and add the Interop.fazam2003expobj.dll assembly.

  5. Add the following libraries at the beginning of each application

    • using Interop.FULLARMORGPRSCRIPTOBJLib;
    • using Interop.fazam2003expobj;

    NOTE:Repeat steps 1-5 for each C# application you write for GPA operations.

  6. Create the following C# method:

    public static void BackupAD()
            {
                string sGPOPath = "LDAP://DomainController/CN={GUID},CN=Policies,CN=System,DC=Domain";
                string sPath = "BackupFolder";
                string sDomainController = "DomainController";
                string sDomain = "Domain";
                string sComment = "Comment";
                long sReserved = 0;
                IfaExplorerRoot oEXPRroot = new PolicyManager();
                oEXPRroot.BackupPolicy(sGPOPath, sPath, sDomain, sDomainController, sComment, sReserved);
                Console.WriteLine("GPO backed up successfully");
                Console.ReadKey();
            }
  7. To back up multiple GPOs, copy the sGPOPath line as many times as necessary, modifying only the GUID for each GPO.

    NOTE:Add the [STAThread] attribute before the main method, which indicates that the COM threading model for the application is single-threaded apartment.

  8. Your C# method should look similar to the following example:

    public static void BackupAD()
            {
                string sGPOPath = "LDAP://MYDOMAIN.LAB/CN={04835D24-7FAC-4B7B-B677-419E598593B0},CN=Policies,CN=System,DC=MYDOMAIN,DC=LAB";
                string sPath = "C://Folder/";
                string sDomainController = "MYDOMAINCONTROLLER.MYDOMAIN.LAB";
                string sDomain = "MYDOMAIN.LAB";
                string sComment = "Backup from Scripts";
                long sReserved = 0;
                IfaExplorerRoot oEXPRroot = new PolicyManager();
                oEXPRroot.BackupPolicy(sGPOPath, sPath, sDomain, sDomainController, sComment, sReserved);
                Console.WriteLine("GPO backed up successfully");
                Console.ReadKey();
            }
  9. Compile the solution, then execute the executable build.