{"id":1498,"date":"2009-05-28T07:00:39","date_gmt":"2009-05-28T15:00:39","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/2009\/05\/28\/vbscript-includes\/"},"modified":"2009-05-28T07:00:39","modified_gmt":"2009-05-28T15:00:39","slug":"vbscript-includes","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2009\/05\/28\/vbscript-includes\/","title":{"rendered":"VBScript Includes"},"content":{"rendered":"<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2009\/05\/librarian.png\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; margin: 0px 5px 5px 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" border=\"0\" alt=\"librarian\" align=\"left\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2009\/05\/librarian-thumb.png\" width=\"95\" height=\"90\"><\/a> I&#8217;ve been using VBScript for a very long time. One drawback it has always had as a scripting language is an INCLUDE statement.&nbsp; In other languages you can use something like INCLUDE <em>scriptfile<\/em> at the beginning of the script. This would load any functions and variables in the specified script file into your current script.&nbsp; In PowerShell we can accomplish this by dot sourcing scripts.<\/p>\n<div style=\"border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, 'Courier New', courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px\">\n<div style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\"><span style=\"color: #008000\">#Sample.ps1<\/span><\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">. c:\\scripts\\myfunctions.ps1<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">. \\\\file01\\admins\\teamfunctions.ps1<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">&nbsp;<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\"><span style=\"color: #008000\"># main code here but I can call any function that is defined<\/span><\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\"><span style=\"color: #008000\"># in myfunctions.ps1 or teamfunctions.ps1 just as if they<\/span><\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\"># were <span style=\"color: #0000ff\">in<\/span> this <span style=\"color: #0000ff\">script<\/span>.<\/pre>\n<\/div>\n<\/div>\n<p>I never knew of a way to do the same thing with VBScript until now. Sure, if you write a Windows Script File (WSF), that has the ability to include external scripts, but not a plain VBS script.<\/p>\n<p>Recently in the SAPIEN support forums someone asked about this topic in relationship to <a href=\"http:\/\/www.primalscript.com\" target=\"_blank\">PrimalScript<\/a>. The customer then posted a short example, which was found somewhere online. I tried it out and it seems to work. I took the sample code and tweaked it a bit to create a function. Here&#8217;s an example of how it might be used.<\/p>\n<div style=\"border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, 'Courier New', courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px\">\n<div style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\"><span style=\"color: #0000ff\">Function<\/span> LoadLibrary(sScriptFile)<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">    <span style=\"color: #0000ff\">Set<\/span> oFSO=CreateObject(<span style=\"color: #006080\">\"Scripting.FileSystemObject\"<\/span>)<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">    <span style=\"color: #0000ff\">If<\/span> oFSO.FileExists(sScriptFile) <span style=\"color: #0000ff\">Then<\/span><\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">        <span style=\"color: #0000ff\">Set<\/span> sFile=oFso.OpenTextFile(sScriptfile)<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">        sInput=sFile.ReadAll<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">        sFile.Close<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">        ExecuteGlobal sInput<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">    <span style=\"color: #0000ff\">Else<\/span><\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">        WScript.Echo <span style=\"color: #006080\">\"Failed to find \"<\/span> &amp; sScriptFile<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">    <span style=\"color: #0000ff\">End<\/span> <span style=\"color: #0000ff\">If<\/span><\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\"><span style=\"color: #0000ff\">End<\/span> <span style=\"color: #0000ff\">Function<\/span><\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">&nbsp;<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">LoadLibrary <span style=\"color: #006080\">\"c:\\scripts\\ScriptFunctionLibrary.vbs\"<\/span><\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">&nbsp;<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\"><span style=\"color: #008000\">'GetOS is a function in ScriptFunctionLibrary.vbs<\/span><\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">os=GetOS<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">WScript.Echo OS<\/pre>\n<\/div>\n<\/div>\n<p>The LoadLibrary function takes a script path as a parameter. Assuming the file exists, its contents are read into a variable. Now here&#8217;s the magic part. The ExecuteGlobal function. Here&#8217;s my description from <a href=\"http:\/\/www.sapienpress.com\/wshcore.asp\" target=\"_blank\">WSH and VBScript Core: TFM<\/a>.<\/p>\n<p><em><font color=\"#0000ff\">In VBScript, it is possible to have an ambiguous expression. For example b=x could be interpreted as assigning the value of variable x to variable b. Or it could be interpreted as a comparison test. Does b have the same value as x? In VBScript, Execute applies the former interpretation and Eval applies the latter. <\/font><\/em><\/p>\n<p><em><font color=\"#0000ff\">Execute and ExecuteGlobal essentially perform the same task. The significant difference is that any result from ExecuteGlobal is available to all namespaces or scopes within your script.<\/font><\/em> <\/p>\n<p>What happens is that ExecuteGlobal takes all of the defined functions, that&#8217;s all that is in ScriptFunctionLibrary.vbs and executes them <strong><em>globally<\/em><\/strong>, which has the effect of loading them into the current script scope. Yep, VBScript has scope just like PowerShell. Thus I can invoke any function in my current script that has been defined in my library script. If there were defined variables in the library script I could access them as well. I wrote the function to make it easier in case you needed to load multiple script files.<\/p>\n<p>Of course, none of this is without risk. You have to be aware of name and variable collisions. Do you really know what is in the contents of the library script you are loading? This function could easily be used to load malicious code. Signing your scripts would help mitigate this risk.<\/p>\n<\/p>\n<div style=\"padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px\" id=\"scid:fb3a1972-4489-4e52-abe7-25a00bb07fdf:7edcbd16-8609-453c-bb28-d1c1a2ba5bfc\" class=\"wlWriterSmartContent\">\n<p>Download a text version of my LoadLibrary function <a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2009\/05\/loadlibrary.txt\" target=\"_blank\">here.<\/a><\/p>\n<\/div>\n<p> Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been using VBScript for a very long time. One drawback it has always had as a scripting language is an INCLUDE statement.  In other languages you can use something like INCLUDE scriptfile at the beginning of the script. This would load any functions and variables in the specified script file into your current script.  In PowerShell we can accomplish this by dot sourcing scripts. I never knew of a way to do the same thing with VBScript until now.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[283,24],"tags":[508,282,145,57,996],"class_list":["post-1498","post","type-post","status-publish","format-standard","hentry","category-howto","category-vbscript","tag-executeglobal","tag-functions","tag-include","tag-scripting","tag-vbscript"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/1498","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=1498"}],"version-history":[{"count":1,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/1498\/revisions"}],"predecessor-version":[{"id":1542,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/1498\/revisions\/1542"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=1498"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=1498"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=1498"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}