信息来源:脚本中心
配置和操作系统恢复
描述
此页包含设置与运行 Windows XP 系统恢复(本地和远程)的示例脚本。
支持平台
|
Windows XP |
是 |
|
Windows Server 2003 |
否 |
|
Windows 2000 |
否 |
|
Windows NT 4.0 |
否 |
|
Windows 98 |
否 |
脚本代码
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
'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
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.
'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
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
'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
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
'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
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
'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
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
'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
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
'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
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
'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
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
'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
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