Creating a Message of the Day banner using a HTA

by Jeremy on December 28, 2007

A customer asked me to assist with implementing a “Message of the Day” (MOTD) banner into their logon scripts. I had written one years ago that was part of a kix32 script, but found that it no longer worked with the wkix32 script processor. I guess that I could have debugged it and fixed it up, but it was old and tired, and needed a face lift. I was always keen to write one using a HTA (HTML Application), so took this opportunity to do so. In my opinion a HTML Application provides a far more professional look for the users.

When I sat down and designed the application I wanted to implement the following features:

  • Separate the MOTD text into a text file, motd.txt, so that no one was actually modifying the HTA itself. This is also how my old kixtart one worked.
  • Have it look for the motd.txt file in the same location as the HTA itself.
  • Provide the ability to supply a command line argument to look for the motd.txt file on a common file share, such as the “netlogon” share on Domain Controllers, which would be “%LogonServer%\netlogon”.
  • If the motd.txt file is missing, close the application. This is important, as it allows you to leave the HTA in place with the existence of the text file determining whether or not it runs.
  • It was also important that the text from the motd.txt file is read in as it is typed. ie. No need to consider formatting, etc. To address this, my script simply appends a <BR> html tag to the end of each line. The <BR> tag provides a carriage return and line feed.
  • I didn’t want multiple instances to run in the same user session at the same time. This can easily be controlled by using singleInstance property of the HTA Application.
  • I didn’t want users being able to simply close or minimise the application. This can easily be controlled by using sysMenu property of the HTA Application. In fact, the only way for them to close the application is for them to accept that they have read and understood it. Unfortunately, if they have permission to do so, they can end the mshta.exe task from Task Manager. This may well be the case for some Desktop/Notebook fleets, but not on locked down Citrix/Terminal Servers.

I looked around on the Internet, but really only came across one sample from Bill Stewart. His article was titled Got Something to Say? Display a Message of the Day. The article and concept was good, and exactly what I was looking for, but the code itself, in my opinion, was not. Although in fairness, Bill had written his application in JavaScript, whereas I was looking for one in VBScript. I personally find VBScript to be cleaner than JavaScript.

It took me a while to get my head around the VBScript code for a few of the features I wanted to implement, as a HTML Application only supports pure VBScript and not the added commands I am used to using with the WScript/CScript Windows scripting host.

If you open a HTA using IE, you will get a security prompt. So to open the HTA silently without the IE security prompt you have two options:

  1. Associate .hta extensions with mshta.exe (Microsoft HTML Application host), the script host for a HTA, which is found in the %SystemRoot%\System32 folder. Then just double click or open the HTA itself.
  2. Simply launch it from the command line using mshta.exe in the syntax.
    i.e. mshta.exe motd.hta

Passing command line parameters/arguments was a challenge until I came across a great Microsoft article by the “Scripting Guy” called How Can I Pass Command-Line Variables to an HTA When It Starts?. It provided me with the exact code required.

So if using command line parameters, you need to use quotes around them. For example:

mshta.exe motd.hta “netlogon”

If you read the Scripting Guy’s article, you will understand that the first argument is array member 3, second is 5, etc. But we are only concerned here with array member 3.

Now I have the mechanism to launch the HTA via the two methods I was looking for in my design.

  1. mshta.exe motd.hta - This will look for the motd.txt file in the same location as the motd.hta.
  2. mshta.exe motd.hta “netlogon”- This will look for the motd.txt file in the %LogonServer%\netlogon share.

Update from 27th March 2012

I received a lot of feedback to suggest that the HTA flashes on the screen and closes. This is due to the parameters not being passed correctly, and therefore the HTA is closing because it cannot find a valid motd.txt file. So I updated the Window-Onload subroutine as per the following:


Sub Window_Onload
 Dim strMOTDFile, arrCommands, strFullName, arrFullName, strScriptFileName, strLogonServer
 Dim strCommonPath, strPath, strcurrentPath, strLine, strContents, i, WshShell, oFSO, objFile

Set WshShell = CreateObject("WScript.Shell")

strMOTDFile = "motd.txt"
 arrCommands = Split(objHTAInfo.commandLine, chr(34))

strFullName = replace(objHTAInfo.commandLine,chr(34),"")
 arrFullName = split(strFullName,"\")
 strScriptFileName = arrFullName(ubound(arrFullName))

' Uncomment the next three lines for testing only.
' For i = 3 to (Ubound(arrCommands) - 1) Step 2
' Msgbox arrCommands(i)
' Next

If Ubound(arrCommands) < 2 Then
 If instr(1,arrCommands(0),"Netlogon",1) = 0 Then
 strcurrentPath = replace(strFullName,strScriptFileName,"")
 strPath = strcurrentPath
 Else
 strLogonServer = WshShell.ExpandEnvironmentStrings("%LogonServer%")
 strPath = strLogonServer & "\Netlogon\"
 End If
 ElseIf Ubound(arrCommands) = 2 Then
 If (arrCommands(2) = "" OR arrCommands(2) = " ") AND instr(1,arrCommands(1),"Netlogon",1) = 0 AND instr(1,arrCommands(2),"Netlogon",1) = 0 Then
 strcurrentPath = replace(strFullName,strScriptFileName,"")
 strPath = strcurrentPath
 ElseIf instr(1,arrCommands(1),"Netlogon",1) > 0 OR instr(1,arrCommands(2),"Netlogon",1) > 0 Then
 strLogonServer = WshShell.ExpandEnvironmentStrings("%LogonServer%")
 strPath = strLogonServer & "\Netlogon\"
 Else
 ' Note that there are two different ways for presenting the path
 ' strcurrentPath = Left(document.location.pathname,InStrRev(document.location.pathname,"\"))
 strcurrentPath = Replace(Left(document.location.pathname,InStrRev(document.location.pathname,"\")),"%20"," ")
 strPath = strcurrentPath
 End If
 ElseIf Ubound(arrCommands) = 3 Then
 strLogonServer = WshShell.ExpandEnvironmentStrings("%LogonServer%")
 strPath = strLogonServer & "\Netlogon\"
 Else
 If instr(1,arrCommands(0),"Netlogon",1) > 0 OR instr(1,arrCommands(3),"Netlogon",1) > 0 Then
 strLogonServer = WshShell.ExpandEnvironmentStrings("%LogonServer%")
 strPath = strLogonServer & "\Netlogon\"
 Else
 strcurrentPath = replace(strFullName,strScriptFileName,"")
 strPath = strcurrentPath
 End If
 End If

' Uncomment the next line for testing only.
' Msgbox "Path to " & strMOTDFile & " is:" & strPath

Set oFSO = CreateObject("Scripting.Filesystemobject")
 If oFSO.fileexists(strPath & strMOTDFile) Then
 Set objFile = oFSO.OpenTextFile(strPath & strMOTDFile, ForReading)
 Do Until objFile.AtEndOfStream
 strLine = objFile.ReadLine
 strContents = strContents & strLine & "<BR>"
 Loop
 objFile.Close
 document.getelementbyid("textarea").innerHTML = strContents
 End If

Set WshShell = Nothing
 Set oFSO = Nothing
 Set objFile = Nothing
 End Sub

Update from 20th August 2013

I received feedback that the HTA was not working correctly with IE9 and IE10. I found that the code needed to be enhanced in two places:

1. Set the <!DOCTYPE> declaration for HTML 4 at the very top of the script as per the code below:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

2. Set the http-equiv attribute to be compatible with IE8 within the <head> entry as per the code below:

<head>
<meta http-equiv="X-UA-Compatible" content="IE=8"/>
<title>Message of the Day</title>

The following screenshot is an example of what the MOTD HTA looks like when running.

motd Creating a Message of the Day banner using a HTA

There are four ways that you can implement this:

  1. Chain or call it from an existing logon script.
  2. Add it as a Logon Script in a Group Policy Object.
  3. Add it to the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run key on all computers.
  4. Add a shortcut to the “%ALLUSERSPROFILE%\Start Menu\Programs\Startup” folder on all computers.

Download the code: motd.zip

Future enhancements:

  • Log the acceptance to a file or registry before closing the application. This can provide some legal and compliance tracking if needed.
  • Add the customers logo into the application. This is simple to do, but I need to consider where on the banner to locate the logo, and how I can construct some code that manages different size logos.
  • Implement different messages based on Group Membership. Could be of value to different organisations. You can actually do this already using multiple Group Policy Objects. However, my idea was to implement this by using different group membership tags within the one motd.txt file that would expose different lines and paragraphs to users depending on their group membership. Probably more complex than it needs to be, but would make a nice feature.

Please feel free to use as you wish and provide any useful feedback you feel may be of benefit to others.

Jeremy Professional Shot Sept 2010 white Creating a Message of the Day banner using a HTA

Jeremy

Independent Consultant | Contractor | Microsoft & Citrix Specialist | Desktop Virtualization Specialist at J House Consulting
Jeremy is a highly respected, IT geek, with over 28 years’ experience in the industry. He is an independent IT consultant providing expertise to enterprise, corporate, higher education and government clients. His skillset, high ethical standards, integrity, morals and attention to detail, coupled with his friendly nature and exceptional design and problem solving skills, makes him one of the most highly respected and sought after Microsoft and Citrix technical resources in Australia. His alignment with industry and vendor best practices puts him amongst the leaders of his field.
Jeremy Professional Shot Sept 2010 white Creating a Message of the Day banner using a HTA
Jeremy Professional Shot Sept 2010 white Creating a Message of the Day banner using a HTA
Jeremy Professional Shot Sept 2010 white Creating a Message of the Day banner using a HTA
  • tom

    nice script… this is EXACTLY what I was interested in doing. I really need the logging feature… is this something you plan to do soon or do you have a recommendation of something commercial I might use? I'm working for a company that needs the audit function/logging function you mention in your article above…

    Thanks – Tom

  • jeremy

    Thank you Tom. No, I haven't had the time to add any of the enhancements yet. Been focused on lots of other things. Logging the acceptance to a file, registry or even the Event Log is not too hard though. Give it a go and see how you go :) The best thing is that you will actually learn so much about scipting and HTAs by doing it yourself.

    Cheers,

    Jeremy.

  • Seth

    Is this being done on a standalone workstation or aare you deploying it as a gruop policy across a network?

  • jeremy

    Hi Seth,

    The main aim of this task was to deploy it through a Group Policy, but I have considered all 4 methods documented in the article, so should therefore work. Admittedly, all 4 have not been tested. Please let me know if you run into a problem and what the issue is, and I'll take a closer look.

    Cheers,

    Jeremy.

    • Adi Ariyanto

      Hi, Jeremy..

      I am try to run your motd use logon script, here what I have done

      1. I am put both motd.hta and motd.txt on C:WindowsSYSVOLsysvolmydomain.localscripts as logonserver.

      2. On test AD user properties, I am fill motd.hta on logon script.

      3. i am log on as test user in my xp virtual machine, It dont show anything, then I try debug it with uncomment

      ‘ Msgbox “Path to ” & strMOTDFile & ” is:” & strPath

      the result is Path to motd.txt is \nameofserverNetlogon –> I think it already correct

      but the message in motd.txt is not shown, only “I have read and understood this message”

      whats wrong ?

      • http://wwww.jhouseconsulting.com/ Jeremy Saunders

        Hi Adi,

        Did you add the extra code into the HTA as from my update of 27th March 2012? That will be you’re problem.It’s related to the arrCommands(3) array. With my update it should work under any situation. Before my update it would fail under some scenarios.

        Cheers,
        Jeremy

        • Adi Ariyanto

          Hi Jeremy,

          Yes, I change Window-Onload subroutine with your update in 27 March, but even after I change back to motd.hta that I get from motd.zip It just splash when login, what do I missed ?

          • http://wwww.jhouseconsulting.com/ Jeremy Saunders

            Hi Adi,

            What is the exact command line you are using, with quotes and all?

            Cheers,
            Jeremy

          • Adi Ariyanto

            Hi Jeremy,

            1. I just put motd.hta and motd.txt in SYSVOL-Script folder then I write down motd.hta user logon script.
            https://drive.google.com/file/d/0B-P9whvDiOuFRjJFQXRWYUZ5ZzQ/edit?usp=sharing
            https://drive.google.com/file/d/0B-P9whvDiOuFLTVlcEtoSmdwNW8/edit?usp=sharing

            2. I am sorry for my lack of knowledge, you ask what exact command line that I use, what command line ? ^^, I just want make that everyone that login into his/her computer that already joint with AD, can get motd,
            actually, I can put motd.hta and motd.txt in user PC local folder and set schedule task to run it, but I woud difficult to change motd content. I am think by put motd.hta and motd.txt in AD server and run from it, It will simplify to change motd content when ever I need.

            Thanks

          • http://wwww.jhouseconsulting.com/ Jeremy Saunders

            Hi Adi,

            As per the screen shot you sent me, I would not be directly launching the HTA from a Group Policy Login Script. Instead, use a batch (.cmd) file that contains the following line…

            start “%SystemRoot%System32mshta.exe” “%~dp0motd.hta”

            Place the batch script in the same location as the HTA, and simply just set it as the logon script. It will take care of the rest and ensure it’s launched correctly. It also ensures that it’s launched via mshta.exe and not iexplore.exe.

            Cheers,
            Jeremy

  • Pingback: J House Consulting / Displaying useful connection information on the desktop wallpaper()

  • mevans1973

    ran accross this and love it… however I added it to the users logon script profile setting and when they log in it just flashes and disappears. I feel like a dork because Im NOT a VB guy and love this tool but just cant get it to stay up.

  • Pingback: HTA file in Group Policy - GP Answers Community Forum()

  • DIEGO

    He mejorado el MOTD para que funcione vía GPO y adicianlmente logre hacer que el usuario al dar click en el boton de confirmación de lectura quede un registro de que ha leido el mensaje en una ruta compartida. Se guarda elnombre de inicio de sesion, el nombre del pc, el controlador de dominio donde autenticó y la hora del evento.

    Tks a lot for Jeremy

    • Jeremy

      Thanks Diego. Would love to see your improvements.

      Cheers,
      Jeremy

  • Ed

    Hi Jeremy:
    I’m trying to shell your banner hta from kix but I keep receiving an error with the ActiveX component can’t create object Scripting.Filesystemobject. Any ideas?

    Thanks!

    Ed

    • http://www.jhouseconsulting.com/ Jeremy

      Hi Ed,

      Are you executing it via the mshta.exe (Microsoft HTML Application host), or are you just executing it directly? If you execute it directly it will use IE, which may be causing this object error. Can you execute it successfully outside of kix?

      I haven’t used kix for a while. Last time I implemented it, I executed it from a batch file using the following command line, assuming that the batch file, motd.hta and motd.txt are in the same folder.

      start “%SystemRoot%\System32\mshta.exe” “%~dp0motd.hta”

      Cheers,
      Jeremy

  • Ed

    Jeremy,
    I am using mshta.exe to run the motd.hta but it still shows the same error. I’ve tried running it from a bat file also.

    • http://www.jhouseconsulting.com/ Jeremy

      Hi Ed,

      Not sure, as I’m not experiencing that error. However, I’ve e-mailed you the same copy I’ve been running just in case there is an issue with the download from here.

      Cheers,
      Jeremy

  • Laurence

    Hi,

    Having a play with your “Message of the Day” but I don’t get the background. The :DXImageTransform.Microsoft.gradient doesn’t seam to work.

    I have changed this to “background: -ms-linear-gradient(top, #000000, #0000FF;’ as per IE 10 but still get nothing.

    Any idea’s?
    Cheers,
    Laurence

    • http://www.jhouseconsulting.com/ Jeremy

      Hi Laurence,

      Sure, there are issues with IE9 and above, but not an issue with the XImageTransform.Microsoft.gradient itself. I’ve updated the article to show the required changes. Basically, you need to…
      1) Set the < !DOCTYPE> declaration for HTML 4 at the very top of the script
      2) Set the http-equiv attribute to be compatible with IE8 within the head entry

      I hope that helps.

      Cheers,
      Jeremy

  • irwan

    hi jeremy

    thanks before, for you code
    if i use it directly, its work
    but if i use it via task schedular, the error pop up
    “subscript out of range”

    can you help me ?

    • http://www.jhouseconsulting.com/ Jeremy

      Hi Irwan,

      You should have received a line number in that error? If you open the hta in notepad, what is the code on that line?

      What’s the action set to in the task? ie. List the Program/Script and Arguments you have set.

      Cheers,
      Jeremy

      • irwan

        hi jeremy

        the error : line 62,
        char : 7
        subscript out of range :’[number:3]‘
        code : 0

        thanks,

        irwan

        • http://www.jhouseconsulting.com/ Jeremy

          Hi Irwan,

          Okay, that’s the arrCommands(3) array, which is related to the arguments. Have you integrated the code from my update of 27th March 2012? That should address all scenarios.

          Cheers,
          Jeremy

          • Adi Ariyanto

            It works perfectly with task scheduler on XP.

  • irwan

    thanks jeremy

    its work now

    :)

  • Tony

    Thanks for this and the continued support. It’s used everyday!

    • http://www.jhouseconsulting.com/ Jeremy

      You’re welcome Tony :-)

Next post: