[MSH] Simple demo of "Type 2" MSH/Active Scripting Interop - native filters from VBScript

Discussion in 'Scripting' started by Alex K. Angelopoulos [MVP], Apr 12, 2006.

  1. Thanks to Jouko and Bruce for spotting the problem I had yesterday with COM
    interop. Without them, this post would not be possible. :)

    I posted a demo showing how to load Active Script functions into the MSH
    shell recently (see the thread "[MSH]Info: Reusing Active Script directly
    from MSH"). This makes it easy to exploit existing well-written code
    snippets from MSH. However, there's another kind of interoperability: making
    Active Scripts work in the MSH pipeline directly.

    From outside a script, this makes a script written in VBScript/JScript and
    even in Perl/Python/Rexx/etc look like a standard MSH script that can be
    called by name and treated as a command. From inside the MSH-hosted Active
    Script, it would look like you are writing a MSH script using <Insert
    Favorite Scripting Language Here>. People have asked about this kind of
    functionality before, and it can be useful in some situations. The
    underlying host would be analogous to the Windows Script Host. Besides
    making it easy to hack up existing scripts to work on MSH objects and then
    pass objects back to MSH, it would provide a "glide slope" for WSH -> MSH.

    There are reasons why it would be ugly and difficult to do a direct
    look-alike port of WSH to MSH, but most of those have to do with the fact
    that most WSH-era scripts are not designed to be good citizens in an
    advanced interop situation. Here's how a MSH Active Script Host would work.

    (1) You have two components, a host in MSH and a script that you run.

    (2) Scripts are written in any AS language, and besides global code - which
    is executed automatically at load - it includes three functions that are
    directly analogous to a MSH scripts begin/process/end blocks. In VBScript,
    the template looks like this (note the square brackets around End that
    permit it be used as a name in VBScript):
    Function Begin()
    End Function
    Function Process(mshobject)
    End Function
    Function [End]()
    End Function

    (3) The MSH host is simply a MSH script that creates a ScriptControl
    instance, sets the language, and then loads a file into the control. It then
    invokes the Begin procedure in the script, invokes Process with each
    pipeline member, and then invokes End when the pipeline is shutting down.

    The attached files (Invoke-ActiveScript.msh1 and Demo-VBScript.txt) are part
    of a quick demo. Save Invoke-ActiveScript.msh1 as Invoke-ActiveScript.msh
    somewhere in your MSH path if you want to call it by name instead of
    specifying a qualified path. Save Demo-VBScript.txt anywhere; you need to
    supply its full path, but the extension doesn't matter. You can then invoke
    it like this:

    Invoke-ActiveScript.msh -Path Demo-VBScript.txt -Engine VBScript

    If you pipe some objects into it, the script will display the typename for
    each one.

    + First thing, the MsgBox use in the demo is done so you can really see
    what's happening item-by-item. You never want to do this in a pipeline
    script that isn't a demo; MsgBox and Alert are two of the reasons why
    reusing off-the-shelf scripts is usually almost impossible.

    + We could pass unconsumed arguments from the MSH script into the hosted AS
    script by setting up an argument array for the Begin() block, so it is

    + We should be able to add the MshHost to the script control using its
    AddObject method. This would let us directly emit objects out to the
    Alex K. Angelopoulos [MVP], Apr 12, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.