 




<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The VBScript Network and Systems Administrator&#039;s Cafe &#187; Subroutines</title>
	<atom:link href="http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/tag/subroutines/feed/" rel="self" type="application/rss+xml" />
	<link>http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator</link>
	<description></description>
	<lastBuildDate>Tue, 11 Oct 2011 18:36:53 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>VBScript Statements: Explanation of the Sub Statement</title>
		<link>http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/vbscript-statements-explanation-of-the-sub-statement/</link>
		<comments>http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/vbscript-statements-explanation-of-the-sub-statement/#comments</comments>
		<pubDate>Fri, 01 Aug 2008 02:32:56 +0000</pubDate>
		<dc:creator>Jerry Lees</dc:creator>
				<category><![CDATA[Sub]]></category>
		<category><![CDATA[Sub Statement]]></category>
		<category><![CDATA[Subroutines]]></category>
		<category><![CDATA[VBScript]]></category>
		<category><![CDATA[VBScript Statements]]></category>

		<guid isPermaLink="false">http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/vbscript-statements-explanation-of-the-sub-statement/</guid>
		<description><![CDATA[The VBScript statement Sub allows you to create a section of code that can be reused over and over, called a subroutine. It also allows you to name this section of code and refer to it by name, rather than typing it in multiple times. Subroutines can optionally have parameters associated with them, but they [...]]]></description>
				<content:encoded><![CDATA[<p>The VBScript statement <em>Sub</em> allows you to create a section of code that can be reused over and over, called a subroutine. It also allows you to name this section of code and refer to it by name, rather than typing it in multiple times.</p>
<p>Subroutines can optionally have parameters associated with them, but they must complete all their work in the subroutine because they can not return any values back to the portion of the program that called them.</p>
<p>Sub routines start with a declaration of the name and parameters and end with a &#8220;End Sub&#8221; statement. An example subroutine would look like so:</p>
<p><font color="#0000ff">Sub AddNumbers (x, y)<br />
    Wscript.Echo (x+y)<br />
End Sub</font></p>
<p>To call this subroutine from your script you would simply place the following code elsewhere in your script:</p>
<p><font color="#0000ff">AddNumbers 2, 4</font></p>
<p>(Notice that parenthesis were not used when calling the subroutine.)</p>
<!-- wpms-network-global-inserts -->]]></content:encoded>
			<wfw:commentRss>http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/vbscript-statements-explanation-of-the-sub-statement/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rebooting or Shuttingdown a Server Remotely with VBScript</title>
		<link>http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/rebooting-or-shuttingdown-a-server-remotely-with-vbscript/</link>
		<comments>http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/rebooting-or-shuttingdown-a-server-remotely-with-vbscript/#comments</comments>
		<pubDate>Thu, 27 Mar 2008 15:03:13 +0000</pubDate>
		<dc:creator>Jerry Lees</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Functions]]></category>
		<category><![CDATA[Subroutines]]></category>
		<category><![CDATA[VBScript]]></category>

		<guid isPermaLink="false">http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/rebooting-or-shuttingdown-a-server-remotely-with-vbscript/</guid>
		<description><![CDATA[In this installment I&#8217;d like to share with you a scriplet I developed out of frustration with not having found this information anywhere else.  On occasion you need to reboot a server, sure you can do it by logging in or by the shutdown command on windows XP. Heck you can even do it with [...]]]></description>
				<content:encoded><![CDATA[<p>In this installment I&#8217;d like to share with you a scriplet I developed out of frustration with not having found this information anywhere else.</p>
<p> On occasion you need to reboot a server, sure you can do it by logging in or by the shutdown command on windows XP. Heck you can even do it with the Windows 32 API calls in C++! However, sometimes you don&#8217;t want to be up and awake when you need to reboot a server. Plus, you might want to have some sort of logic behind it like instead of rebooting every night blindly you might want to check to see is a process is running (like a windows service) and stop it in a controlled manner before rebooting the system&#8211; we&#8217;ve all go those one off servers that have some weird application that needs fiddling with before you reboot, or atleast the lore at the office says you need to do something first. Right?</p>
<p>Well, I recently had such a situation and became frustrated that I couldn&#8217;t reboot a server cleanly with in a script without first executing from within the script, as a child process, a batch file to do the reboot for me. Sure it works&#8230; and the main reason I didn&#8217;t like it was it wasn&#8217;t as sophisticated as I&#8217;d like. But another problem is that I would have no real way of knowing what the outcome of the reboot process&#8211; in other words, I wouldn&#8217;t be able to handle the fact that the reboot didn&#8217;t occur or the batch file had an error because windows would have returned only that the command prompt executed and the batch was ran.</p>
<p>In two previous posts, I&#8217;ve provided information on Functions, WMI, and Methods to lead up to this topic. If you missed them you might go back and read the quickly so you&#8217;ll be up to speed. The posts are:</p>
<p><a rel="bookmark" href="http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/working-with-subroutines-and-functions-in-vbscript/" title="Permanent Link to Working with Subroutines and Functions in VBScript"><font color="#92a3b1">Working with Subroutines and Functions in VBScript</font></a><br />
<a rel="bookmark" href="http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/using-windows-management-interface-wmi-to-make-your-life-easy/" title="Permanent Link to Using Windows Management Interface (WMI) to Make Your Life Easy."><font color="#92a3b1">Using Windows Management Interface (WMI) to Make Your Life Easy.</font></a></p>
<p> In the following script, I create a function called <font color="#ff0000">Reboot </font><font color="#000000">that uses WMI to create a connection to a server name you pass to it (<font color="#0000ff"><strong><em>Note: </em></strong><font color="#000000">to protect you I have set the call to reboot() in the script to use &#8220;.&#8221;, which is the local machine. Change &#8220;.&#8221; to &#8220;ServerName&#8221; to reboot a specific remote machine. In other words&#8230; run this code unchanged and your desktop <strong><em>will</em></strong> reboot in the next 30 seconds.)</font></font></font></p>
<p><font color="#0000ff">Reboot(&#8220;.&#8221;)</font></p>
<p><font color="#0000ff">Function Reboot(StrSrv)<br />
   Set objWMIService = GetObject(_<br />
   &#8221;winmgmts:{impersonationLevel=impersonate,authenticationLevel=Pkt,(Shutdown)}!\\&#8221;_<br />
   </font><font color="#0000ff">&amp; StrSrv &amp; &#8220;\root\cimv2&#8243;)<br />
   Set colItems = objWMIService.ExecQuery(&#8220;Select * from Win32_OperatingSystem&#8221;,,48)<br />
   For Each objItem In colItems<br />
        WScript.Echo objitem.Reboot<br />
   Next<br />
End Function</font></p>
<p><font color="#0000ff"><font color="#000000">This is a really useful scriptl function, when combined with other pieces of code to gather performance monitor counters (later I&#8217;ll post some code examples on this) you could reboot a server if the non-paged pool memory reaches a point where the system is probably going to lock up due to a leaky app or driver&#8230; this was my issue I solved with vbscript. </font></font></p>
<p><font color="#0000ff"><font color="#000000"><font color="#0000ff"><font color="#000000">As always, this code works perfectly. However, sometimes the formatting of the blog breaks the code if you copy and paste it into your editor. So, if you’d like to not type or troubleshoot any syntax errors due to the copy and paste problems– I’ve provided the code for download, plus example output files  from my final tests for you. You’ll find the code and other files available for download from my website’s (<a href="http://www.websystemsadministration.com/">www.websystemsadministration.com</a>) File Depot under the <font color="#0000ff"><em>ITKE Blog Scripts</em></font> category.<font color="#0000ff"> </font><font color="#0000ff"><font color="#0000ff"><font color="#000000">Enjoy and happy scripting!</font></font></font></font></font></p>
<p></font></font><font color="#0000ff"><font color="#ff0000">Extra credit:</font> <font color="#000000">Can you figure out what the following part of the code does?</font><font color="#0000ff">{impersonationLevel=impersonate,authenticationLevel=Pkt,(Shutdown)}</font></font></p>
<!-- wpms-network-global-inserts -->]]></content:encoded>
			<wfw:commentRss>http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/rebooting-or-shuttingdown-a-server-remotely-with-vbscript/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Explanation: Working with Subroutines and Functions in VBScript</title>
		<link>http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/explanation-working-with-subroutines-and-functions-in-vbscript/</link>
		<comments>http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/explanation-working-with-subroutines-and-functions-in-vbscript/#comments</comments>
		<pubDate>Thu, 20 Mar 2008 18:26:40 +0000</pubDate>
		<dc:creator>Jerry Lees</dc:creator>
				<category><![CDATA[DataManagement]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Functions]]></category>
		<category><![CDATA[Subroutines]]></category>
		<category><![CDATA[VBScript]]></category>

		<guid isPermaLink="false">http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/explaination-working-with-subroutines-and-functions-in-vbscript/</guid>
		<description><![CDATA[I just realized in my haste to get out a piece of code celebrating the XBOX 360 contest, I negected to give a proper answer for our discussion on subroutines and functions in a vbscript. In this installment, we&#8217;ll explain how to build a function and a subroutine a bit better. First, remember functions and [...]]]></description>
				<content:encoded><![CDATA[<p>I just realized in my haste to get out a piece of code celebrating the XBOX 360 contest, I negected to give a proper answer for our discussion on subroutines and functions in a vbscript.</p>
<p>In this installment, we&#8217;ll explain how to build a function and a subroutine a bit better. First, remember functions and subroutines are basically smaller pieces of your script &#8212; they essentially are the work horse of your script.</p>
<p>They both get <em>passed</em> information when they are called and preform their work according to what the information they have been given. These values passed can literally be anything you need to make the subroutine or function work but are generally only what the function needs to preform it&#8217;s work. You can reference the whole blog entry <a href="http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/working-with-subroutines-and-functions-in-vbscript/">here</a>.</p>
<p>Basically, you can build a subroutine like so:</p>
<p><font color="#0000ff">Sub MySub(myvariable)</font></p>
<p><font color="#0000ff">&#8216;&#8230; your code here</font></p>
<p><font color="#0000ff">End Sub</font></p>
<p><font color="#000000">You would call this subroutine like so:</font></p>
<p><font color="#0000ff">MySub XVariable<br />
</font>-Or-<br />
<font color="#0000ff">MySub 10</font><br />
-Or-<br />
<font color="#0000ff">MySub &#8220;This Value&#8221;</font></p>
<p>Notice that I didn&#8217;t use Parenthesis, you don&#8217;t when calling a subroutine. The value you pass the subroutine can be variable (XVariable), a literal number (10), a string (&#8220;This Value&#8221;), or any other specific item you need to pass into the subroutine&#8211; even an array, providing you&#8217;re expecting the variable type!</p>
<p>Functions are the exact same as subroutines except in the label when you create them and in that they can return a value.</p>
<p>To create a function you:</p>
<p><font color="#0000ff">Function MyFunction</font></p>
<p><font color="#0000ff">&#8216;&#8230; your code here</font></p>
<p><font color="#ff0000">MyFunction = X</font></p>
<p><font color="#0000ff">End Function</font></p>
<p>To call a function you use parenthesis, unlike a subroutine, like so:</p>
<p><font color="#0000ff">X = MyFunction (XVariable)<br />
</font>-Or-<br />
<font color="#0000ff">X = MyFunction (10)</font><br />
-Or-<br />
<font color="#0000ff">X = MyFunction (&#8220;This Value&#8221;)</font></p>
<p><font color="#000000">Notice the line in red? This is the line of the function that does the magic of returning a value! It essentially works on the premise that the function name is a variable in the function that is always there. at the end of the function&#8217;s work the value is passed back to the calling routine as the return value&#8211; and it does this automatically. </font></p>
<p><font color="#000000">Also notice how I changed the line to call a function? I placed a variable, X, in front of the function and set X equal the return of the function. You can also use a function in comparisons like so:</font></p>
<p><font color="#0000ff">If Myfunction(10) = 1 then<br />
&#8216;your code here<br />
end if</font></p>
<p><font color="#000000">So, we&#8217;ve covered the creation and calling of a subroutine, the creation and calling of a function, how to assign a value to the return of a function, and how to use the return from the function.</font></p>
<p>From this point, you can use your imagination for when and where to use functions and subroutines!</p>
<!-- wpms-network-global-inserts -->]]></content:encoded>
			<wfw:commentRss>http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/explanation-working-with-subroutines-and-functions-in-vbscript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Working with Subroutines and Functions in VBScript</title>
		<link>http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/working-with-subroutines-and-functions-in-vbscript/</link>
		<comments>http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/working-with-subroutines-and-functions-in-vbscript/#comments</comments>
		<pubDate>Mon, 10 Mar 2008 15:33:36 +0000</pubDate>
		<dc:creator>Jerry Lees</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Functions]]></category>
		<category><![CDATA[Subroutines]]></category>
		<category><![CDATA[VBScript]]></category>

		<guid isPermaLink="false">http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/working-with-subroutines-and-functions-in-vbscript/</guid>
		<description><![CDATA[In this installment, we&#8217;ll discuss the advantages of using Subroutines and Functions in your script rather than using a all-in-one script that runs from start to finish. I&#8217;ll only give a breif over veiw of subroutines and function, but more information on them can be found in the VBScript User&#8217;s guide and Language reference. First, Functions [...]]]></description>
				<content:encoded><![CDATA[<p>In this installment, we&#8217;ll discuss the advantages of using Subroutines and Functions in your script rather than using a all-in-one script that runs from start to finish. I&#8217;ll only give a breif over veiw of subroutines and function, but more information on them can be found in the <a href="http://msdn2.microsoft.com/en-us/library/t0aew7h6(VS.85).aspx">VBScript User&#8217;s guide and Language reference</a>.</p>
<p>First, Functions and subroutines are basically smaller pieces of your script. They are generally a part that completed a specific task in the script and are very helpful in accomplishing the following:</p>
<p>      Creating reusable code to use in other scripts without having to re-write the code<br />
      Making your code more readable<br />
      Using the same set of code multiple times in the same script without a loop of some type.<br />
      Helpful in debugging a script and disabling a section of code<br />
      Helpful in organising the logic of the script into smaller bite sized pieces</p>
<p>They essentially are the work horse of your script. In General, I try to keep all the logic of a script in a Function or a subroutine&#8211; that way If I have to make modifications I&#8217;m only going to at worst break a small (but possibly important) piece of the script. Remember, Subroutines and functions can call other subroutines and functions&#8211; so they are not limited to just being used within your main script&#8211; Infact, a single function or subroutine can call itself! This is outside the scope of this posting, but is called a recursive funtion or subroutine.</p>
<p>They both get <em>passed</em> information when they are called and  preform their work according to what the information they have been given. These values passed can literally be anything you need to make the subroutine or function work. These values are generally only what the function needs to preform it&#8217;s work. For example:</p>
<p>Say you need to calculate the price you pay for an item at the store, so you can have exact change in cash. Think about what information you need. You just need the price of the item and any sales tax on the item, you don&#8217;t need anything else like where the store is or who the cashier is. That&#8217;s essentially what a subroutine is!</p>
<p>Now, Functions and subroutines are VERY similar. In fact, there is only one major difference in the two of them. They both offer a way to better organize your code into bite sized chunks, create re-usable sections that are easier to manage and modify, and they both preform a very specific task.</p>
<p>The only difference is that a subroutine does it&#8217;s work and deals with it&#8217;s result inside itself without communicating the end result back to the routine that called it to run.  So it&#8217;s kind of a loner&#8230; just does it&#8217;s work and doesn&#8217;t bother anyone else with the details or the result. This is useful for file or screen I/O where you don&#8217;t need any information <em>return</em>ed to you. Basically, it&#8217;s a script all in itself. In our earlier example the subroutine would need to print out or do something with the values calculated and no other area of the program would be aware of where the result.</p>
<p>A function on the other hand will communicate back to the calling routine some value&#8230; possibly the result of some calculation, like our total price of the item, or an error condition for the calling routine to process and deal with in a manner that is fitting for the situation.</p>
<p>In my example below I provide you with both examples of a subroutine and a function to add two numbers together. Notice that when I call the subroutine in vbscript I do not use parenthesis around the values I pass to the routine, but with a function I do use them. This is just simply the way it is done in VBScript and will result in an error if your do it the other way around.</p>
<p><font color="#0000ff">Option Explicit<br />
Dim ReturnValue</font></p>
<p><font color="#0000ff">TestSub 2,2 &#8216; subroutine call– notice no ( ) around the two values!?!?!<br />
ReturnValue = TestFunction (2,2) &#8216;function call– notice the ( ) around the two values… plus some thing else!<br />
WScript.Echo &#8220;Function returned: &#8221; &amp; ReturnValue</font></p>
<p><font color="#0000ff">&#8216; end of main code</font></p>
<p><font color="#0000ff">Function TestFunction (Value1, Value2)<br />
&#8216; Function to add two numbers and RETURN the value to the calling routine.</font></p>
<p><font color="#0000ff">Dim Sum<br />
 Sum = value1 + Value2<br />
 WScript.Echo (&#8220;Functions: All your work is done here and used here, but can be sent back to the main script.&#8221;)<br />
 WScript.Echo Value1 &amp; &#8221; + &#8221; &amp; Value2 &amp; &#8221; = &#8221; &amp; Sum</font></p>
<p><font color="#0000ff">TestFunction = Sum</font></p>
<p><font color="#0000ff">End Function<br />
Sub TestSub (Value1, Value2)<br />
&#8216; Subroutine to add two values together… notice all the work is done and used here?</font></p>
<p><font color="#0000ff">Dim Sum<br />
 Sum = value1 + Value2<br />
 WScript.Echo (&#8220;Subroutines: All your work is done here and used here.&#8221;)<br />
 WScript.Echo Value1 &amp; &#8221; + &#8221; &amp; Value2 &amp; &#8221; = &#8221; &amp; Sum</font></p>
<p><font color="#0000ff">end Sub</font></p>
<p>Play with this code&#8230; see if you can figure it out. In a few days I will explain it in a bit of better detail. Take note of how I build the function, subroutine, and see if you can figure out the line that does the magic of returning the value from the function. </p>
<p><font color="#ff0000">Extra Credit:</font> Did you notice the line that had <strong>option explicit</strong> and the lines that have <strong>Dim</strong> in them? What do they do? Post a comment and I&#8217;ll let you know in a few days.<br />
 </p>
<!-- wpms-network-global-inserts -->]]></content:encoded>
			<wfw:commentRss>http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/working-with-subroutines-and-functions-in-vbscript/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
