Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Maybe usefull to someone... Script to backup Microsoft Virtual Server

Status
Not open for further replies.

JadeKnight

IS-IT--Management
Feb 23, 2004
94
0
0
NO
'***************************************************************************************************************************
' VirtualServerBackup.vbs Ver. 1.0
'***************************************************************************************************************************
'Copyright (C) 2006 Nils-Erik Auråker, nils-erik.auraaker@toetip.com
'
'This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
'as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
'
'This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
'of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
'
'You should have received a copy of the GNU General Public License along with this program; if not, write to the
'Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
'
'Special thanks to Tommy Finsen for providing good input/solution(s)
'---------------------------------------------------------------------------------------------------------------------------

'********
'Abstract
'********
'Perform backup of all guests running on a Microsoft Virtual Server 2005, using shadowcopy.

'********
'Features
'********
'- Individually put a virtual server in saved state, execute shadow copy, and start the virtual server again before processing backup of files
'- Downtime for each server is : time used to save state + time to take shadow copy (normally less than a minute)
'- Remembers state of every server, if server was in save state before job started. It will continue to be in that state after backup
'- DO NOT interfere with other shadow copy jobs running on server (some scripts seen on the net do delete all shadow copy jobs on the server)
'- Detailed parsing of vshadow output, and gives you feedback if something goes wrong
'- An easy extendable log interface. Gives you detailed output to file, eventlog or both. It's also easy to extend to other type of media. Ex : e-mail, syslog, etc.


'*********
'Howto use
'*********
' 1) Copy this script to a directory on a Windows 2003 server running Microsoft Virtual Server 2005, ex : C:\Backup
' 2) Go to the section "Customizable variables" in this script and edit the exclude array. If you don't have anything to exclude, just leave it.
' 3) Run the script according to syntax

'******
'Syntax
'******
'cscript VirtualServerBackup.vbs
'cscript VirtualServerBackup.vbs /?

'************
'Requirements
'************
'Windows Script Host 5.6 or above
'VShadow.exe 2.2 or above (must reside in same directory as script)
'The server "Volume Shadow Copy" can not be disabled

'****************
'Supported System
'****************
'Windows Server 2003 Family or higher

'********************
'Scheduled Task Howto
'********************
'There are number of ways to schedule a VirtualServerBackup, however the recommended secure way is to run the VirtualServerBackup in context of System. To schedule a task to run
'in system context, you can use command line task scheduler, schtasks.

'Command line Examples :

'Add a job to run every day at 23:00 in context of system
'schtasks /create /tn "Daily VirtualServerBackup" /tr "C:\VirtualServerBackup.vbs" /sc daily /st 23:00 /ru System

'Add a job to run every day at 23:00 in context of system, with path containing spaces
'schtasks /create /tn "Daily VirtualServerBackup" /tr "\"c:\program files\VirtualServerBackup\VirtualServerBackup.vbs\"" /sc daily /st 23:00 /ru System

'Delete scheduled task named "Daily VirtualServerBackup"
'schtasks /delete /tn "Daily VirtualServerBackup" /f

'Run scheduled task named "Daily VirtualServerBackup"
'schtasks /run /tn "Daily VirtualServerBackup"

'GUI :
'When specifying account credentials, use "NT AUTHORITY\SYSTEM" w/blank password to run under system.

'SECURITY NOTE :
'You should avoid schedule a task with username/password, this can easily be revealed.

'Schedule a task with path containing spaces :
'
'*************
'Release Notes
'*************
'*******************
'Ver 1.0 09/08/2006
'*******************
' - Initial Release

'******
'Prefix
'******
'a = Array
'b = Booalean
'c = Collection
'f_ = Function
'i = Integer
'o = Object
's = String
's_ = Sub
'CAPITAL = Constant

Option Explicit

'***************************************************************************************************************************
' DO NOT CHANGE CONSTANT
'***************************************************************************************************************************
Const ABORT = "VirtualServerBackup.vbs aborted with error(s). To determine cause, see logged information"
Const APPNAME = "VirtualServerBackup"
Const APPVER = "1.0"
Const AUTHOR = "Nils-Erik Auråker"
Const COPYYEAR = "2006"
Const ERRORL = 1
Const FORAPPENDING = 8
Const FORREADING = 1
Const FORWRITING = 2
Const INFORMATION = 4
Const SUCCESS = 0
Const VM_INVALID = 0
Const VM_TURNEDOFF = 1
Const VM_SAVED = 2
Const VM_TURNINGON = 3
Const VM_RESTORING = 4
Const VM_RUNNING = 5
Const VM_PAUSED = 6
Const VM_SAVING = 7
Const VM_TURNINGOFF = 8
Const VM_MERGINGDRIVES = 9
Const VM_DELETEMACHINE = 10
Const VSHADOW_SRCH_FAIL = "is a drive letter already in use"
Const VSHADOW_SRCH_ID = "* SNAPSHOT ID = {"
Const WARNING = 2

'***************************************************************************************************************************

Dim aNamedArg,aLog,aFiles,aExclude
Dim bFileLog,bCpy
Dim cFiles,cHdConn
Dim i,iErr,iNumberOfLogs,iEventLogLevel,iFileLogLevel,iState
Dim oShell,oFso,oRegEx,oFldr,oFile,oVS,oVM,oExec,oStdOut,oDrv,oHd
Dim sArg,sArchive,sFileLog,sStorage,sVshadow,sMountDrive,sSnapID,sLine,sFile,sSavedStateFilePath

'Create Shell object
Set oShell = Wscript.CreateObject("Wscript.Shell")

'Check if script is invoked by cscript engine, force if not
s_ForceUseCScript

'Create FileSystemObject
Set oFso = CreateObject("Scripting.FileSystemObject")

'Bind to arguments
Set aNamedarg = Wscript.Arguments.Named
s_LogInterface INFORMATION, "[Main Code] Number of named arguments provided : " & aNamedarg.Count

'Parse arguments in array
For each sArg in aNamedArg
s_ParseArg sArg, aNamedArg(sArg)
Next

'***************************************************************************************************************************
' Customizable variables
'***************************************************************************************************************************
aExclude = Array("exampleserver1","exampleserver2") 'Array of Guest(s) wich will NOT be processed for backup. If you don't have anything to exclude leave default.
iEventLogLevel = 0 'Loglevel eventlog 0 = None 1 = Success/Error 2 = Full 3 = Debug (NOTE : You should avoid setting this level higher than 1, use file for higher levels)
iFileLogLevel = 2 'Loglevel File 0 = None 1 = Success/Error 2 = Full 3 = Debug
iNumberOfLogs = 10 'Number of logs to store in root directory (sStorage) 0 = Do not delete any logs
sArchive = oShell.CurrentDirectory & "\Backup" 'Path to archive directory (root of backup) Default is scriptdir\Backup.
sFileLog = "" 'Path to logfile, leave blank to use same dir as storage, filename will be generated based upon date
sMountDrive = "X:" 'Driveletter where backup from shadowcopy will be mounted.
sStorage = oShell.CurrentDirectory & "\Log" 'Path to root of Log store. Default is scriptdir\Log
sVshadow = oShell.CurrentDirectory & "\vshadow.exe" 'Path to vshadow.exe. Default is scriptdir\vshadow.exe
'***************************************************************************************************************************
' Main Code
'***************************************************************************************************************************
s_LogInterface Success, "[Main Code] " & APPNAME & " " & APPVER & " " & "Started Successfully."
s_LogInterface INFORMATION, "[Main Code] Working directory is : " & oShell.CurrentDirectory

'Check sStorage path, abort if it doesn't exists and can't be created
s_LogInterface INFORMATION, "[Main Code] Call f_CreateFolder, " & sStorage
If f_CreateFolder(sStorage) <> True Then
s_LogInterface ERRORL, ABORT
wscript.quit
End If

'Switch start file log
bFileLog = True

If iFileLogLevel > 0 AND sFileLog = "" Then
sFileLog = sStorage & f_GetDayTimeDate & " VirtualServerBackup.log"
s_LogInterface SUCCESS, "[Main Code] Path to logfile is determined to be : " & sFileLog
s_LogInterface INFORMATION, "[Main Code] Write Log from array"
ElseIf iFileLogLevel > 0 Then
s_LogInterface SUCCESS, "[Main Code] Path to logfile is determined to be : " & sFileLog
s_LogInterface INFORMATION, "[Main Code] Write Log from array"
End If

'Check if vshadow.exe exists
If (oFso.FileExists(sVshadow)) Then
s_LogInterface INFORMATION, "[Main Code] vshadow.exe found in path specified by sVshadow."
Else
s_LogInterface ERRORL, "[Main Code] vshadow.exe was NOT found in path specified by sVshadow (" & sVshadow & ")."
s_LogInterface ERRORL, ABORT
wscript.quit
End If

'Create Virtual Server Object
Set oVS = CreateObject("VirtualServer.Application")

s_LogInterface INFORMATION, "[Main Code] Finding all registered virtual machines."

'List out every Virtual Machine registered to the Virtual Server
For each oVM in oVS.VirtualMachines

s_LogInterface SUCCESS, "[Main Code] Found virtual machine with name : " & oVM.Name & ", verify against exclusion array (aExclude)."

'Comparing VM found against exclusion array
For i = 0 to UBound(aExclude)

'Exit if match found in array
If LCase(aExclude(i)) = LCase(oVM.Name) Then
s_LogInterface INFORMATION, "[Main Code] Virtual Machine : " & oVM.Name & " is listed in exclusion array. Backup will not be processed on this machine."
Exit For

'If Not found in exclusion array, processing backup
ElseIf i = UBound(aExclude) Then
s_LogInterface INFORMATION, "[Main Code] Virtual Machine : " & oVM.Name & " is NOT listed in exclusion array. Starting to process backup."
s_LogInterface INFORMATION, "[Main Code] Checking State of : " & oVM.Name

'Retrieve current state of machine
iState = oVM.State

'Perform backup if state allows
If f_PerformBackup(iState, oVM.name) Then

'Save state if running
If iState = VM_RUNNING or iState = VM_PAUSED Then
s_LogInterface INFORMATION, "[Main Code] Saving state of " & oVM.Name & "."
s_WaitForTask oVM.Save, "Save State", oVM.Name
'Get the savedstatefilepath
sSavedStateFilePath = oVM.SavedStateFilePath
End If

'Check sArchive path, abort if it doesn't exists and can't be created
s_LogInterface INFORMATION, "[Main Code] Call f_CreateFolder, " & sArchive & "\" & oVM.Name
If sArchive <> "" AND f_CreateFolder(sArchive& "\" & oVM.Name) <> True Then
s_LogInterface ERRORL, ABORT
wscript.quit
End If

'Create a shadowcopy of the drive containing virtual machine using vshadow
s_LogInterface INFORMATION, "[Main Code] Executing : " & sVshadow & " -p " & Left(oVM.File, 2)
Set oExec = oShell.Exec(sVshadow & " -p " & Left(oVM.File, 2))

'Looping allowing it to finish
Do Until oExec.Status = "1"
wscript.sleep 100
Loop

'Create sdtout object for redirect of output from vshadow
Set oStdOut = oExec.StdOut

'Grabbing output from vshadow
s_LogInterface INFORMATION, "[Main Code] Parsing output from : " & sVshadow & " -p " & Left(oVM.File, 2)
While Not oStdOut.AtEndOfStream
sLine = oStdOut.ReadLine
s_LogInterface INFORMATION, "[Main Code] - : " & sLine

'Parsing stdout searching for snapshot id
If InStr(UCase(sLine), VSHADOW_SRCH_ID) Then
s_LogInterface SUCCESS, "[Main Code] Found string containing SNAPSHOT ID, assigning it to sSnapID"
sSnapID = sLine
s_LogInterface INFORMATION, "[Main Code] sSnapID = " & sSnapID & ", now parsing string to get SNAP ID ONLY"
sSnapID = Mid(sSnapID, InStr(sSnapID, "{"), InStr(sSnapID, "}") - InStr(sSnapID, "{")+1)
s_LogInterface SUCCESS, "[Main Code] sSnapID = " & sSnapID & "."

End If

Wend

'Start the virtual machine if it was running/paused before state was saved
If iState = VM_RUNNING or iState = VM_Paused Then
s_LogInterface INFORMATION, "[Main Code] Resuming running state of : " & oVM.Name
s_WaitForTask oVM.StartUp, "Start", oVM.Name
End If

'Put the virtual machine back on pause if it was paused before backup
If iState = VM_Paused Then
s_LogInterface INFORMATION, "[Main Code] Resuming paused state of : " & oVM.Name
oVM.Pause
End If

'Mount the newly created shadow copy volume to sMountDrive
Set oExec = oShell.Exec(sVshadow & " -el=" & sSnapID & "," & sMountDrive)


'Looping allowing it to finish
Do Until oExec.Status = "1"
wscript.sleep 100
Loop

'Create sdtout object for redirect of output from vshadow
Set oStdOut = oExec.StdOut

'Grabbing output from vshadow
s_LogInterface INFORMATION, "[Main Code] Redirecting output from : " & sVshadow & " -el=" & sSnapID & "," & sMountDrive
While Not oStdOut.AtEndOfStream

sLine = oStdOut.ReadLine
s_LogInterface INFORMATION, "[Main Code] - : " & sLine
'Parsing stdout searching for "is a drive letter already in use"
If InStr(LCase(sLine), VSHADOW_SRCH_FAIL) Then
s_LogInterface ERRORL, "[Main Code] THE DRIVE : " & sMountDrive & " IS ALLREADY IN USE!!!!!!!!!!!!!!!!!!!!!!!!"
s_LogInterface ERRORL, "[Main Code] BACKUP FROM SHADOW COPY WILL NOT BE PROCESSED !!!!!!!!!!!!!!!!!!!!!!!!!!!!"
'Set copy flag to false
bCpy = False
Else
bCpy = True
End If
Wend

'Start copy data from shadow copy volume if successfully mounted
If bCpy Then

'Copy .VMC file from Shadow Copy Volume to backup directory (Path converted)
s_LogInterface INFORMATION, "[Main Code] Call f_CopyFile, " & sMountDrive & Right(oVM.File, Len(oVM.File) -2) & "," & sArchive & "\" & oVM.Name & "\"
f_CopyFile sMountDrive & Right(oVM.File, Len(oVM.File) -2), sArchive & "\" & oVM.Name & "\"

'Copy .VSV file from Shadow Copy Volume to backup directory (Path converted)
If sSavedStateFilePath <> "" Then
s_LogInterface INFORMATION, "[Main Code] Call f_CopyFile, " & sMountDrive & Right(sSavedStateFilePath, Len(sSavedStateFilePath) -2) & "," & sArchive & "\" & oVM.Name & "\"
f_CopyFile sMountDrive & Right(sSavedStateFilePath, Len(sSavedStateFilePath) -2), sArchive & "\" & oVM.Name & "\"
End If

'Copy files from Shadow Copy Volume to Backup directory
s_LogInterface INFORMATION, "[Main Code] Starting to copy data from shadow copy."
Set cHdConn = oVM.HardDiskConnections
s_LogInterface INFORMATION, "[Main Code] Finding drives used by : " & oVM.Name

For each oDrv in cHDConn
Set oHd = oDrv.HardDisk
s_LogInterface INFORMATION, "[Main Code] Found harddisk with path : " & oHd.File

'Convert path to match path in Shadow Copy Volume
sFile = sMountDrive & Right(oHd.File, Len(oHd.File) -2)
s_LogInterface SUCCESS, "[Main Code] Converted path to harddisk in Shadow Copy Volume is : " & sFile

'Copy harddisk to backup directory
s_LogInterface INFORMATION, "[Main Code] Call f_CopyFile, " & sFile & "," & sArchive & "\" & oVM.Name & "\"
f_CopyFile sFile, sArchive & "\" & oVM.Name & "\"

'Check if undoable drive exists
If oVM.Undoable Then
Set oHd = oDrv.UndoHardDisk
s_LogInterface INFORMATION, "[Main Code] Found undo harddisk with path : " & oHd.File

'Convert path to match path in Shadow Copy Volume
sFile = sMountDrive & Right(oHd.File, Len(oHd.File) -2)
s_LogInterface SUCCESS, "[Main Code] Converted path to undo harddisk in Shadow Copy Volume is : " & sFile

'Copy harddisk to backup directory
s_LogInterface INFORMATION, "[Main Code] Call f_CopyFile, " & sFile & "," & sArchive & "\" & oVM.Name & "\"
f_CopyFile sFile, sArchive & "\" & oVM.Name & "\"
Else
s_LogInterface INFORMATION, "[Main Code] No undo harddisk(s) was found connected with : " & oVM.Name

End If
Next

Set cHdConn = Nothing
Set oHd = Nothing

End If

'Delete the shadow copy volume, and remove the mount
Set oExec = oShell.Exec("cmd /C echo y | " & sVshadow & " -ds=" & sSnapId)

'Looping allowing it to finish
Do Until oExec.Status = "1"
wscript.sleep 100
Loop

'Create sdtout object for redirect of output from vshadow
Set oStdOut = oExec.StdOut

'Grabbing output from cmd/vshadow
s_LogInterface INFORMATION, "[Main Code] Redirecting output from : cmd /C echo y | " & sVshadow & " -ds=" & sSnapId
While Not oStdOut.AtEndOfStream
sLine = oStdOut.ReadLine
s_LogInterface INFORMATION, "[Main Code] - : " & sLine
Wend


End If

End If
Next
Next

'Logfile rollover
If iNumberOfLogs <> 0 Then
s_LogInterface INFORMATION, "[Main Code] Call s_KeepOnlyF " & sStorage & " " & iNumberOfLogs
s_KeepOnlyF sStorage, iNumberOfLogs
End If

s_LogInterface Success, "[Main Code] VirtualServerBackup.vbs ended Successfully."

Set oShell = Nothing
Set aNamedArg = Nothing
Set oFso = Nothing

'***************************************************************************************************************************
' FUNCTION
'***************************************************************************************************************************

Function f_CopyFile(sSrc,sDst)
'*****************************************************
' Purpose : Copy a file
' Input : String (sSrc) Path to file
' String (sDst) Path to destination (folder)
' Return : True if file was successfully copied
'*****************************************************
On Error Resume Next

oFso.CopyFile sSrc, sDst, TRUE

If err.number <> 0 Then
s_LogInterface ERRORL, "[f_CopyFile] Error occured copying file : " & sSrc & " to " & sDst & " The error was : " & err.description
f_CopyFile = False
Else
s_LogInterface SUCCESS, "[f_CopyFile] The file : " & sSrc & " was successfully copied to : " & sDst & "."
f_CopyFile = True
End If

End Function


Function f_CreateFolder(s)
'*************************************************************
' Purpose : Create a folder, or a subset of folders based upon
' one path. Support both path and unc path. Ex :
' \\fqdn\share\my\path or D:\share\my\path. If only
' \\fqdn\share or D:\Share exists, function loop
' folder creation until path provided is created.
' Input : String Path to folder
' Return : True if folder exists or successfully created
'************************************************************

Dim sUnc,sResult

On Error Resume Next

Dim sFolder

If Right(s, 1) <> "\" Then
s = s & "\"
End If

'Check if path is UNC replace base path \\server.fqdn\something with \
If Left(s, 2) = "\\" Then
s_LogInterface INFORMATION, "[Function f_CreateFolder] UNC Path : " & s
sUNC = s
Set oRegEx = New RegExp
oRegEx.Pattern = "\\\\[A-Za-z0-9.]+\\\w+\$?\\"
sResult = oRegEx.Replace (s, "")
s_LogInterface INFORMATION, "[Function f_CreateFolder] UNC stripped, result : " & sResult
sUnc = Left(s, Len(s) - Len(sResult))
sFolder = sUnc
End If

s_LogInterface INFORMATION, "[Function f_CreateFolder] Start creating directory structure : " & s

Do Until sFolder = s
sFolder = Left(s, InStr(Len(sFolder) + 1, s, "\"))
s_LogInterface INFORMATION, "[Function f_CreateFolder] Checking if folder exists : " & sFolder
If Not oFso.FolderExists(sFolder) Then
s_LogInterface INFORMATION, "[Function f_CreateFolder] Creating folder : " & sFolder
ofso.CreateFolder(sFolder)
If err.number <> 0 Then
s_LogInterface ERRORL, "[Function f_CreateFolder] Error creating : " & sFolder & ". The error was : " & err.description
Exit Function
End If
End If
Loop

f_CreateFolder = True

End Function

Function f_GetDayTimeDate()
'********************************************
' Purpose : Get Day,Date,Time
' Input : None
' Return : Current day of week + Date + Time
'********************************************

f_GetDayTimeDate = WeekDayName(Weekday(Now, vbUseSystemDayOfWeek),,vbUseSystemDayOfWeek) & " " & Date & " "

If CInt(Hour(Now)) < 10 Then
f_GetDayTimeDate = f_GetDayTimeDate & "0" & Hour(Now)
Else
f_GetDayTimeDate = f_GetDayTimeDate & Hour(Now)
End If

If CInt(Minute(Now)) < 10 Then
f_GetDayTimeDate = f_GetDayTimeDate & "0" & Minute(Now)
Else
f_GetDayTimeDate = f_GetDayTimeDate & Minute(Now)
End If

End Function

Function f_PerformBackup(iVMState, sVMName)
'*************************************************************
' Purpose : Check weather or not to perform backup of a virtual
' based upon state of machine.
' Input : Integer (iWMState) State of machine
' String (sVMName), name of VM where task is processed
' Return : True/False (Boolean)
'************************************************************
Select Case iVMState

Case VM_INVALID
s_LogInterface ERRORL, "[PerformBackup] State of : " & sVMName & " is INVALID. Backup will NOT be performed."
f_PerformBackup = False

Case VM_TURNEDOFF
s_LogInterface WARNING, "[PerformBackup] State of : " & sVMName & " is TURNED OFF. Backup will be performed."
s_LogInterface WARNING, "[PerformBackup] State of : " & sVMName & " will be maintained, the virtual machine will NOT be started"
f_PerformBackup = True

Case VM_SAVED
s_LogInterface WARNING, "[PerformBackup] State of : " & sVMName & " is SAVED. Backup will be performed."
s_LogInterface WARNING, "[PerformBackup] State of : " & sVMName & " will be maintained, the virtual machine will NOT be started after backup"
f_PerformBackup = True

Case VM_TURNINGON
s_LogInterface ERRORL, "[PerformBackup] State of : " & sVMName & " is TURNING ON. Backup will NOT be performed."
f_PerformBackup = False

Case VM_RESTORING
s_LogInterface ERRORL, "[PerformBackup] State of : " & sVMName & " is RESTORING. Backup will NOT be performed."
f_PerformBackup = False

Case VM_RUNNING
s_LogInterface SUCCESS, "[PerformBackup] State of : " & sVMName & " is RUNNING. Backup will be performed."
s_LogInterface INFORMATION, "[PerformBackup] State of : " & sVMName & " will be maintained, the virtual machine will be started after backup"
f_PerformBackup = True

Case VM_PAUSED
s_LogInterface WARNING, "[PerformBackup] State of : " & sVMName & " is PAUSED. Backup will be performed."
s_LogInterface WARNING, "[PerformBackup] State of : " & sVMName & " will be maintained, the virtual machine will PAUSED after backup"
f_PerformBackup = True

Case VM_SAVING
s_LogInterface ERRORL, "[PerformBackup] State of : " & sVMName & " is SAVING. Backup will NOT be performed."
f_PerformBackup = False

Case VM_TURNINGOFF
s_LogInterface ERRORL, "[PerformBackup] State of : " & sVMName & " is TURNING OFF. Backup will NOT be performed."
f_PerformBackup = False

Case VM_MERGINGDRIVES
s_LogInterface ERRORL, "[PerformBackup] State of : " & sVMName & " is MERGING DRIVES. Backup will NOT be performed."
f_PerformBackup = False

Case VM_DELETEMACHINE
s_LogInterface ERRORL, "[PerformBackup] State of : " & sVMName & " is DELETE MACHINE. Backup will NOT be performed."
f_PerformBackup = False

Case Else
s_LogInterface ERRORL, "[PerformBackup] State of : " & sVMName & " is UNKNOWN. Backup will NOT be performed."
f_PerformBackup = False
End Select

End Function

'***************************************************************************************************************************
' SUB
'***************************************************************************************************************************

Sub s_DeleteFile(s)
'*************************************
' Purpose : Delete folder
' Input : String (s) path to folder
'************************************
On Error Resume Next

err.clear

oFso.DeleteFile s, True

If err.number <> 0 Then
s_LogInterface ERRORL, "[s_DeleteFile] Failed to delete File : " & s & ". The error was : " & err.description & " (" & err.number & ")"
Else
s_LogInterface SUCCESS, "[s_DeleteFile] File : " & s & " was successfully deleted."
End If

End Sub

Sub s_EventLog(i, s)
'*************************************************************
' Purpose : Write Event to Eventlog.
' EventLog Level 0 = None 1 = Success/Error 2 = Full
' Input : i : Event type, Integer
' 0 SUCCESS
' 1 ERROR (Const ERRORL)
' 2 WARNING
' 4 INFORMATION
' s : Log message, String
'*************************************************************
'Const SUCCESS = 0
'Const ERRORL = 1
'Const WARNING = 2
'Const INFORMATION = 4
'0 = None 1 = Success/Error 2 = Full


Select Case iEventLogLevel
Case 1
Select Case i
Case SUCCESS
oShell.LogEvent i, s
Case ERRORL
oShell.LogEvent i, s
Case Else
Exit Sub
End Select
Case 2
Select Case i
Case SUCCESS
oShell.LogEvent i, s
Case ERRORL
oShell.LogEvent i, s
Case WARNING
oShell.LogEvent i, s
Case INFORMATION
oShell.LogEvent i, s
Case Else
Exit Sub
End Select
Case 3
Select Case i
Case SUCCESS
oShell.LogEvent i, s
Case ERRORL
oShell.LogEvent i, s
Case WARNING
oShell.LogEvent i, s
Case INFORMATION
oShell.LogEvent i, s
Case Else
Exit Sub
End Select
Case Else
Exit Sub
End Select

End Sub

Sub s_FileLog(i, s)
'*********************************************************************************************
' Purpose : Write Event to logfile. Build an array of events until path logfile is determined.
' If logfile can not be written. Events are redirected to eventlog.
' EventLog Level 0 = None 1 = Success/Error 2 = Full
' Input : i : Event type, Integer
' 0 SUCCESS
' 1 ERROR (Const ERRORL)
' 2 WARNING
' 4 INFORMATION
' s : Log message, String
' Use : bFileLog True/False when actually start logging to file
' : aLog to store temporary log entries before writing
'*********************************************************************************************
'Const SUCCESS = 0
'Const ERRORL = 1
'Const WARNING = 2
'Const INFORMATION = 4
'0 = None 1 = Success/Error 2 = Full
'Dim aLog
'Dim bFileLog


On Error Resume Next

Dim iArr
Dim oLog


'Make sure errorlevel 0
err.clear

'Log to file if bFileLog flag is true
If bFileLog = True Then

Set oLog = oFso_OpenTextFile(sFileLog, FORAPPENDING, True)

If err.number <> 0 Then
iErr = err.number
'Clear error
err.clear
'Check if log folder exists, try to create
If f_CreateFolder(Left(sFileLog, InStrRev(sFileLog, "\"))) Then
Set oLog = oFso_OpenTextFile(sFileLog, FORAPPENDING, True)

Else

'Abort filelog operation if can't be written, switch to EventLog
iEventLogLevel = 2
iFileLogLevel = 0
err.Raise = iErr
s_LogInterface ERRORL, "[Sub s_FileLog] Error occured while writing to logfile : " & sFileLog & ". The error was : " & err.description
s_LogInterface WARNING, "[Sub s_FileLog] Events will permanently be redirected to eventlog. All events occured prior to this warning will now be written."

'Write events from array to eventlog
If IsArray(aLog) AND aLog(0) <> "" Then
For iArr = 0 To UBound(aLog)
s_LogInterface CInt(Left(aLog(iArr), 1)), Right(aLog(iArr), Len(aLog(iArr)) - 1)
Next

ReDim aLog(0)
End If

Exit Sub
End If
End If

'Write events from array to logfile
If IsArray(aLog) AND aLog(0) <> "" Then
For iArr = 0 To UBound(aLog)
oLog.WriteLine Right(aLog(iArr), Len(aLog(iArr)) - 1)
Next

ReDim aLog(0)
End If

Select Case iFileLogLevel
Case 1
Select Case i
Case SUCCESS
oLog.WriteLine Now & " SUCCESS " & s
Case ERRORL
oLog.WriteLine Now & " ERROR " & s
Case Else
Exit Sub
End Select
Case 2
Select Case i
Case SUCCESS
oLog.WriteLine Now & " SUCCESS " & s
Case ERRORL
oLog.WriteLine Now & " ERROR " & s
Case WARNING
oLog.WriteLine Now & " WARNING " & s
Case INFORMATION
oLog.WriteLine Now & " INFORMATION " & s
Case Else
Exit Sub
End Select
Case 3
Select Case i
Case SUCCESS
oLog.WriteLine Now & " SUCCESS " & s
Case ERRORL
oLog.WriteLine Now & " ERROR " & s
Case WARNING
oLog.WriteLine Now & " WARNING " & s
Case INFORMATION
oLog.WriteLine Now & " INFORMATION " & s
Case Else
Exit Sub
End Select
Case Else
Exit Sub
End Select

oLog.close
Set oLog = Nothing
Else

'Log to array while path to logfile determined
If IsArray(aLog) Then
iArr = UBound(aLog) + 1
ReDim Preserve aLog(iArr)
Else
ReDim aLog(1)
End If

Select Case iFileLogLevel
Case 1
Select Case i
Case SUCCESS
ReDim Preserve aLog(iArr)
aLog(iArr) = i & Now & " SUCCESS " & s
Case ERRORL
ReDim Preserve aLog(iArr)
aLog(iArr) = i & Now & " ERROR " & s
Case Else
Exit Sub
End Select
Case 2
Select Case i
Case SUCCESS
ReDim Preserve aLog(iArr)
aLog(iArr) = i & Now & " SUCCESS " & s
Case ERRORL
ReDim Preserve aLog(iArr)
aLog(iArr) = i & Now & " ERROR " & s
Case WARNING
ReDim Preserve aLog(iArr)
aLog(iArr) = i & Now & " WARNING " & s
Case INFORMATION
ReDim Preserve aLog(iArr)
aLog(iArr) = i & Now & " INFORMATION " & s
Case Else
Exit Sub
End Select
Case 3
Select Case i
Case SUCCESS
ReDim Preserve aLog(iArr)
aLog(iArr) = i & Now & " SUCCESS " & s
Case ERRORL
ReDim Preserve aLog(iArr)
aLog(iArr) = i & Now & " ERROR " & s
Case WARNING
ReDim Preserve aLog(iArr)
aLog(iArr) = i & Now & " WARNING " & s
Case INFORMATION
ReDim Preserve aLog(iArr)
aLog(iArr) = i & Now & " INFORMATION " & s
Case Else
Exit Sub
End Select

Case Else
Exit Sub
End Select

End If

End Sub

Sub s_ForceUseCScript()
'********************************************
' Purpose: Force script to use cscript engine
'********************************************
If Not WScript.FullName = WScript.Path & "\cscript.exe" Then
oShell.Popup "Script is not invoked uder cscript engine. Relaunching under cscript...",5,"WSCRIPT"
oShell.Run "cmd.exe /k " & WScript.Path & "\cscript.exe //NOLOGO " & Chr(34) & WScript.scriptFullName & Chr(34),1,False
WScript.Quit 0
End If
End Sub

Sub s_KeepOnlyF(s, i)
'***************************************************
' Purpose : Delete obsolete files based upon date.
' Newest files are kept.
' Input : String (s) Path to root of files
' Integer (i) Number of files to keep
'***************************************************
On Error Resume Next

Dim aDate
' Dim oFldr,oFile
' Dim cFiles
Dim iCnt
Dim n,m,temp

Set oFldr = oFso.GetFolder(s)

If err.number <> 0 Then
s_LogInterface ERRORL, "[s_KeepOnlyF] An Error occured connecting to : " & s & " old files must be deleted manually."
Exit Sub
Else
s_LogInterface INFORMATION, "[s_KeepOnlyF] Connected to : " & s
End If

Set cFiles = oFldr.Files

If err.number <> 0 Then
s_LogInterface ERRORL, "[s_KeepOnlyF] An Error occured while listing files in folder : " & s & ". Old files must be deleted manually."
Exit Sub
Else
s_LogInterface INFORMATION, "[s_KeepOnlyF] Connected to : " & s
End If

'Check if number of files greater than i
If cFiles.Count <= i Then
s_LogInterface INFORMATION, "[s_KeepOnlyF] Number of file(s) in root is : " & cFiles.Count & ". Number is less than or equal to : " & i & " wich is the number of file(s) to keep."
Exit Sub
End If

s_LogInterface INFORMATION, "[s_KeepOnlyF] Number of files in root is : " & cFiles.Count & ". Number is greater than : " & i & " wich is the number of file(s) to keep."

'ReDim array to number of folders
ReDim aDate(cFiles.Count -1)

s_LogInterface INFORMATION, "[s_KeepOnlyF] Creating array with fixed size : " & cFiles.Count -1 & ". Adding date of folders to array"

iCnt = 0

'Add date creation of all folders to array
For Each oFile in cFiles
s_LogInterface INFORMATION, "[s_KeepOnlyF] aDate(" & iCnt & ") = " & oFile.DateCreated & " (Path : " & oFile.Path & ")"
aDate(iCnt) = oFile.DateCreated
iCnt = iCnt + 1
Next

'Sorting array by date, ascent sort order (The famous bubble sort)
s_LogInterface INFORMATION, "[s_KeepOnlyF] Sorting dates in array, ascent sort order"
For n = 0 to UBound(aDate) -1
For m = n+1 to UBound(aDate)
If aDate(m) < aDate(n) Then
temp = aDate(m)
aDate(m) = aDate(n)
aDate(n) = temp
End If
Next
Next

'Just logging
s_LogInterface INFORMATION, "[s_KeepOnlyF] Array sorted, result in ascent order : "
For iCnt = 0 To UBound(aDate)
s_LogInterface INFORMATION, "[s_KeepOnlyF] aDate(" & iCnt & ") = " & aDate(iCnt)
Next



'Cut array, keep (i) rows with date of folders wich is going to be deleted
ReDim Preserve aDate(UBound(aDate) -i)
s_LogInterface INFORMATION, "[s_KeepOnlyF] Array aDate cut, remaing rows are : "

'Even more logging (those debuggers ;))
For iCnt = 0 To UBound(aDate)
s_LogInterface INFORMATION, "[s_KeepOnlyF] aDate(" & iCnt & ") = " & aDate(iCnt)
Next

s_LogInterface INFORMATION, "[s_KeepOnlyF] Start deleting file(s)..."

For Each oFile in cFiles
For iCnt = 0 To UBound(aDate)
If oFile.DateCreated = aDate(iCnt) Then
s_LogInterface INFORMATION, "[s_KeepOnlyF] aDate(" & iCnt & ") = " & aDate(iCnt) & " matches DateCreated on file : " & oFile.Path & " (Date=" & oFile.DateCreated & ")"
s_DeleteFile oFile.Path
Exit For
End If
Next
Next
End Sub

Sub s_LogInterface(i, s)
'***********************************************************************************************
' Purpose : Log interface, write various type of events to different formats, NT Event, File etc
' Easily extend logging into other formats, ex mail,xml etc.
' Input : i : Event type, Integer 0 = None 1 = Success/Error 2 = Full
' s : Log message, String
'***********************************************************************************************

'Eventlog output
If iEventLogLevel = 1 or iEventLogLevel = 2 Then
Set oRegEx = New RegExp
oRegEx.Pattern = "\[.*\] "
s = oRegEx.Replace (s, "")
s_EventLog i, s
Set oRegEx = Nothing
End If

'Debug EventLog output
If iEventLogLevel = 3 Then
s_EventLog i, s
End If

'Logfile output
If iFileLogLevel = 1 or iFileLogLevel = 2 Then
Set oRegEx = New RegExp
oRegEx.Pattern = "\[.*\] "
s = oRegEx.Replace (s, "")
s_FileLog i, s
Set oRegEx = Nothing
End If

'Debug logfile output
If iFileLogLevel = 3 Then
s_FileLog i, s
End If

'Debug screen output
If iFileLogLevel = 3 or iEventLogLevel = 3 Then
wscript.echo s
End If

End Sub

Sub s_ShowSyntax()
'*******************************
' Purpose: Show syntax of script
'*******************************
wscript.echo "Cscript " & APPNAME & ".vbs [[/C:Filepath]|[/D:Filepath]] [/Options]" & VbNewLine
wscript.echo "/License Show License"
wscript.echo "/? or /Help Show help"
wscript.echo "/Copyright Show CopyRight"
wscript.quit

End Sub

Sub s_ShowCopyRight()
'************************
' Purpose: Show CopyRight
'************************
wscript.echo APPNAME & " Ver." & APPVER & " , Copyright (C) " & COPYYEAR & " " & AUTHOR & "." & vbNewLine & vbNewLine &_
APPNAME & " Ver." & APPVER & " comes with ABSOLUTELY NO WARRANTY;" & vbNewLine &_
"This is free software, and you are welcome to redistribute it under certain" & vbNewLine &_
"conditions; type " & APPNAME & ".vbs /License for details." & vbNewLine

End Sub

Sub s_ShowLicense()
'*************************
' Purpose: Show GNU Licens
'*************************
wscript.echo vbNewLine & vbNewLine
wscript.echo "TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION" & vbNewLine

wscript.echo "0. This License applies to any program or other work which contains a notice" & vbNewLine &_
"placed by the copyright holder saying it may be distributed under the terms of" & vbNewLine &_
"this General Public License. The " & Chr(34) & "Program" & Chr(34) & ", below, refers to any such program" & vbNewLine &_
"or work, and a " & Chr(34) & "work based on the Program" & Chr(34) & " means either the Program or any" & vbNewLine &_
"derivative work under copyright law: that is to say, a work containing the" & vbNewLine &_
"Program or a portion of it, either verbatim or with modifications and/or" & vbNewLine &_
"translated into another language. (Hereinafter, translation is included without" & vbNewLine &_
"limitation in the term " & Chr(34) & "modification" & Chr(34) & ".) Each licensee is addressed as " & Chr(34) & "you" & Chr(34) & "." & vbNewLine

wscript.echo "Activities other than copying, distribution and modification are not covered by" & vbNewLine &_
"this License; they are outside its scope. The act of running the Program is not" & vbNewLine &_
"restricted, and the output from the Program is covered only if its contents" & vbNewLine &_
"constitute a work based on the Program (independent of having been made by" & vbNewLine &_
"running the Program). Whether that is true depends on what the Program does." & vbNewLine & vbNewLine

wscript.echo "1. You may copy and distribute verbatim copies of the Program's source code" & vbNewLine &_
"as you receive it, in any medium, provided that you conspicuously and" & vbNewLine &_
"appropriately publish on each copy an appropriate copyright notice and" & vbNewLine &_
"disclaimer of warranty; keep intact all the notices that refer to this License" & vbNewLine &_
"and to the absence of any warranty; and give any other recipients of the" & vbNewLine &_
"Program a copy of this License along with the Program." & vbNewLine

wscript.echo "You may charge a fee for the physical act of transferring a copy, and you may" & vbNewLine &_
"at your option offer warranty protection in exchange for a fee." & vbNewLine & vbNewLine

wscript.echo "2. You may modify your copy or copies of the Program or any portion of it, thus" & vbNewLine &_
"forming a work based on the Program, and copy and distribute such modifications" & vbNewLine &_
"or work under the terms of Section 1 above, provided that you also meet all of" & vbNewLine &_
"these conditions:" & vbNewLine

wscript.echo "a) You must cause the modified files to carry prominent notices stating that" & vbNewLine &_
"you changed the files and the date of any change." & vbNewLine

wscript.echo "b) You must cause any work that you distribute or publish, that in whole or" & vbNewLine &_
"in part contains or is derived from the Program or any part thereof, to be" & vbNewLine &_
"licensed as a whole at no charge to all third parties under the terms of this" & vbNewLine &_
" License." & vbNewLine

wscript.echo "c) If the modified program normally reads commands interactively when run, you" & vbNewLine &_
"must cause it, when started running for such interactive use in the most" & vbNewLine &_
"ordinary way, to print or display an announcement including an appropriate" & vbNewLine &_
"copyright notice and a notice that there is no warranty (or else, saying that" & vbNewLine &_
"you provide a warranty) and that users may redistribute the program under these" & vbNewLine &_
"conditions, and telling the user how to view a copy of this License. (Exception" & vbNewLine &_
": if the Program itself is interactive but does not normally print such an" & vbNewLine &_
"announcement, your work based on the Program is not required to print an" & vbNewLine &_
"announcement.)" & vbNewLine

wscript.echo "These requirements apply to the modified work as a whole. If identifiable" & vbNewLine &_
"sections of that work are not derived from the Program, and can be reasonably" & vbNewLine &_
"considered independent and separate works in themselves, then this License," & vbNewLine &_
"and its terms, do not apply to those sections when you distribute them as" & vbNewLine &_
"separate works. But when you distribute the same sections as part of a whole" & vbNewLine &_
"which is a work based on the Program, the distribution of the whole must be" & vbNewLine &_
"on the terms of this License, whose permissions for other licensees extend" & vbNewLine &_
"to the entire whole, and thus to each and every part regardless of who wrote" & vbNewLine &_
"it." & vbNewLine

wscript.echo "Thus, it is not the intent of this section to claim rights or contest your" & vbNewLine &_
"rights to work written entirely by you; rather, the intent is to exercise the" & vbNewLine &_
"right to control the distribution of derivative or collective works based on" & vbNewLine &_
"the Program." & vbNewLine

wscript.echo "In addition, mere aggregation of another work not based on the Program with" & vbNewLine &_
"the Program (or with a work based on the Program) on a volume of a storage" & vbNewLine &_
"or distribution medium does not bring the other work under the scope of this" & vbNewLine &_
"License." & vbNewLine

wscript.echo "3. You may copy and distribute the Program (or a work based on it, under" & vbNewLine &_
"Section 2) in object code or executable form under the terms of Sections 1 and" & vbNewLine &_
"2 above provided that you also do one of the following:" & vbNewLine

wscript.echo "a) Accompany it with the complete corresponding machine-readable source code," & vbNewLine &_
"which must be distributed under the terms of Sections 1 and 2 above on a medium" & vbNewLine &_
"customarily used for software interchange; or," & vbNewLine

wscript.echo "b) Accompany it with a written offer, valid for at least three years, to give" & vbNewLine &_
"any third party, for a charge no more than your cost of physically performing" & vbNewLine &_
"source distribution, a complete machine-readable copy of the corresponding" & vbNewLine &_
"source code, to be distributed under the terms of Sections 1 and 2 above on a" & vbNewLine &_
"medium customarily used for software interchange; or," & vbNewLine

wscript.echo "c) Accompany it with the information you received as to the offer to distribute" & vbNewLine &_
"corresponding source code. (This alternative is allowed only for noncommercial" & vbNewLine &_
"distribution and only if you received the program in object code or executable" & vbNewLine &_
"form with such an offer, in accord with Subsection b above.)" & vbNewLine

wscript.echo "The source code for a work means the preferred form of the work for making" & vbNewLine &_
"modifications to it. For an executable work, complete source code means all the" & vbNewLine &_
"source code for all modules it contains, plus any associated interface" & vbNewLine &_
"definition files, plus the scripts used to control compilation and installation" & vbNewLine &_
"of the executable. However, as a special exception, the source code distributed" & vbNewLine &_
"need not include anything that is normally distributed (in either source or" & vbNewLine &_
"binary form) with the major components (compiler, kernel, and so on) of the" & vbNewLine &_
"operating system on which the executable runs, unless that component itself" & vbNewLine &_
"accompanies the executable." & vbNewLine

wscript.echo "If distribution of executable or object code is made by offering access to copy" & vbNewLine &_
"from a designated place, then offering equivalent access to copy the source" & vbNewLine &_
"code from the same place counts as distribution of the source code, even though" & vbNewLine &_
"third parties are not compelled to copy the source along with the object code." & vbNewLine

wscript.echo "4. You may not copy, modify, sublicense, or distribute the Program except as" & vbNewLine &_
"expressly provided under this License. Any attempt otherwise to copy, modify," & vbNewLine &_
"sublicense or distribute the Program is void, and will automatically terminate" & vbNewLine &_
"your rights under this License. However, parties who have received copies, or" & vbNewLine &_
"rights, from you under this License will not have their licenses terminated so" & vbNewLine &_
"long as such parties remain in full compliance." & vbNewLine

wscript.echo "5. You are not required to accept this License, since you have not signed it." & vbNewLine &_
"However, nothing else grants you permission to modify or distribute the Program" & vbNewLine &_
"or its derivative works. These actions are prohibited by law if you do not" & vbNewLine &_
"accept this License. Therefore, by modifying or distributing the Program (or" & vbNewLine &_
"any work based on the Program), you indicate your acceptance of this License to" & vbNewLine &_
" do so, and all its terms and conditions for copying, distributing or modifying" & vbNewLine &_
"the Program or works based on it." & vbNewLine

wscript.echo "6. Each time you redistribute the Program (or any work based on the Program)," & vbNewLine &_
"the recipient automatically receives a license from the original licensor to" & vbNewLine &_
"copy, distribute or modify the Program subject to these terms and conditions." & vbNewLine &_
"You may not impose any further restrictions on the recipients' exercise of the" & vbNewLine &_
"rights granted herein. You are not responsible for enforcing compliance by" & vbNewLine &_
"third parties to this License." & vbNewLine

wscript.echo "7. If, as a consequence of a court judgment or allegation of patent" & vbNewLine &_
"infringement or for any other reason (not limited to patent issues), conditions" & vbNewLine &_
"are imposed on you (whether by court order, agreement or otherwise) that" & vbNewLine &_
"contradict the conditions of this License, they do not excuse you from the" & vbNewLine &_
"conditions of this License. If you cannot distribute so as to satisfy" & vbNewLine &_
"simultaneously your obligations under this License and any other pertinent" & vbNewLine &_
"obligations, then as a consequence you may not distribute the Program at all." & vbNewLine &_
"For example, if a patent license would not permit royalty-free redistribution" & vbNewLine &_
"of the Program by all those who receive copies directly or indirectly through" & vbNewLine &_
"you, then the only way you could satisfy both it and this License would be to" & vbNewLine &_
"refrain entirely from distribution of the Program." & vbNewLine

wscript.echo "If any portion of this section is held invalid or unenforceable under any" & vbNewLine &_
"particular circumstance, the balance of the section is intended to apply and" & vbNewLine &_
"the section as a whole is intended to apply in other circumstances." & vbNewLine

wscript.echo "It is not the purpose of this section to induce you to infringe any patents or" & vbNewLine &_
"other property right claims or to contest validity of any such claims; this" & vbNewLine &_
"section has the sole purpose of protecting the integrity of the free software" & vbNewLine &_
"distribution system, which is implemented by public license practices. Many" & vbNewLine &_
"people have made generous contributions to the wide range of software" & vbNewLine &_
"distributed through that system in reliance on consistent application of that" & vbNewLine &_
"system; it is up to the author/donor to decide if he or she is willing to" & vbNewLine &_
"distribute software through any other system and a licensee cannot impose that" & vbNewLine &_
"choice." & vbNewLine

wscript.echo "This section is intended to make thoroughly clear what is believed to be a" & vbNewLine &_
"consequence of the rest of this License." & vbNewLine

wscript.echo "8. If the distribution and/or use of the Program is restricted in certain" & vbNewLine &_
"countries either by patents or by copyrighted interfaces, the original" & vbNewLine &_
"copyright holder who places the Program under this License may add an explicit" & vbNewLine &_
"geographical distribution limitation excluding those countries, so that" & vbNewLine &_
"distribution is permitted only in or among countries not thus excluded. In such" & vbNewLine &_
"case, this License incorporates the limitation as if written in the body of" & vbNewLine &_
"this License." & vbNewLine

wscript.echo "9. The Free Software Foundation may publish revised and/or new versions of the" & vbNewLine &_
"General Public License from time to time. Such new versions will be similar in" & vbNewLine &_
"spirit to the present version, but may differ in detail to address new" & vbNewLine &_
"problems or concerns." & vbNewLine

wscript.echo "Each version is given a distinguishing version number. If the Program specifies" & vbNewLine &_
"a version number of this License which applies to it and " & Chr(34) & "any later version" & Chr(34) & "," & vbNewLine &_
"you have the option of following the terms and conditions either of that" & vbNewLine &_
"version or of any later version published by the Free Software Foundation. If" & vbNewLine &_
"the Program does not specify a version number of this License, you may choose" & vbNewLine &_
"any version ever published by the Free Software Foundation." & vbNewLine

wscript.echo "10. If you wish to incorporate parts of the Program into other free programs" & vbNewLine &_
"whose distribution conditions are different, write to the author to ask for" & vbNewLine &_
"permission. For software which is copyrighted by the Free Software Foundation," & vbNewLine &_
"write to the Free Software Foundation; we sometimes make exceptions for this." & vbNewLine &_
"Our decision will be guided by the two goals of preserving the free status of" & vbNewLine &_
"all derivatives of our free software and of promoting the sharing and reuse of" & vbNewLine &_
"software generally." & vbNewLine

wscript.echo "NO WARRANTY" & vbNewLine

wscript.echo "11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR" & vbNewLine &_
"THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE" & vbNewLine &_
"STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE" & vbNewLine &_
"PROGRAM " & Chr(34) & "AS IS" & Chr(34) & " WITHOUT WARRANTY OF ANY KIND, EITHER" & vbNewLine &_
"EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF" & vbNewLine &_
"MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE" & vbNewLine &_
"QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE" & vbNewLine &_
"DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION." & vbNewLine

wscript.echo "12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL" & vbNewLine &_
"ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE" & vbNewLine &_
"PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY" & vbNewLine &_
"GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR" & vbNewLine &_
"INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA" & vbNewLine &_
"BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A" & vbNewLine &_
"FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER" & vbNewLine &_
"OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES." & vbNewLine

wscript.echo "END OF TERMS AND CONDITIONS"

wscript.quit

End Sub

Sub s_ParseArg(sArg, s)
'*******************************************************
' Purpose : Parse arguments passed from aNamedArg array.
' Input : sArg : Name of argument, String
' s : Value of argument, String
'*******************************************************
sArg = UCase(sArg)

Select Case sArg
Case "LICENSE"
s_ShowLicense
Case "?"
s_ShowSyntax
Case "HELP"
s_ShowSyntax
Case Else
s_ShowSyntax
End Select

End Sub

'************************************************************
'Purpose : Wait for a Virtual Server task to complete
'Input : Object (oTask), task wich should be waited for
' String (sTaskName), a given name for the task
' String (sVMName), name of VM where task is processed
'************************************************************
Sub s_WaitForTask(oTask, sTaskName, sVMName)

s_LogInterface INFORMATION, "[s_WaitForTask] Waiting to complete task : " & sTaskName & " " & " on " & sVMName

While Not oTask.IsComplete
wscript.sleep 100
Wend

s_LogInterface INFORMATION, "[s_WaitForTask] Task : " & sTaskName & " " & " on " & sVMName & " is now completed"

End Sub
 
' Purpose : Write Event to logfile. Build an array of events until path logfile is determined.
' If logfile can not be written. Events are redirected to eventlog.
' EventLog Level 0 = None 1 = Success/Error 2 = Full
' Input : i : Event type, Integer
' 0 SUCCESS
' 1 ERROR (Const ERRORL)
' 2 WARNING
' 4 INFORMATION
' s : Log message, String
' Use : bFileLog True/False when actually start logging to file
' : aLog to store temporary log entries before writing
'*********************************************************************************************
'Const SUCCESS = 0
'Const ERRORL = 1
'Const WARNING = 2
'Const INFORMATION = 4
'0 = None 1 = Success/Error 2 = Full
'Dim aLog
'Dim bFileLog


On Error Resume Next

Dim iArr
Dim oLog


'Make sure errorlevel 0
err.clear

'Log to file if bFileLog flag is true
If bFileLog = True Then

Set oLog = oFso_OpenTextFile(sFileLog, FORAPPENDING, True)

If err.number <> 0 Then
iErr = err.number
'Clear error
err.clear
'Check if log folder exists, try to create
If f_CreateFolder(Left(sFileLog, InStrRev(sFileLog, "\"))) Then
Set oLog = oFso_OpenTextFile(sFileLog, FORAPPENDING, True)

Else

'Abort filelog operation if can't be written, switch to EventLog
iEventLogLevel = 2
iFileLogLevel = 0
err.Raise = iErr
s_LogInterface ERRORL, "[Sub s_FileLog] Error occured while writing to logfile : " & sFileLog & ". The error was : " & err.description
s_LogInterface WARNING, "[Sub s_FileLog] Events will permanently be redirected to eventlog. All events occured prior to this warning will now be written."

'Write events from array to eventlog
If IsArray(aLog) AND aLog(0) <> "" Then
For iArr = 0 To UBound(aLog)
s_LogInterface CInt(Left(aLog(iArr), 1)), Right(aLog(iArr), Len(aLog(iArr)) - 1)
Next

ReDim aLog(0)
End If

Exit Sub
End If
End If

'Write events from array to logfile
If IsArray(aLog) AND aLog(0) <> "" Then
For iArr = 0 To UBound(aLog)
oLog.WriteLine Right(aLog(iArr), Len(aLog(iArr)) - 1)
Next

ReDim aLog(0)
End If

Select Case iFileLogLevel
Case 1
Select Case i
Case SUCCESS
oLog.WriteLine Now & " SUCCESS " & s
Case ERRORL
oLog.WriteLine Now & " ERROR " & s
Case Else
Exit Sub
End Select
Case 2
Select Case i
Case SUCCESS
oLog.WriteLine Now & " SUCCESS " & s
Case ERRORL
oLog.WriteLine Now & " ERROR " & s
Case WARNING
oLog.WriteLine Now & " WARNING " & s
Case INFORMATION
oLog.WriteLine Now & " INFORMATION " & s
Case Else
Exit Sub
End Select
Case 3
Select Case i
Case SUCCESS
oLog.WriteLine Now & " SUCCESS " & s
Case ERRORL
oLog.WriteLine Now & " ERROR " & s
Case WARNING
oLog.WriteLine Now & " WARNING " & s
Case INFORMATION
oLog.WriteLine Now & " INFORMATION " & s
Case Else
Exit Sub
End Select
Case Else
Exit Sub
End Select

oLog.close
Set oLog = Nothing
Else

'Log to array while path to logfile determined
If IsArray(aLog) Then
iArr = UBound(aLog) + 1
ReDim Preserve aLog(iArr)
Else
ReDim aLog(1)
End If

Select Case iFileLogLevel
Case 1
Select Case i
Case SUCCESS
ReDim Preserve aLog(iArr)
aLog(iArr) = i & Now & " SUCCESS " & s
Case ERRORL
ReDim Preserve aLog(iArr)
aLog(iArr) = i & Now & " ERROR " & s
Case Else
Exit Sub
End Select
Case 2
Select Case i
Case SUCCESS
ReDim Preserve aLog(iArr)
aLog(iArr) = i & Now & " SUCCESS " & s
Case ERRORL
ReDim Preserve aLog(iArr)
aLog(iArr) = i & Now & " ERROR " & s
Case WARNING
ReDim Preserve aLog(iArr)
aLog(iArr) = i & Now & " WARNING " & s
Case INFORMATION
ReDim Preserve aLog(iArr)
aLog(iArr) = i & Now & " INFORMATION " & s
Case Else
Exit Sub
End Select
Case 3
Select Case i
Case SUCCESS
ReDim Preserve aLog(iArr)
aLog(iArr) = i & Now & " SUCCESS " & s
Case ERRORL
ReDim Preserve aLog(iArr)
aLog(iArr) = i & Now & " ERROR " & s
Case WARNING
ReDim Preserve aLog(iArr)
aLog(iArr) = i & Now & " WARNING " & s
Case INFORMATION
ReDim Preserve aLog(iArr)
aLog(iArr) = i & Now & " INFORMATION " & s
Case Else
Exit Sub
End Select

Case Else
Exit Sub
End Select

End If

End Sub

Sub s_ForceUseCScript()
'********************************************
' Purpose: Force script to use cscript engine
'********************************************
If Not WScript.FullName = WScript.Path & "\cscript.exe" Then
oShell.Popup "Script is not invoked uder cscript engine. Relaunching under cscript...",5,"WSCRIPT"
oShell.Run "cmd.exe /k " & WScript.Path & "\cscript.exe //NOLOGO " & Chr(34) & WScript.scriptFullName & Chr(34),1,False
WScript.Quit 0
End If
End Sub

Sub s_KeepOnlyF(s, i)
'***************************************************
' Purpose : Delete obsolete files based upon date.
' Newest files are kept.
' Input : String (s) Path to root of files
' Integer (i) Number of files to keep
'***************************************************
On Error Resume Next

Dim aDate
' Dim oFldr,oFile
' Dim cFiles
Dim iCnt
Dim n,m,temp

Set oFldr = oFso.GetFolder(s)

If err.number <> 0 Then
s_LogInterface ERRORL, "[s_KeepOnlyF] An Error occured connecting to : " & s & " old files must be deleted manually."
Exit Sub
Else
s_LogInterface INFORMATION, "[s_KeepOnlyF] Connected to : " & s
End If

Set cFiles = oFldr.Files

If err.number <> 0 Then
s_LogInterface ERRORL, "[s_KeepOnlyF] An Error occured while listing files in folder : " & s & ". Old files must be deleted manually."
Exit Sub
Else
s_LogInterface INFORMATION, "[s_KeepOnlyF] Connected to : " & s
End If

'Check if number of files greater than i
If cFiles.Count <= i Then
s_LogInterface INFORMATION, "[s_KeepOnlyF] Number of file(s) in root is : " & cFiles.Count & ". Number is less than or equal to : " & i & " wich is the number of file(s) to keep."
Exit Sub
End If

s_LogInterface INFORMATION, "[s_KeepOnlyF] Number of files in root is : " & cFiles.Count & ". Number is greater than : " & i & " wich is the number of file(s) to keep."

'ReDim array to number of folders
ReDim aDate(cFiles.Count -1)

s_LogInterface INFORMATION, "[s_KeepOnlyF] Creating array with fixed size : " & cFiles.Count -1 & ". Adding date of folders to array"

iCnt = 0

'Add date creation of all folders to array
For Each oFile in cFiles
s_LogInterface INFORMATION, "[s_KeepOnlyF] aDate(" & iCnt & ") = " & oFile.DateCreated & " (Path : " & oFile.Path & ")"
aDate(iCnt) = oFile.DateCreated
iCnt = iCnt + 1
Next

'Sorting array by date, ascent sort order (The famous bubble sort)
s_LogInterface INFORMATION, "[s_KeepOnlyF] Sorting dates in array, ascent sort order"
For n = 0 to UBound(aDate) -1
For m = n+1 to UBound(aDate)
If aDate(m) < aDate(n) Then
temp = aDate(m)
aDate(m) = aDate(n)
aDate(n) = temp
End If
Next
Next

'Just logging
s_LogInterface INFORMATION, "[s_KeepOnlyF] Array sorted, result in ascent order : "
For iCnt = 0 To UBound(aDate)
s_LogInterface INFORMATION, "[s_KeepOnlyF] aDate(" & iCnt & ") = " & aDate(iCnt)
Next



'Cut array, keep (i) rows with date of folders wich is going to be deleted
ReDim Preserve aDate(UBound(aDate) -i)
s_LogInterface INFORMATION, "[s_KeepOnlyF] Array aDate cut, remaing rows are : "

'Even more logging (those debuggers ;))
For iCnt = 0 To UBound(aDate)
s_LogInterface INFORMATION, "[s_KeepOnlyF] aDate(" & iCnt & ") = " & aDate(iCnt)
Next

s_LogInterface INFORMATION, "[s_KeepOnlyF] Start deleting file(s)..."

For Each oFile in cFiles
For iCnt = 0 To UBound(aDate)
If oFile.DateCreated = aDate(iCnt) Then
s_LogInterface INFORMATION, "[s_KeepOnlyF] aDate(" & iCnt & ") = " & aDate(iCnt) & " matches DateCreated on file : " & oFile.Path & " (Date=" & oFile.DateCreated & ")"
s_DeleteFile oFile.Path
Exit For
End If
Next
Next
End Sub

Sub s_LogInterface(i, s)
'***********************************************************************************************
' Purpose : Log interface, write various type of events to different formats, NT Event, File etc
' Easily extend logging into other formats, ex mail,xml etc.
' Input : i : Event type, Integer 0 = None 1 = Success/Error 2 = Full
' s : Log message, String
'***********************************************************************************************

'Eventlog output
If iEventLogLevel = 1 or iEventLogLevel = 2 Then
Set oRegEx = New RegExp
oRegEx.Pattern = "\[.*\] "
s = oRegEx.Replace (s, "")
s_EventLog i, s
Set oRegEx = Nothing
End If

'Debug EventLog output
If iEventLogLevel = 3 Then
s_EventLog i, s
End If

'Logfile output
If iFileLogLevel = 1 or iFileLogLevel = 2 Then
Set oRegEx = New RegExp
oRegEx.Pattern = "\[.*\] "
s = oRegEx.Replace (s, "")
s_FileLog i, s
Set oRegEx = Nothing
End If

'Debug logfile output
If iFileLogLevel = 3 Then
s_FileLog i, s
End If

'Debug screen output
If iFileLogLevel = 3 or iEventLogLevel = 3 Then
wscript.echo s
End If

End Sub

Sub s_ShowSyntax()
'*******************************
' Purpose: Show syntax of script
'*******************************
wscript.echo "Cscript " & APPNAME & ".vbs [[/C:Filepath]|[/D:Filepath]] [/Options]" & VbNewLine
wscript.echo "/License Show License"
wscript.echo "/? or /Help Show help"
wscript.echo "/Copyright Show CopyRight"
wscript.quit

End Sub

Sub s_ShowCopyRight()
'************************
' Purpose: Show CopyRight
'************************
wscript.echo APPNAME & " Ver." & APPVER & " , Copyright (C) " & COPYYEAR & " " & AUTHOR & "." & vbNewLine & vbNewLine &_
APPNAME & " Ver." & APPVER & " comes with ABSOLUTELY NO WARRANTY;" & vbNewLine &_
"This is free software, and you are welcome to redistribute it under certain" & vbNewLine &_
"conditions; type " & APPNAME & ".vbs /License for details." & vbNewLine

End Sub

Sub s_ShowLicense()
'*************************
' Purpose: Show GNU Licens
'*************************
wscript.echo vbNewLine & vbNewLine
wscript.echo "TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION" & vbNewLine

wscript.echo "0. This License applies to any program or other work which contains a notice" & vbNewLine &_
"placed by the copyright holder saying it may be distributed under the terms of" & vbNewLine &_
"this General Public License. The " & Chr(34) & "Program" & Chr(34) & ", below, refers to any such program" & vbNewLine &_
"or work, and a " & Chr(34) & "work based on the Program" & Chr(34) & " means either the Program or any" & vbNewLine &_
"derivative work under copyright law: that is to say, a work containing the" & vbNewLine &_
"Program or a portion of it, either verbatim or with modifications and/or" & vbNewLine &_
"translated into another language. (Hereinafter, translation is included without" & vbNewLine &_
"limitation in the term " & Chr(34) & "modification" & Chr(34) & ".) Each licensee is addressed as " & Chr(34) & "you" & Chr(34) & "." & vbNewLine

wscript.echo "Activities other than copying, distribution and modification are not covered by" & vbNewLine &_
"this License; they are outside its scope. The act of running the Program is not" & vbNewLine &_
"restricted, and the output from the Program is covered only if its contents" & vbNewLine &_
"constitute a work based on the Program (independent of having been made by" & vbNewLine &_
"running the Program). Whether that is true depends on what the Program does." & vbNewLine & vbNewLine

wscript.echo "1. You may copy and distribute verbatim copies of the Program's source code" & vbNewLine &_
"as you receive it, in any medium, provided that you conspicuously and" & vbNewLine &_
"appropriately publish on each copy an appropriate copyright notice and" & vbNewLine &_
"disclaimer of warranty; keep intact all the notices that refer to this License" & vbNewLine &_
"and to the absence of any warranty; and give any other recipients of the" & vbNewLine &_
"Program a copy of this License along with the Program." & vbNewLine

wscript.echo "You may charge a fee for the physical act of transferring a copy, and you may" & vbNewLine &_
"at your option offer warranty protection in exchange for a fee." & vbNewLine & vbNewLine

wscript.echo "2. You may modify your copy or copies of the Program or any portion of it, thus" & vbNewLine &_
"forming a work based on the Program, and copy and distribute such modifications" & vbNewLine &_
"or work under the terms of Section 1 above, provided that you also meet all of" & vbNewLine &_
"these conditions:" & vbNewLine

wscript.echo "a) You must cause the modified files to carry prominent notices stating that" & vbNewLine &_
"you changed the files and the date of any change." & vbNewLine

wscript.echo "b) You must cause any work that you distribute or publish, that in whole or" & vbNewLine &_
"in part contains or is derived from the Program or any part thereof, to be" & vbNewLine &_
"licensed as a whole at no charge to all third parties under the terms of this" & vbNewLine &_
" License." & vbNewLine

wscript.echo "c) If the modified program normally reads commands interactively when run, you" & vbNewLine &_
"must cause it, when started running for such interactive use in the most" & vbNewLine &_
"ordinary way, to print or display an announcement including an appropriate" & vbNewLine &_
"copyright notice and a notice that there is no warranty (or else, saying that" & vbNewLine &_
"you provide a warranty) and that users may redistribute the program under these" & vbNewLine &_
"conditions, and telling the user how to view a copy of this License. (Exception" & vbNewLine &_
": if the Program itself is interactive but does not normally print such an" & vbNewLine &_
"announcement, your work based on the Program is not required to print an" & vbNewLine &_
"announcement.)" & vbNewLine

wscript.echo "These requirements apply to the modified work as a whole. If identifiable" & vbNewLine &_
"sections of that work are not derived from the Program, and can be reasonably" & vbNewLine &_
"considered independent and separate works in themselves, then this License," & vbNewLine &_
"and its terms, do not apply to those sections when you distribute them as" & vbNewLine &_
"separate works. But when you distribute the same sections as part of a whole" & vbNewLine &_
"which is a work based on the Program, the distribution of the whole must be" & vbNewLine &_
"on the terms of this License, whose permissions for other licensees extend" & vbNewLine &_
"to the entire whole, and thus to each and every part regardless of who wrote" & vbNewLine &_
"it." & vbNewLine

wscript.echo "Thus, it is not the intent of this section to claim rights or contest your" & vbNewLine &_
"rights to work written entirely by you; rather, the intent is to exercise the" & vbNewLine &_
"right to control the distribution of derivative or collective works based on" & vbNewLine &_
"the Program." & vbNewLine

wscript.echo "In addition, mere aggregation of another work not based on the Program with" & vbNewLine &_
"the Program (or with a work based on the Program) on a volume of a storage" & vbNewLine &_
"or distribution medium does not bring the other work under the scope of this" & vbNewLine &_
"License." & vbNewLine

wscript.echo "3. You may copy and distribute the Program (or a work based on it, under" & vbNewLine &_
"Section 2) in object code or executable form under the terms of Sections 1 and" & vbNewLine &_
"2 above provided that you also do one of the following:" & vbNewLine

wscript.echo "a) Accompany it with the complete corresponding machine-readable source code," & vbNewLine &_
"which must be distributed under the terms of Sections 1 and 2 above on a medium" & vbNewLine &_
"customarily used for software interchange; or," & vbNewLine

wscript.echo "b) Accompany it with a written offer, valid for at least three years, to give" & vbNewLine &_
"any third party, for a charge no more than your cost of physically performing" & vbNewLine &_
"source distribution, a complete machine-readable copy of the corresponding" & vbNewLine &_
"source code, to be distributed under the terms of Sections 1 and 2 above on a" & vbNewLine &_
"medium customarily used for software interchange; or," & vbNewLine

wscript.echo "c) Accompany it with the information you received as to the offer to distribute" & vbNewLine &_
"corresponding source code. (This alternative is allowed only for noncommercial" & vbNewLine &_
"distribution and only if you received the program in object code or executable" & vbNewLine &_
"form with such an offer, in accord with Subsection b above.)" & vbNewLine

wscript.echo "The source code for a work means the preferred form of the work for making" & vbNewLine &_
"modifications to it. For an executable work, complete source code means all the" & vbNewLine &_
"source code for all modules it contains, plus any associated interface" & vbNewLine &_
"definition files, plus the scripts used to control compilation and installation" & vbNewLine &_
"of the executable. However, as a special exception, the source code distributed" & vbNewLine &_
"need not include anything that is normally distributed (in either source or" & vbNewLine &_
"binary form) with the major components (compiler, kernel, and so on) of the" & vbNewLine &_
"operating system on which the executable runs, unless that component itself" & vbNewLine &_
"accompanies the executable." & vbNewLine

wscript.echo "If distribution of executable or object code is made by offering access to copy" & vbNewLine &_
"from a designated place, then offering equivalent access to copy the source" & vbNewLine &_
"code from the same place counts as distribution of the source code, even though" & vbNewLine &_
"third parties are not compelled to copy the source along with the object code." & vbNewLine

wscript.echo "4. You may not copy, modify, sublicense, or distribute the Program except as" & vbNewLine &_
"expressly provided under this License. Any attempt otherwise to copy, modify," & vbNewLine &_
"sublicense or distribute the Program is void, and will automatically terminate" & vbNewLine &_
"your rights under this License. However, parties who have received copies, or" & vbNewLine &_
"rights, from you under this License will not have their licenses terminated so" & vbNewLine &_
"long as such parties remain in full compliance." & vbNewLine

wscript.echo "5. You are not required to accept this License, since you have not signed it." & vbNewLine &_
"However, nothing else grants you permission to modify or distribute the Program" & vbNewLine &_
"or its derivative works. These actions are prohibited by law if you do not" & vbNewLine &_
"accept this License. Therefore, by modifying or distributing the Program (or" & vbNewLine &_
"any work based on the Program), you indicate your acceptance of this License to" & vbNewLine &_
" do so, and all its terms and conditions for copying, distributing or modifying" & vbNewLine &_
"the Program or works based on it." & vbNewLine

wscript.echo "6. Each time you redistribute the Program (or any work based on the Program)," & vbNewLine &_
"the recipient automatically receives a license from the original licensor to" & vbNewLine &_
"copy, distribute or modify the Program subject to these terms and conditions." & vbNewLine &_
"You may not impose any further restrictions on the recipients' exercise of the" & vbNewLine &_
"rights granted herein. You are not responsible for enforcing compliance by" & vbNewLine &_
"third parties to this License." & vbNewLine

wscript.echo "7. If, as a consequence of a court judgment or allegation of patent" & vbNewLine &_
"infringement or for any other reason (not limited to patent issues), conditions" & vbNewLine &_
"are imposed on you (whether by court order, agreement or otherwise) that" & vbNewLine &_
"contradict the conditions of this License, they do not excuse you from the" & vbNewLine &_
"conditions of this License. If you cannot distribute so as to satisfy" & vbNewLine &_
"simultaneously your obligations under this License and any other pertinent" & vbNewLine &_
"obligations, then as a consequence you may not distribute the Program at all." & vbNewLine &_
"For example, if a patent license would not permit royalty-free redistribution" & vbNewLine &_
"of the Program by all those who receive copies directly or indirectly through" & vbNewLine &_
"you, then the only way you could satisfy both it and this License would be to" & vbNewLine &_
"refrain entirely from distribution of the Program." & vbNewLine

wscript.echo "If any portion of this section is held invalid or unenforceable under any" & vbNewLine &_
"particular circumstance, the balance of the section is intended to apply and" & vbNewLine &_
"the section as a whole is intended to apply in other circumstances." & vbNewLine

wscript.echo "It is not the purpose of this section to induce you to infringe any patents or" & vbNewLine &_
"other property right claims or to contest validity of any such claims; this" & vbNewLine &_
"section has the sole purpose of protecting the integrity of the free software" & vbNewLine &_
"distribution system, which is implemented by public license practices. Many" & vbNewLine &_
"people have made generous contributions to the wide range of software" & vbNewLine &_
"distributed through that system in reliance on consistent application of that" & vbNewLine &_
"system; it is up to the author/donor to decide if he or she is willing to" & vbNewLine &_
"distribute software through any other system and a licensee cannot impose that" & vbNewLine &_
"choice." & vbNewLine

wscript.echo "This section is intended to make thoroughly clear what is believed to be a" & vbNewLine &_
"consequence of the rest of this License." & vbNewLine

wscript.echo "8. If the distribution and/or use of the Program is restricted in certain" & vbNewLine &_
"countries either by patents or by copyrighted interfaces, the original" & vbNewLine &_
"copyright holder who places the Program under this License may add an explicit" & vbNewLine &_
"geographical distribution limitation excluding those countries, so that" & vbNewLine &_
"distribution is permitted only in or among countries not thus excluded. In such" & vbNewLine &_
"case, this License incorporates the limitation as if written in the body of" & vbNewLine &_
"this License." & vbNewLine

wscript.echo "9. The Free Software Foundation may publish revised and/or new versions of the" & vbNewLine &_
"General Public License from time to time. Such new versions will be similar in" & vbNewLine &_
"spirit to the present version, but may differ in detail to address new" & vbNewLine &_
"problems or concerns." & vbNewLine

wscript.echo "Each version is given a distinguishing version number. If the Program specifies" & vbNewLine &_
"a version number of this License which applies to it and " & Chr(34) & "any later version" & Chr(34) & "," & vbNewLine &_
"you have the option of following the terms and conditions either of that" & vbNewLine &_
"version or of any later version published by the Free Software Foundation. If" & vbNewLine &_
"the Program does not specify a version number of this License, you may choose" & vbNewLine &_
"any version ever published by the Free Software Foundation." & vbNewLine

wscript.echo "10. If you wish to incorporate parts of the Program into other free programs" & vbNewLine &_
"whose distribution conditions are different, write to the author to ask for" & vbNewLine &_
"permission. For software which is copyrighted by the Free Software Foundation," & vbNewLine &_
"write to the Free Software Foundation; we sometimes make exceptions for this." & vbNewLine &_
"Our decision will be guided by the two goals of preserving the free status of" & vbNewLine &_
"all derivatives of our free software and of promoting the sharing and reuse of" & vbNewLine &_
"software generally." & vbNewLine

wscript.echo "NO WARRANTY" & vbNewLine

wscript.echo "11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR" & vbNewLine &_
"THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE" & vbNewLine &_
"STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE" & vbNewLine &_
"PROGRAM " & Chr(34) & "AS IS" & Chr(34) & " WITHOUT WARRANTY OF ANY KIND, EITHER" & vbNewLine &_
"EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF" & vbNewLine &_
"MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE" & vbNewLine &_
"QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE" & vbNewLine &_
"DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION." & vbNewLine

wscript.echo "12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL" & vbNewLine &_
"ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE" & vbNewLine &_
"PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY" & vbNewLine &_
"GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR" & vbNewLine &_
"INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA" & vbNewLine &_
"BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A" & vbNewLine &_
"FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER" & vbNewLine &_
"OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES." & vbNewLine

wscript.echo "END OF TERMS AND CONDITIONS"

wscript.quit

End Sub

Sub s_ParseArg(sArg, s)
'*******************************************************
' Purpose : Parse arguments passed from aNamedArg array.
' Input : sArg : Name of argument, String
' s : Value of argument, String
'*******************************************************
sArg = UCase(sArg)

Select Case sArg
Case "LICENSE"
s_ShowLicense
Case "?"
s_ShowSyntax
Case "HELP"
s_ShowSyntax
Case Else
s_ShowSyntax
End Select

End Sub

'************************************************************
'Purpose : Wait for a Virtual Server task to complete
'Input : Object (oTask), task wich should be waited for
' String (sTaskName), a given name for the task
' String (sVMName), name of VM where task is processed
'************************************************************
Sub s_WaitForTask(oTask, sTaskName, sVMName)

s_LogInterface INFORMATION, "[s_WaitForTask] Waiting to complete task : " & sTaskName & " " & " on " & sVMName

While Not oTask.IsComplete
wscript.sleep 100
Wend

s_LogInterface INFORMATION, "[s_WaitForTask] Task : " & sTaskName & " " & " on " & sVMName & " is now completed"

End Sub
 
EXCEPT WHEN OTHERWISE" & vbNewLine &_
"STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE" & vbNewLine &_
"PROGRAM " & Chr(34) & "AS IS" & Chr(34) & " WITHOUT WARRANTY OF ANY KIND, EITHER" & vbNewLine &_
"EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF" & vbNewLine &_
"MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE" & vbNewLine &_
"QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE" & vbNewLine &_
"DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION." & vbNewLine

wscript.echo "12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL" & vbNewLine &_
"ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE" & vbNewLine &_
"PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY" & vbNewLine &_
"GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR" & vbNewLine &_
"INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA" & vbNewLine &_
"BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A" & vbNewLine &_
"FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER" & vbNewLine &_
"OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES." & vbNewLine

wscript.echo "END OF TERMS AND CONDITIONS"

wscript.quit

End Sub

Sub s_ParseArg(sArg, s)
'*******************************************************
' Purpose : Parse arguments passed from aNamedArg array.
' Input : sArg : Name of argument, String
' s : Value of argument, String
'*******************************************************
sArg = UCase(sArg)

Select Case sArg
Case "LICENSE"
s_ShowLicense
Case "?"
s_ShowSyntax
Case "HELP"
s_ShowSyntax
Case Else
s_ShowSyntax
End Select

End Sub

'************************************************************
'Purpose : Wait for a Virtual Server task to complete
'Input : Object (oTask), task wich should be waited for
' String (sTaskName), a given name for the task
' String (sVMName), name of VM where task is processed
'************************************************************
Sub s_WaitForTask(oTask, sTaskName, sVMName)

s_LogInterface INFORMATION, "[s_WaitForTask] Waiting to complete task : " & sTaskName & " " & " on " & sVMName

While Not oTask.IsComplete
wscript.sleep 100
Wend

s_LogInterface INFORMATION, "[s_WaitForTask] Task : " & sTaskName & " " & " on " & sVMName & " is now completed"

End Sub
 
I've posted a long script here before using, code tags. It was truncated then. I hoped it wouldn't be truncated if I didn't use code tags. But I was wrong...

Anyway hope it can be usefull to someone :)...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top