''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Microsoft Windows ' Copyright (C) 2001 Microsoft Corporation ' ' File: srwmi.vbs ' ' Contents: call sr functionalities via wmi ' ' Script samples for configuring and operating System Restore (locally & remotely) ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Option Explicit ' explicit decalaration On Error Resume Next ' when error don't quit ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' global constraints for system restore configuration ' Eg: Configuration parameters including ' datastore size as a percentage of overall disk space ' Time interval for scheduled restore points (Global & session) ' Time interval for storage duration limit on restore points ' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Const DISK_PERCENT = "DiskPercent" Const RP_GLOBAL_INTERVAL = "RPGlobalInterval" Const RP_LIFE_INTERVAL = "RPLifeInterval" Const RP_SESSION_INTERVAL = "RPSessionInterval" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' global for getting command line argument ' Commands for System Restore functionality including ' On/Off SR ' Creating restore point ' Listing available restore points per machine ' Invoking a restore operation ' Validating restore success ' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Const ENABLE_SR = "/enable" Const DISABLE_SR = "/disable" Const RP_CREATION = "/rpcreate" Const RP_ENUMERATION = "/rpenum" Const LAST_RESTORE = "/lastrestore" Const RESTORE = "/restore" Const SET_DISK_PERCENT = "/diskpercent" Const SET_RP_GLOBAL_INTERVAL = "/rpglobalinterval" Const SET_RP_LIFE_INTERVAL = "/rplifeinterval" Const SET_RP_SESSION_INTERVAL = "/rpsessional" Const MACHINE_NAME = "/machine" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' global variable for machine name ' Define this variable to the machine name for remote functionality else leave blank. ' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Dim g_szMachineName ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Function: WmiEnablesr ' ' Content: call enable via wmi ' Description: This function allows administrators to enable full System Restore functionality ' if it has been disabled (default enabled). ' ' Return: HRESULT ' History: 16-03-2000 autlam Created ' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' function WmiEnableSr(szMachineName) ' local variable Dim oWmi Dim hResult Dim szGetObject ' assignment for get object statement if szMachineName = "" then szGetObject = "winmgmts:{impersonationLevel=impersonate}!root/default:SystemRestore" else szGetObject = "winmgmts:{impersonationLevel=impersonate}!//" & szMachineName & "/root/default:SystemRestore" end if ' get wmi object Set oWmi = GetObject(szGetObject) ' call enable sr hResult = oWmi.Enable("") ' release object Set oWmi = Nothing ' return WmiEnableSr = hResult end function ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Function: WmiDisableSr ' ' Content: call disable sr via wmi ' ' Return: HRESULT ' Description: This function allows administrators to disable full System Restore functionality. ' ' History: 16-03-2000 autlam Created ' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' function WmiDisableSr(szMachineName) ' local variable Dim oWmi Dim hResult Dim szGetObject ' assignment for get object statement if szMachineName = "" then szGetObject = "winmgmts:{impersonationLevel=impersonate}!root/default:SystemRestore" else szGetObject = "winmgmts:{impersonationLevel=impersonate}!//" & szMachineName & "/root/default:SystemRestore" end if ' get wmi object Set oWmi = GetObject(szGetObject) ' call disable sr hResult = oWmi.Disable("") ' release object Set oWmi = Nothing ' return WmiDisableSr = hResult end function ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Function: WmiRpCreate ' Description: This function allows administrators to create a restore point by calling ' STRestorePT.API. ' ' Content: call rp creation api via wmi ' ' Return: HRESULT ' ' History: 16-03-2000 autlam Created ' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' function WmiRpCreate(szRpDescription, dwRpType, dwEventType, szMachineName) ' local variable Dim oWmi Dim hResult Dim szGetObject ' assignment for get object statement if szMachineName = "" then szGetObject = "winmgmts:{impersonationLevel=impersonate}!root/default:SystemRestore" else szGetObject = "winmgmts:{impersonationLevel=impersonate}!//" & szMachineName & "/root/default:SystemRestore" end if ' get wmi object Set oWmi = GetObject(szGetObject) ' call CreateRestorePoint hResult = oWmi.CreateRestorePoint(szRpDescription, dwRpType, dwEventType) ' release object Set oWmi = Nothing ' return WmiRpCreate = hResult end function ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Function: WmiRestore ' Description: This function enables administrators to conduct remote or local restores via WMI ' script. ' ' Content: call restore via wmi ' ' Return: HRESULT ' ' History: 16-03-2000 autlam Created ' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' function WmiRestore(dwRpNum, szMachineName) ' local variable Dim oWmi Dim hResult Dim szGetObject ' assignment for get object statement if szMachineName = "" then szGetObject = "winmgmts:{impersonationLevel=impersonate}!root/default:SystemRestore" else szGetObject = "winmgmts:{impersonationLevel=impersonate}!//" & szMachineName & "/root/default:SystemRestore" end if ' get wmi object Set oWmi = GetObject(szGetObject) ' call restore hResult = oWmi.restore(dwRpNum) ' release object Set oWmi = Nothing ' return WmiRestore = hResult end function ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Function: WmiLastRestoreStatus ' Description: This function returns success/fail status of last restore on local or ' remote machine. ' ' Content: call GetLastRestoreStatus via wmi ' ' Return: HRESULT ' ' History: 16-03-2000 autlam Created ' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' function WmiLastRestoreStatus(szMachineName) ' local variable Dim oWmi Dim hResult Dim szGetObject ' assignment for get object statement if szMachineName = "" then szGetObject = "winmgmts:{impersonationLevel=impersonate}!root/default:SystemRestore" else szGetObject = "winmgmts:{impersonationLevel=impersonate}!//" & szMachineName & "/root/default:SystemRestore" end if ' get wmi object Set oWmi = GetObject(szGetObject) ' call lastRestoreStatus hResult = oWmi.GetLastRestoreStatus() ' release object Set oWmi = Nothing ' return WmiLastRestoreStatus = hResult end function ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Function: WmiSeeAllConfigValue ' Description: This function allows administrators to view all the configuration parameter settings on local ' or remote machine. ' ' Content: see all wmi system restore config value ' ' Return: a string of return value ' ' History: 26-04-2001 autlam Created ' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' function WmiSeeAllConfigValue(szMachineName) ' local variable Dim configSet Dim config Dim tempReturn Dim szGetObject ' assignment for get object statement if szMachineName = "" then szGetObject = "winmgmts:root/default" else szGetObject = "winmgmts://" & szMachineName & "/root/default" end if ' query wmi repository Set configSet = GetObject(szGetObject).InstancesOf("SystemRestoreConfig") For Each config in configSet tempReturn = "Disk Percent: " & config.DiskPercent & " Global Interval: " & config.RPGlobalInterval & " Life Interval: " & config.RPLifeInterval & " Session Interval: " & config.RPSessionInterval Next ' release interfaces Set configSet = Nothing ' return WmiSeeAllConfigValue = tempReturn end function ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Function: setOneConfigValue ' Description: This function sets each individual configuration parameter (see ' below). ' ' Content: set each wmi system restore config value ' ' Return: no return ' ' History: 26-04-2001 autlam Created ' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' sub setOneConfigValue(whichValue, value, szMachineName) ' local variable Dim configSet Dim szGetObject ' assignment for get object statement if szMachineName = "" then szGetObject = "winmgmts:{impersonationLevel=impersonate}!root/default:SystemRestoreConfig='SR'" else szGetObject = "winmgmts:{impersonationLevel=impersonate}!//" & szMachineName & "/root/default:SystemRestoreConfig='SR'" end if ' get wmi object Set configSet = GetObject(szGetObject) ' wmi assignment select case whichValue case DISK_PERCENT configSet.DiskPercent = value case RP_GLOBAL_INTERVAL configSet.RPGlobalInterval = value case RP_LIFE_INTERVAL configSet.RPLifeInterval = value case RP_SESSION_INTERVAL configSet.RPSessionInterval = value end select ' put the value configSet.Put_ ' release interfaces Set configSet = Nothing end sub ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Function: rpEnumeration ' Description: This function allows administrators to view a list of all existing restore points. ' ' Content: see all restore point info in current system ' ' Return: no return ' ' History: 26-04-2001 autlam Created ' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' sub rpEnumeration(szMachineName) ' local variable Dim rpSet Dim rp Dim szTemp Dim szGetObject ' assignment for get object statement if szMachineName = "" then szGetObject = "winmgmts:root/default" else szGetObject = "winmgmts://" & szMachineName & "/root/default" end if ' query wmi repository Set rpSet = GetObject(szGetObject).InstancesOf("SystemRestore") For Each rp in rpSet szTemp = "Name: " & rp.Description & " Number: " & rp.SequenceNumber & " Type: " & rp.RestorePointType & " Time: " & rp.CreationTime WScript.Echo szTemp Next if rpSet.Count = 0 then WScript.Echo "No restore point in system" end if ' release interfaces Set rpSet = Nothing end sub ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Function: displayHelp ' Description: This function allows administrators to view help for SR commands. ' ' Content: display help for this tool ' ' Return: no return ' ' History: 08-10-2001 autlam Created ' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' sub displayHelp() WScript.Echo "Usage:" WScript.Echo "" WScript.Echo " /enable - enable sr" WScript.Echo " /disable - disable sr" WScript.Echo " /rpcreate - create restore point" WScript.Echo " /rpenum - see rp information in current system" WScript.Echo " /lastrestore - see last restore result" WScript.Echo " /restore - restore to rp number" WScript.Echo " /diskpercent - set disk percent" WScript.Echo " /rpglobalinterval - set rp global interval" WScript.Echo " /rplifeinterval - set rp life interval" WScript.Echo " /rpsessioninterval - set rp sessional interval" WScript.Echo "" WScript.Echo "Default:" WScript.Echo " percent for disk percent: 12" WScript.Echo " rp global interval: 86400" WScript.Echo " rp life interval: 0" WScript.Echo " rp session interval: 0" WScript.Echo "" WScript.Echo "Note:" WScript.Echo " - rp number is needed for restore" WScript.Echo " please use /rpenum to see rp information" WScript.Echo " - you can use /machine option to specify the machine" WScript.Echo " so that the script can be run remotely" end sub ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' sample main ' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' dim tempIndex dim tempReturn dim objArgs dim helpBit ' assignment helpBit = 0 g_szMachineName = "" ' get command line argument Set objArgs = WScript.Arguments if objArgs.Count > 4 then displayHelp() helpBit = 1 else if objArgs.Count = 1 Or objArgs.Count = 3 then if objArgs.Count = 3 then g_szMachineName = objArgs(2) end if select case objArgs(0) case ENABLE_SR tempReturn = WmiEnableSr(g_szMachineName) helpBit = 1 WScript.Echo "hResult: " & tempReturn case DISABLE_SR tempReturn = WmiDisableSr(g_szMachineName) helpBit = 1 WScript.Echo "hResult: " & tempReturn case RP_CREATION tempReturn = WmiRpCreate("wmi", 0, 100, g_szMachineName) helpBit = 1 WScript.Echo "hResult: " & tempReturn case RP_ENUMERATION rpEnumeration(g_szMachineName) helpBit = 1 case LAST_RESTORE tempReturn = WmiLastRestoreStatus(g_szMachineName) helpBit = 1 WScript.Echo "Last Restore Status: " & tempReturn case SET_DISK_PERCENT setOneConfigValue DISK_PERCENT, 12, g_szMachineName helpBit = 1 tempReturn = WmiSeeAllConfigValue(g_szMachineName) WScript.Echo tempReturn case SET_RP_GLOBAL_INTERVAL setOneConfigValue RP_GLOBAL_INTERVAL, 86400, g_szMachineName helpBit = 1 tempReturn = WmiSeeAllConfigValue(g_szMachineName) WScript.Echo tempReturn case SET_RP_LIFE_INTERVAL setOneConfigValue RP_LIFE_INTERVAL, 0, g_szMachineName helpBit = 1 tempReturn = WmiSeeAllConfigValue(g_szMachineName) WScript.Echo tempReturn case SET_RP_SESSION_INTERVAL setOneConfigValue RP_SESSION_INTERVAL, 0, g_szMachineName helpBit = 1 tempReturn = WmiSeeAllConfigValue(g_szMachineName) WScript.Echo tempReturn end select end if if objArgs.Count = 2 Or objArgs.Count = 4 then if objArgs.Count = 4 then g_szMachineName = objArgs(3) end if select case objArgs(0) case SET_DISK_PERCENT setOneConfigValue DISK_PERCENT, objArgs(1), g_szMachineName helpBit = 1 tempReturn = WmiSeeAllConfigValue(g_szMachineName) WScript.Echo tempReturn case SET_RP_GLOBAL_INTERVAL setOneConfigValue RP_GLOBAL_INTERVAL, objArgs(1), g_szMachineName helpBit = 1 tempReturn = WmiSeeAllConfigValue(g_szMachineName) WScript.Echo tempReturn case SET_RP_LIFE_INTERVAL setOneConfigValue RP_LIFE_INTERVAL, objArgs(1), g_szMachineName helpBit = 1 tempReturn = WmiSeeAllConfigValue(g_szMachineName) WScript.Echo tempReturn case SET_RP_SESSION_INTERVAL setOneConfigValue RP_SESSION_INTERVAL, objArgs(1), g_szMachineName helpBit = 1 tempReturn = WmiSeeAllConfigValue(g_szMachineName) WScript.Echo tempReturn case RESTORE tempReturn = WmiRestore(objArgs(1), g_szMachineName) helpBit = 1 WScript.Echo "hResult: " & tempReturn end select end if end if ' display help content or result if helpBit = 0 then displayHelp() end if ' release object Set objArgs = Nothing