Auteur de questions
Scripts are an excellent way to make your daily work with a computer less painful or even more fun.
The objective of the FIM and the ILM Scriptbox is to implement a community driven Resource Kit that enables everybody to contribute new ideas and to customize existing ideas the way you like and need it.
For example, if a script generates a report and you need additional information, just modify the script code that retrieves the data.
If you don't like the look and feel of a report, modify the XSLT that is in charge of rendering the data.
There is no need to file a bug and way for a new release - you can just do it...
When looking at the ScriptBox, some of you may have noticed that I’m a HUGE fan HTAs.
To me, HTAs, which is a shortcut for HTML Applications, are the greatest thing since sliced bread since they give you an easy to implement and nice looking way to add a GUI to your script based on simple HTML code.
This is in a nutshell all what a HTA is from the coding perspective - a HTML file with one special tag.
Here is an example for this:
You can find a reference that explains the available attributes / properties here.
HTAs represent full-fledged application without the need for developing source code in Visual Studio.
You can easily modify an existing HTA with - if don't have anything better - for example notepad.
So, if you don’t like the look and feel of a UI, it is very simple to modify it as well.
It is not my intention to explain, how HTA’s work in greater detail.
The Scripting Guys have already done a great job in conjunction with this.
Next to trying to get you motivated to build your own HTAs, I thought it might be helpful to share some tips with you.
Like so often in life, you might run into some hurdles at the beginning.
Fortunately, there is not a lot you need to know.
A script can die and usually, I configure my HTAs to be single instance applications.
When you double-click a "lost" HTA, you can easily identitfy when you are dealing with a dead process - the UI does not show up.
If this happens to you, you should open Task Manager and check, whether you have "hanging" mshta processes.
If so, just end them:
At this point, you should be at least able to get your HTA UI up and running.
Connecting the dots
You might be able to get the UI to show up - but when you click a button in your HTA UI, nothing happens although, it should - now what?
A nice UI without any kind of functionality is a bit lame.
To put some live into your HTA, you need to add script code.
The simplest implementation consists of some VBScript code that does the magic.
While this is already cool, the real power of HTAs is the possibility to combine your VBScript code with other processes.
One example here are command-line applications.
In the ILM world, CSExport and SvrExport are prominent examples of command-line apps you can run from a HTA.
To start another process from your HTA script code, you can use the Run method of the Windows Script Host.
This method enables you to pause the HTA code until the external process has completed.
In addition to that, you can specify the window style for the external process.
One option here is to completely hide the window of the new process.
I tend to use the setting to hide the window of the external process relatively often.
However, if you do this, you should also know how you can get to the error messages of your external process!
An external process is not necessarily an application - it can also be a script.
This is how you can integrate PowerShell into your HTA framework - by calling PowerShell scripts.
Scripting with Visual Basic was already cool; however, PowerShell represents an amazing revolutionary step regarding scripting.
There is hardly anything you can't do with PowerShell - it is amazing!
However, as a lazy person, I found myself often in situations where I missed a nice GUI when I worked with PowerShell.
Yes, you can also create GUIs with PowerShell scripts; however, I found it a bit too complicated - at least, not as simple as creating the UI with HTAs.
So, wouldn't it be cool to bring HTAs and PowerShell together?
The following script code sequence shows the simplest way to call a PowerShell script from your HTA script code:
Dim oShell, scriptPath, appCmd Set oShell = CreateObject("WScript.Shell") scriptPath = oShell.CurrentDirectory & "\Res\Hello.ps1" appCmd = "powershell -noexit &'" & scriptPath & "'" oShell.Run appCmd, 4, true
To run a PowerShell script from your HTA this way, you need to configure your execution policy to allow this.
"I have already set the execution policy and the script still doesn't work".
Have you also set the execution policy on the right PowerShell version?
The following screenshot shows what happens when your try to run a PowerShell script from your HTA in an environment that doesn't allow doing this:
If you look at the path of PowerShell, you will notice that it includes "SysWOW64", which indicates the 32 bit version of PowerShell.
Since we are talking talking about hurdles, there are two things you need to do to determine whether the execution policy is blocking you from running PowerShell scripts from your HTA:
- Locate the script code string, that starts PowerShell and add "-noexit" to it
- Locate the run command and make sure that it doesn't hide the PowerShell window by setting the window style accordingly.
If you run into the error message above, type "set-executionpolicy unrestricted" in your PowerShell window.
Unfortunately, there is one more thing that can prevent a downloaded ScriptBox script from running in your environment - Alternate Data Streams (ADS).
Even, when you have set the execution policy correctly, you might get the following message:
To check, whether ADS is blocking your PowerShell script from running automatically, use the following command:
notepad <Name of your PowerShell script>.ps1:Zone.Identifier
If an ADS is assigned to your PowerShell script, entering the command above will result in something like this:
To get rid of the ADS, just delete the content of the notepad file and save it.
This was the last hurdle; your HTA script should now work as expected.
If your HTA doesn't work as expected, check the following:
- Do you have a hanging mshta process?
- If so, end it
- Is the execution policy blocking your PowerShell script from being invoked?
- If so, set the execution policy accordingly on the right PowerShell implementation
- Is an ADS blocking your PowerShell script from being invoked?
- If so, delete the ADS data
At this point, you can hopefully not wait to start scripting!
I have uploaded with FIM Hello Scriptbox a HTA template that includes tests for the "hurdles" that are outlined in this post.
You can use this template to make the already posted scripts work if you had trouble and also as a template for your own scripts.
As outlined in the introduction, the idea of the ScriptBox is to implement a community driven Resource Kit.
Your participation is key to make this idea work.
Please taka advantage of the option to discuss the content of the ScriptBox by responding to related posts.
Your feedback is paramount to make improvements and to determine gaps.
Do you like the viewers, are documenters good enough, is a HTA usefull, is a PowerShell only implementation good enough, etc..
Please use the voting feature if you find ScriptBox related post helpful.
Last but not least, happy scripting!
Markus Vilcinskas, Technical Content Developer, Microsoft Corporationmercredi 5 août 2009 13:19Modérateur