Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Today I am going to go over a basic script for compacting a virtual hard disk. This script creates a virtual hard disk object from a string that points to the virtual hard disk file (in the script this information is provided as a command line parameter). The script then executes the 'compact' method - which returns a VMTask object. We can then use this VMTask object to monitor the progress of the compaction:
'Script Begins
On Error Resume Next
'Connect to Virtual Server and check for failure
Set virtualServer = CreateObject("VirtualServer.Application")
If Err.number <> 0 Then error("Failed to connect to Virtual Server")'Connect to virtual hard disk (provided as command line parameter) and check for failure
Set aVirtualHardDisk = virtualServer.GetHardDisk(WScript.Arguments(0))
If Err.number <> 0 Then error("Failed to connect to virtual hard disk")'Start compaction process and check for failure
Set compactionTask = aVirtualHardDisk.Compact
If Err.number <> 0 Then error("Failed to compact virtual hard disk")'Display task description
wscript.echo compactionTask.Description
wscript.echo'Loop waiting for task completion - and display status
while not compactionTask.isComplete
wscript.echo "Compaction is " & compactionTask.PercentCompleted & "% complete"
WScript.Sleep 2000
wendwscript.echo
wscript.echo "Compaction complete"'Pause before exiting
WScript.Sleep 5000
wscript.quit'Generic error handler
sub error(message)wscript.echo message
wscript.echo
wscript.echo Err.Description
WScript.Sleep 5000
wscript.quitend sub
'Script Ends
As there are a couple of areas where you could hit a problem - I have put in some basic error handling to explain why things fail. Also - it should be noted that while this script will request that the virtual hard disk is compacted - the actual compaction is done by Virtual Server. This means that if you close the command prompt window while the script is running - Virtual Server will continue to compact the virtual hard disk in the background.
Cheers,
Ben
Comments
- Anonymous
February 10, 2005
Will this scripting be added to future release of Virtual PC anytime soon? - Anonymous
February 10, 2005
To make this a little more user friendly, add something like this:
'Check for CScript
If Not IsCScript() Then
Wscript.Echo Wscript.ScriptName & " must be run with CScript."
Wscript.Quit 1
End If
'----------------------------------------+----------------------------------------
' IsCscript - Checks CScript vs. WScript
'
' Date Name Comment
' ------ ------- -----------------------------------------------------------------
' 020507 CGL Created
'----------------------------------------+----------------------------------------
Function IsCScript()
Dim objRegExp
Set objRegExp = New RegExp
objRegExp.IgnoreCase = true
objRegExp.Pattern = "cscript.exe$"
IsCScript = objRegExp.Test(WScript.FullName)
Set objRegExp = Nothing
End Function - Anonymous
February 11, 2005
Raymond -
We are aware of the customer demand for this - however I do not tend to talk about future releases on this blog.
Christopher -
Yes - this is a good idea, however I try to keep my scripts as short as possible when I am posting them to my blog.
Cheers,
Ben