Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 20 Next »

For an example of a generated VBScript source file please see Example VBScript Source


To generate a VBScript file select the 'XML Definition Files' tab on the main window then right click on a file and select 'Generate a VBScript file'.

 

This open the VBScript Generation Options dialog:


Output

PropertyValue
DirectoryThe folder where the source file is saved
File nameThe name of the VBscript source file. This defaults to the XML file name with a .vbs extension

 


Shutdown

 Generated code...
' Issue shutdown command

    objShell.Run "shutdown -s"



PropertyValue
Enable Shut DownSelect to enable this property
Shut downThe PC will shut own at the end of the backup
Log OffThe user will be logged out at the end of the backup

 


Application Event logs

Please note that Macrium Reflect Server and Server Plus Edition includes Windows Event logging built-in.

 Generated code...
    if iReturnCode = 2 then
' Handle XML validation error 
    elseif iReturnCode = 1 then
' Handle backup error 
        objShell.LogEvent 1, "Macrium Reflect - VBScript  Backup Failed"
    elseif iReturnCode = 0 then
' Everything OK
        objShell.LogEvent 0, "Macrium Reflect - Successful VBScript  Backup"
    end if



PropertyValue
Write a message on successEnable a Windows Event on successful backup
 Enter the message text in the edit box below
Write a message on failureEnable a Windows Event for failed backups
 Enter the message text in the edit box below

 

 

Note: The Windows Event logs generated by this script will have WHS (Windows Scripting host) as the message source

 


Run Once a Day

 Generated code...
' The following function call ensures that this script only runs once a day
    If HasRunToday Then 
        WScript.Quit
    End If

...........

'******************************************************************************
'* Function: HasRunToday
'*
'* Purpose:  determines if this script has run today
'*
'*
'* Input:    None
'* Output:   true if has run today false otherwise
'*
'******************************************************************************
Function HasRunToday
    Dim RegScriptKey
    Dim LastRunDate
    Dim objShell
 
    Set objShell = WScript.CreateObject("WScript.Shell")
    RegScriptKey = "HKCU\SOFTWARE\Macrium\Reflect\Scripts\" & WScript.ScriptFullName & "\LastRun"
    'Check if script has run today
    ON ERROR RESUME NEXT
    LastRunDate = objShell.RegRead(RegScriptKey)
    If LastRunDate =  cstr(Date) Then
          HasRunToday = true
    Else 
         objShell.RegWrite RegScriptKey, Date,"REG_SZ"
         HasRunToday  = false
    End If
    Set objShell = nothing
End Function


PropertyValue
Run Once a DayIf selected, will only enable this script to run once per day. This is useful for example if you want a backup to happen at first login or shutdown.



Run Programs

 Generated code...
' Run program before backup
    objShell.Run """C:\Users\Public\Admin\Before backup.bat""", 1, true
' Do the backup
    ExitCode = Backup ("""C:\Program Files\Macrium\Reflect\reflect.exe"" -e -w <BACKUP_TYPE> ""C:\Users\Dev\Documents\Reflect\example.xml""")
' Run program after backup
    objShell.Run """C:\users\public\Admin\After backup.exe""", 1, true


PropertyValue
Run a program or script at the startSelect to enable a program or script to run before the backup starts
File nameThe path and executable file name
ParametersOptional command line parameters for the program
Run a program or script at the startSelect to enable a program or script to run when the backup ends
File nameThe path and executable file name
ParametersOptional command line parameters for the program



Vista Elevation

 Generated code...
Sub VBMain()
    Dim objShell 
    Dim ExitCode
' Elevate this script for Admin privileges in Vista
    Elevate

.................


'******************************************************************************
'* Sub:     Elevate
'*
'* Purpose: Elevates this script for Vista UAC.
'*          This means that only one UAC Elevation prompt is displayed and
'*          functions/programs will not fail if they require admin privileges
'*
'* Input:   None
'* Output:  None
'*
'******************************************************************************
Sub Elevate
    Dim ShellApp
    Dim objShell 
    Dim objWshProcessEnv
    ' Don't elevate if run from Windows Task Scheduler
    If WScript.Arguments.Count > 0 Then 
        If WScript.Arguments.Item(0) = "-s" then
              Exit Sub
        End If
    End If
    Set objShell = WScript.CreateObject("WScript.Shell")
    Set objWshProcessEnv = objShell.Environment("PROCESS")
    If objWshProcessEnv("ELEVATED_APP") <> "True" Then
        objWshProcessEnv("ELEVATED_APP") = "True"
        Set ShellApp = CreateObject("Shell.Application") 
        Call ShellApp.ShellExecute("""" &  WScript.FullName  & """", """" & WScript.ScriptFullName & """" & " " & GetBackupTypeParameter,  , "runas")
        set ShellApp = nothing
        Set objWshProcessEnv = nothing
        wscript.quit 
    End If
    Set objWshProcessEnv = nothing
    Set objShell = nothing
End Sub 


PropertyValue
Enable ElevationIf selected will enable UAC elevation for the entire script. This enables functions and programs to run outside the context of Macrium Reflect without requesting further elevation.

 


Directory Synchronization

 Generated code...
    SynchroniseDirectories ExitCode, "C:\C++"
................


'******************************************************************************
'* Function: RobocopyExists
'*
'* Purpose:  determines whether the program Robocopy can be called from the
'*           command line.
'*
'* Input:    None
'* Output:   boolean - true if Robocopy exists
'*
'******************************************************************************
Function RobocopyExists()
    On Error Resume Next
    Dim objShell
    Dim objExec
    Set objShell = WScript.CreateObject("WScript.Shell")
    Set objExec = objShell.Exec ("robocopy.exe", 0)

    If Err.Number = 0 Then
        RobocopyExists = True
    Else
        RobocopyExists = False
    End If
End Function


'******************************************************************************
'* Function: SynchroniseDirectories
'*
'* Purpose:  Copies all files created by the previous backup / image
'*           to a supplied directory. Uses Macrium environment variables to
'*           determine which files to copy. 
'*
'* Input:    ExitCode - The exit code of the last backup
'*           strBackupDirectory - Directory to copy to
'*
'******************************************************************************
Function SynchroniseDirectories(Byval ExitCode, Byval strSyncDirectory)
    Dim objShell
    Dim objWshProcessEnv
    Dim strEnvPrefix
    Dim strBackupDirectory
    Dim strCmdLine
    Dim iReturnCode    
    Dim fs
    Dim objSyncFiles
    Dim objBackupDirectory
    Dim objSyncDirectory
    Dim objBackupFile
    Dim objSyncFile
    Dim strExtension
    Dim dateBackupFile
    Dim dateSyncFile

' Only copy files if backup was successful 
    if ExitCode <> 0 Then
        Exit Function
    End If

    Set objShell         = WScript.CreateObject("WScript.Shell")
    Set objWshProcessEnv = objShell.Environment("VOLATILE")

' Get the prefix for the last backup set
    strEnvPrefix = objWshProcessEnv("MACRIUM_PREFIX")
    
' Get the directory where we just created a backup
    strBackupDirectory = objWshProcessEnv(strEnvPrefix + "_DIRECTORY")

    If right(strBackupDirectory, 1) = "\" Then strBackupDirectory = left(strBackupDirectory, len(strBackupDirectory)-1)
    If right(strSyncDirectory, 1)   = "\" Then strSyncDirectory   = left(strSyncDirectory, len(strSyncDirectory)-1)

    If RobocopyExists Then
        strCmdLine = "robocopy ""<SOURCE>"" ""<DESTINATION>"" *.mr* /copy:DAT /lev:0 /purge /r:0"
    Else
    ' Robocopy does not exist - using xcopy
        Set fs = CreateObject("Scripting.FilesystemObject")
        Set objBackupDirectory = fs.GetFolder(strBackupDirectory)
        Set objSyncDirectory = fs.GetFolder(strSyncDirectory)

        For Each objSyncFile in objSyncDirectory.Files
            strExtension = fs.GetExtensionName(objSyncFile)
            dateSyncFile = objSyncFile.DateLastModified
            If Left(strExtension,2) = "mr" Then
            ' Check if file has been deleted
                If Not(fs.FileExists(strBackupDirectory+"\"+objSyncFile.name)) Then
                    fs.DeleteFile(objSyncFile)
                Else
                ' Check if file has been modified
                    Set objBackupFile = fs.GetFile(strBackupDirectory+"\"+objSyncFile.name)
                    dateBackupFile = objBackupFile.DateLastModified
                    If DateDiff("m", dateBackupFile, dateSyncFile) <> 0 Then
                        fs.DeleteFile(objSyncFile)
                    End If
                End If
            End If
        Next

        strCmdLine = "xcopy ""<SOURCE>\*.mr*"" ""<DESTINATION>"" /c /d /h /i /v /y"
    End If

    strCmdLine = Replace(strCmdLine,"<SOURCE>", strBackupDirectory)
    strCmdLine = Replace(strCmdLine,"<DESTINATION>", strSyncDirectory)
    iReturnCode = objShell.Run(strCmdLine,0,true)

    if iReturnCode <> 0 then
' Handle synchronisation error
    else
' Everything OK
    end if

' Clean up
    Set objShell         = nothing
    Set objWshProcessEnv = nothing
End Function


PropertyValue
Enable directory synchronizationIf enabled will use the MS utility RoboCopy to synchronize the backup target directory with the supplied directory.
DirectoryEnter the folder that you want to copy/archive your backup files to

 


Click 'OK' to generate the VBScript source file



 

  • No labels