Include other files in VBScript

Have you ever wanted to include another .vbs file with all your common functions in a script?

Many people I have met thought it wasn’t possible.

But it is actually pretty easy to do with “ExecuteGlobal”

to make it even easier, I have created a small function which you can put in your scripts and use a simple

Include(“C:\functions.vbs”)

I have made 2 small example scripts to show you how to use it:

Functions.vbs:

This file could be a shared file with all your common functions such as Log writer.

Include-Example.Vbs

Then everytime you create a script you include the function and make sure to include the other vbs files in the beggning of the script.

Now you have access to all the functions from the other scripts, from inside your new script!

and the next time the management tells you to change the Log Format to a different date/time format, so simular. You just have to change the function in the shared file, instead of editing all your scripts!

But beware not to corrupt the shared file, cause it will make all your scripts fail 😉

Merry x-mas!

By | 2010-12-17T14:36:03+00:00 December 17th, 2010|Scripting & Development|4 Comments

About the Author:

Jakob Gottlieb Svendsen

Twitter: @JakobGSvendsen

Jakob Gottlieb Svendsen is a Microsoft Cloud and Data Center Management MVP (http://mvp.microsoft.com/en-us/default.aspx), Working as Global Lead Developer, Senior Consultant and Trainer at CTGlobal, where he is one of the driving forces in keeping CTGlobal a System Center Gold Partner and member of the System Center Alliance.

Since he started at Coretech in 2007, he has focused on Scripting and Development, primarily developing tools, extensions and scripts for the System Center Suite. His main area is Automation (including OMS/Azure Automation, Service Management Automation, PowerShell and Orchestrator). Another area is Windows Azure Pack / Azure Stack, where he does implementation, development, workshops and presentations. He is a world-wide renowned voice in the Automation field.

He is passionately devoted to the community, to which he contributes by being a moderator at TechNet and sharing his knowledge at https://blog.ctglobalservices.com/jgs

  • Co-founder: PowerShell User Group Denmark
  • Speaker at MMS 2016, Minneapolis (www.mmsmoa.com)
  • SCU Europe 2014, 2015, 2016 (www.systemcenteruniverse.ch)
  • Microsoft TechEd North America 2014, Houston
  • NIC 2012,2013,2014,2015, Oslo (www.nic.com)
  • Microsoft CampusDays 2011, 2013, Copenhagen
  • Microsoft TechDays 2015, Sweden (www.techdays.se)
  • Microsoft Partner Event: New in SC2012 SP1
  • User group meetings (PSUG.DK , SCUG.DK/BE/NO, AZMUG + more)
  • Microsoft Certified Trainer.
  • Microsoft Scripting Guys Forum Moderator

Main working areas:

  • Automation (Azure Automation, SMA, SCO)
  • Windows Azure Pack / Azure Stack
  • System CenterVisual Studio Team Services / Team Foundation Server
  • Development:C#.Net, VB.NET, VBScript, PowerShell, Service Manager, OpsMgr, ConfigMgr
  • Orchestrator
  • Windows Azure Pack / Azure Stack

Training:

  • Azure Automation
  • Service Management Automation
  • System Center Orchestrator
  • PowerShell, VBScript, C#.Net, VB.Net
  • Windows Azure Pack / Azure Stack Development Workshops

4 Comments

  1. msenn February 24, 2011 at 12:54 - Reply

    Hi Jacob,
    why don’t you use build-in feature of .wsf files?
    There’s no problem using .vbs include files:

    include.vbs:
    Sub Test
    sText = “Huhu”
    End Sub

    sample.wsf:

    Call Test ‘ you can treat subs or functions from include.vbs as if they exist in sampe.wsf
    MsgBox sText

    ~msenn

  2. msenn February 24, 2011 at 12:55 - Reply

    Sorry, your php removed the Job and Script Tags!

  3. Etalon September 11, 2012 at 18:45 - Reply

    I have posted this on another forum but may be useful here as well :

    “Hi all,

    I know this is an old thread but I post my answer anyway so others can learn what I have learnt about VBS and WSF files by “trial and error” :

    So to have the same functionality as in other languages you can create one WSF file and include all of your VBS libs there, including the main program.

    Something like this :

    ‘ Here we call the main program
    MainProgram()

    In “Constants.vbs” collect all constants you want to use later and in the other VBS files define your functions. In your main program file “MainProgram.vbs” create a Sub called “MainProgram()” and write your program there.
    In this sub you can use all of the constants and functions defined in the other VBS files.

    For example :

    sub MainProgram()
    ‘ Local variables
    Dim strMessage, strSendTo, strSubject
    ‘ OpenFile is a function from FileFunctions.vbs
    strMessage = OpenFile(“C:Msgmessage.html”)
    strSendTo = “email.address@yourdomain.com”
    strSubject = “Daily report – ” & date
    ‘ SendMessage is a function from SendMail.vbs
    ‘ cFrom and cServer are constants from Constants.vbs
    SendMessage(cFrom, strSendTo, strSubject, strMessage, cServer)
    ‘ Logger is a function from LoggingFunctions.vbs
    Logger(“Daily report sent – ” & now())
    end sub

    Hope you get the idea and I could help some people write better VBS apps 🙂 “

  4. Slingshot Braves Cheat May 1, 2014 at 1:18 - Reply

    Thanks designed for sharing such a fastidious thinking, post is good, thats
    why i have read it fully

Leave A Comment