Welcome to TechNet Blogs Sign in | Join | Help

Robert Larson

Thoughts and information on Virtualization and other topics

News

  • Virtual Server 2005 R2 Resource Kit Now on the Shelves!


    Welcome to my blog.

    I am an Architect with MS Consulting Services specializing in Infrastructure Optimization and Virtualization. I am also the co-author of the new Virtual Server 2005 R2 Resource Kit.

    In this blog I will talk about various technology subjects that are my passion and also drive me crazy sometimes.

    The information in this weblog is provided "AS IS" with no warranties, and confers no rights. This weblog does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my opinion. Inappropriate comments will be deleted at the authors discretion. All code samples are provided "AS IS" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.
Virtual Server COM API - VMTask usage and error handling in VBscript

Virtual Server tasks are used to track and obtain status of asynchronous actions of COM methods. There are many Virtual Server COM API methods that return a VMTask interface so that the progress of the task can be tracked. The simplest usage of the VMTask interface is to define a variable to hold a VMTask object reference and assign the VMTask return value from a method. Once you have that reference you can then call an VMTask method like WaitForCompletion to wait for the asynchronous task to complete.

Dim objTask

 

Set objTask = someinterface.method()

objTask.WaitforCompletion(10000)

 

If objTask.IsComplete Then

   WScript.Echo "Task Successful"

Else

   WScript.Echo "Task still not Complete"

End If

The example code demonstrates the dimension and assignment of the objTask variable the task from the someinterface.method()method. The WaitForCompletion method is then called with a value of 10000 milliseconds. The script will wait for one of two actions to happen:

1. The task completes within 10 seconds and the method returns early

2. The task does not complete within 10 seconds and the method exits.

In the first case, the IsComplete method would return a True, in the second case the IsComplete method would return a False.

But you have tried this in your script and you get an error "object reference not found" on the Set objTask = someinterface.method() line. This is probably happening because there was an error in the someinterface.method()call and the VMTask reference was never returned and assigned to objTask. This results in an error in the script everytime you attempt to use the objTask methods or look at its properties. To solve this problem, modify the script slightly by adding Error handling using On Error Resume Next and checking to be sure that the objTask object reference contains a value before you attempt to use it. You accomplish this using the "Nothing" keyword in an object reference check.

Dim objTask

 

On Error Resume Next

Set objTask = someinterface.method()

 

   If objTask Is Nothing Then
      WScript.Echo "Error:" & Err.Description
      Err.Clear
   Else 
      objTask.WaitforCompletion(10000)

 

   If objTask.IsComplete Then

      WScript.Echo "Task Successful"

   Else

      WScript.Echo "Task still not Complete"

      Wscript.Echo "Task Percent Remaining = " & (100 - objTask.PercentCompleted)

   End If

   End If

 I also added the feature to tell you if the task is not completed, what percentage is left to complete.

Well this is an intro to Task usage in Virtual Server scripts....hope you find it useful.

Posted: Thursday, June 28, 2007 1:15 PM by RobLarso

Comments

tonyso said:

Robert Larson , co-author of the Virtual Server 2005 R2 Resource Kit, has joined the blogosphere. Gate

# July 2, 2007 12:30 PM
Anonymous comments are disabled
Page view tracker