<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.technet.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Robert Larson : Virtual Server COM API</title><link>http://blogs.technet.com/roblarson/archive/tags/Virtual+Server+COM+API/default.aspx</link><description>Tags: Virtual Server COM API</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Virtual Server COM API - VMTask usage and error handling in VBscript</title><link>http://blogs.technet.com/roblarson/archive/2007/06/28/virtual-server-com-api-vmtask-usage-and-error-handling-in-vbscript.aspx</link><pubDate>Thu, 28 Jun 2007 21:15:00 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:1387586</guid><dc:creator>RobLarso</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.technet.com/roblarson/comments/1387586.aspx</comments><wfw:commentRss>http://blogs.technet.com/roblarson/commentrss.aspx?PostID=1387586</wfw:commentRss><description>&lt;P class=MsoNormal style="MARGIN: 6pt 0in 12pt"&gt;&lt;FONT face="Times New Roman" size=3&gt;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. &lt;/FONT&gt;&lt;FONT size=3&gt;&lt;FONT face="Times New Roman"&gt;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 &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;&lt;FONT color=#000000&gt;WaitForCompletion&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face="Times New Roman"&gt; to wait for the asynchronous task to complete.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=CodeBlock style="MARGIN: 0in -59.75pt 3pt 0.25in"&gt;&lt;FONT face="Courier New"&gt;Dim objTask&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=CodeBlock style="MARGIN: 0in -59.75pt 3pt 0.25in"&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=CodeBlock style="MARGIN: 0in -59.75pt 3pt 0.25in"&gt;&lt;FONT face="Courier New"&gt;Set objTask = someinterface.method()&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=CodeBlock style="MARGIN: 0in -59.75pt 3pt 0.25in"&gt;&lt;FONT face="Courier New"&gt;objTask.WaitforCompletion(10000)&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=CodeBlock style="MARGIN: 0in -59.75pt 3pt 0.25in"&gt;&lt;o:p&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=CodeBlock style="MARGIN: 0in -59.75pt 3pt 0.25in"&gt;&lt;FONT face="Courier New"&gt;If objTask.IsComplete Then&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=CodeBlock style="MARGIN: 0in -59.75pt 3pt 0.25in"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;WScript.Echo "Task Successful"&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=CodeBlock style="MARGIN: 0in -59.75pt 3pt 0.25in"&gt;&lt;FONT face="Courier New"&gt;Else&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=CodeBlock style="MARGIN: 0in -59.75pt 3pt 0.25in"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;WScript.Echo "Task still not Complete"&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=CodeBlock style="MARGIN: 0in -59.75pt 3pt 0.25in"&gt;&lt;FONT face="Courier New"&gt;End If&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 6pt 0in 12pt"&gt;&lt;FONT size=3&gt;&lt;FONT face="Times New Roman"&gt;The example code demonstrates the dimension and assignment of the &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;objTask&lt;/SPAN&gt;&lt;FONT face="Times New Roman"&gt; variable the task from the &lt;FONT face="Courier New"&gt;someinterface.method()&lt;/FONT&gt;method. The &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;WaitForCompletion&lt;/SPAN&gt;&lt;FONT face="Times New Roman"&gt; method is then called with a value of 10000 milliseconds. The script will wait for one of two actions to happen: &lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 6pt 0in 12pt"&gt;&lt;FONT size=3&gt;&lt;FONT face="Times New Roman"&gt;1. The task completes within 10 seconds and the method returns early&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 6pt 0in 12pt"&gt;&lt;FONT size=3&gt;&lt;FONT face="Times New Roman"&gt;2. The task does not complete within 10 seconds and the method exits. &lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 6pt 0in 12pt"&gt;&lt;FONT size=3&gt;&lt;FONT face="Times New Roman"&gt;In&amp;nbsp;the first case,&amp;nbsp;the &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;IsComplete&lt;/SPAN&gt;&lt;FONT face="Times New Roman"&gt; method would return a True, in the second case&amp;nbsp;the &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;IsComplete&lt;/SPAN&gt;&lt;FONT face="Times New Roman"&gt; method would return a False. &lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 6pt 0in 12pt"&gt;&lt;FONT face="Times New Roman"&gt;&lt;FONT size=3&gt;But you have tried this in your script and you get an error "object reference not found" on the &lt;FONT face="Courier New"&gt;Set objTask = someinterface.method() &lt;/FONT&gt;&lt;FONT face="times new roman,times"&gt;line&lt;/FONT&gt;. This is probably happening because there was an error in the &lt;FONT face="Courier New"&gt;someinterface.method()&lt;FONT face="times new roman,times"&gt;call and the VMTask reference was never returned and assigned to &lt;FONT face="courier new,courier"&gt;objTask&lt;/FONT&gt;. This results in an error in the script everytime you attempt to use the &lt;FONT face="courier new,courier"&gt;objTask&lt;/FONT&gt; 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 &lt;FONT face="courier new,courier"&gt;objTask&lt;/FONT&gt; object reference contains a value before you attempt to use it. You accomplish this using the "Nothing" keyword in an object reference check.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=CodeBlock style="MARGIN: 0in -59.75pt 3pt 0.25in"&gt;&lt;FONT face="Courier New"&gt;Dim objTask&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=CodeBlock style="MARGIN: 0in -59.75pt 3pt 0.25in"&gt;&lt;o:p&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=CodeBlock style="MARGIN: 0in -59.75pt 3pt 0.25in"&gt;&lt;o:p&gt;&lt;FONT face="Courier New" color=#0000ff&gt;On Error Resume Next&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=CodeBlock style="MARGIN: 0in -59.75pt 3pt 0.25in"&gt;&lt;FONT face="Courier New"&gt;Set objTask = someinterface.method()&lt;/FONT&gt;&lt;/P&gt;&lt;FONT face="Courier New"&gt;
&lt;P class=MsoNormal style="MARGIN: 6pt 0in 12pt" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 6pt 0in 12pt"&gt;&amp;nbsp;&amp;nbsp; If &lt;FONT color=#0000ff&gt;objTask Is Nothing&lt;/FONT&gt; Then&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; WScript.Echo "Error:" &amp;amp; Err.Description&lt;BR&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; Err.Clear&lt;BR&gt;&amp;nbsp;&amp;nbsp; Else&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; objTask.WaitforCompletion(10000)&lt;/P&gt;
&lt;P class=CodeBlock style="MARGIN: 0in -59.75pt 3pt 0.25in"&gt;&lt;o:p&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=CodeBlock style="MARGIN: 0in -59.75pt 3pt 0.25in"&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&amp;nbsp; If objTask.IsComplete Then&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=CodeBlock style="MARGIN: 0in -59.75pt 3pt 0.25in"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;WScript.Echo "Task Successful"&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=CodeBlock style="MARGIN: 0in -59.75pt 3pt 0.25in"&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&amp;nbsp; Else&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=CodeBlock style="MARGIN: 0in -59.75pt 3pt 0.25in"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;WScript.Echo "Task still not Complete"&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=CodeBlock style="MARGIN: 0in -59.75pt 3pt 0.25in"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Wscript.Echo "Task Percent Remaining = " &amp;amp; &lt;FONT color=#0000ff&gt;(100 - objTask.PercentCompleted)&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=CodeBlock style="MARGIN: 0in -59.75pt 3pt 0.25in"&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&amp;nbsp; End If&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 6pt 0in 12pt"&gt;&amp;nbsp;&amp;nbsp; End If&lt;/P&gt;&lt;/FONT&gt;
&lt;P class=MsoNormal style="MARGIN: 6pt 0in 12pt"&gt;&amp;nbsp;I also added the feature to tell you if the task is not completed, what percentage is left to complete.&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 6pt 0in 12pt"&gt;Well this is an intro to Task usage in Virtual Server scripts....hope you find it useful.&lt;/P&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=1387586" width="1" height="1"&gt;</description><category domain="http://blogs.technet.com/roblarson/archive/tags/Virtual+Server+COM+API/default.aspx">Virtual Server COM API</category></item></channel></rss>