.\Matthew Long

{An unsorted collection of thoughts}

Scripting Series – Interesting things you can do with VBScript and Powershell – Post 3, embedded scripts

Posted by Matthew on March 15, 2011

In the third article in this series, i’m going to discuss embedding scripts inside each other so that you can have a library of scripts deployed from a single script.  This can also be useful if you need to say use Powershell to create and execute a different kind of script (in my case, so we can use a VBScript to perform self deletion).  In addition, when we write out the embedded script we can take arguments for the current script and modify the embedded script to “remember” those runtime parameters. 

In powershell this is a fairly trivial task, as depending on the script language you wish to embed you can either use a script block or a Here-String.  A Here-String is a powershell construct you can use to declare a string where all formatting, special characters are ignored.  This includes carriage returns, so you can easily create multiline strings.  You can also use a variant to suspend string substitution.  The block below shows an example..


$Script = @"
'This is the first line of a 'String'' without any matching "quote` marks.
'This is the second line of the string with a         tab in it
option explicit
Dim someVar, otherVar
'Do some work here
WScript.Echo "Some output"
WScript.Quit

"@
$Script | Set-Content '.\VBScript.vbs'

Unfortunately wordpresses code block doesn’t recognise the here-string, but i assure you it works! Note that because tabs are captured inside the here-string, if you choose to indent the string, the tabs will be captured into the middle of your string.  Note that if you use speechmarks string substitution will still take place, so if you want to use $ in your here-string, use apostrophes instead ( @’ this is my string ‘@ )

Once you’ve got your string, all that you have to do is output the string to a file using Set-Content or Add-Content.

If you’re embedding a powershell script, you can instead use a scriptblock.  This is quite nice if you are writing your script inside an IDE like Powershell ISE, PowerGUI or PrimalScript, as now you can have syntax highlighting which hopefully will help reduce the number of errors inside your embedded script.  I’ve given an example below..


$Script = {

                  Write-Host "inside scripblock.."

}

$Script | Set-content .\Powershell.ps1

If you want to persist or setup arguments into your embedded script, you can use string replacement with here-strings to modify your script. Just put unique placeholder text into your string (such as ARG1_PLACEHOLDER) and then use pattern matching (or string substitution) to replace this with your value.  See the below example..


$Script = @"
'This is the first line of a 'String'' without any matching "quote` marks.
'This is the second line of the string with a         tab in it
option explicit
Dim someVar, otherVar
someVar = SOMEVAR_VALUE
otherVar = OTHERVAR_VALUE
'Do some work here
WScript.Echo "Some output"
WScript.Quit

"@
$Script = $Script -replace 'SOMEVAR_VALUE', "myValue"
$Script = $Script -replace 'OTHERVAR_VALUE', "myOtherValue"
$Script | Set-Content '.\VBScript.vbs'

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s