如何使用script來自動建立系統還原點與執行系統還原?
如果我想以執行script的方式(非透過UI的方式)來建立XP或是Windows 7的系統還原點,或是透過script的方式執行系統還原,該如何做?請參考以下的範例(分別存成不同的VBS檔案來執行)
一、第一個script,建立系統還原點
Set IRP = getobject("winmgmts:\.rootdefault:Systemrestore")
MYRP = IRP.createrestorepoint ("Created By IT", 0, 100)
二、第二個script,列出有多少個還原點
Set dtmConvertedDate = CreateObject("WbemScripting.SWbemDateTime")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\" & strComputer & "rootdefault")
Set colItems = objWMIService.ExecQuery("Select * from SystemRestore")
If colItems.Count = 0 Then
WScript.Echo "No restore point in system."
Else
For Each objItem in colItems
Wscript.Echo "Name: " & objItem.Description
Wscript.Echo "Number: " & objItem.SequenceNumber
Select Case objItem.RestorePointType
Case 0 strRestoreType = "Application installation"
Case 1 strRestoreType = "Application uninstall"
Case 6 strRestoreType = "Restore"
Case 7 strRestoreType = "Checkpoint"
Case 10 strRestoreType = "Device drive installation"
Case 11 strRestoreType = "First run"
Case 12 strRestoreType = "Modify settings"
Case 13 strRestoreType = "Cancelled operation"
Case 14 strRestoreType = "Backup recovery"
Case Else strRestoreType = "Unknown"
End Select
Wscript.Echo "Restore Point Type: " & strRestoreType
dtmConvertedDate.Value = objItem.CreationTime
dtmCreationTime = dtmConvertedDate.GetVarDate
Wscript.Echo "Time: " & dtmCreationTime
Next
End If
三、第三個script,執行系統還原到指定的還原點。以此script為例,是還原到第10個備份的還原點。
Const RESTORE_POINT =10
strComputer = "."
Set objWMIService = GetObject("winmgmts:\" & strComputer & "rootdefault")
Set objItem = objWMIService.Get("SystemRestore")
errResults = objItem.Restore(RESTORE_POINT)
Wscript.Echo errResults
補充說明:
- 假設您要執行系統還原點備份,只要執行第一個script即可。
- 假設您要執行系統還原至先前的時間點,您需要執行第二個和第三個script。第二個script是列出此機器上有多少備份還原點,第三個script執行後,才是還原到指定的還原點。在第三個script執行後重開機,才會開時系統還原。