2

I have a HTA file containing javascript executed on load to pull the users network logon:

var WinNetwork = new ActiveXObject("WScript.Network");
return WinNetwork.UserName;

When I first open the HTA file, I receive a runtime error:

"The automation server can't create object"

on the first line of code above. I dismiss the error and close the HTA window.

If I then re-launch the HTA file within a few seconds of closing, it works without error and I get the UserName value. If I leave it for a few minutes and launch it again, the error returns. I have been able to repeat these outcomes every time I have tried.

Does anyone know why this doesn't work the first time? And how I might resolve it? Thanks

Edit: Thank you for your answers so far, but this is a HTA and is not run in IE, security settings are not relevant.

from https://en.wikipedia.org/wiki/HTML_Application: An HTML Application (HTA) is a Microsoft Windows program whose source code consists of HTML, Dynamic HTML, and one or more scripting languages supported by Internet Explorer, such as VBScript or JScript. The HTML is used to generate the user interface, and the scripting language is used for the program logic. An HTA executes without the constraints of the internet browser security model; in fact, it executes as a "fully trusted" application.

  • possible duplicate of [IE9, Automation server can't create object error while using CertEnroll.dll](http://stackoverflow.com/questions/15686040/ie9-automation-server-cant-create-object-error-while-using-certenroll-dll) – SK. Jul 15 '15 at 07:22
  • not a duplicate, nothing to do with CertEnroll.dll and I'm using HTA shell not IE browser so the browser security settings are not relevant. – brock.appleby Jul 15 '15 at 23:28
  • Can you read the username this way? http://stackoverflow.com/questions/14276032/hta-html-application-vbscript-reading-textfile-line-and-colouring-that-line-on – BoltBait Jul 15 '15 at 23:45
  • How about other ActiveXs, can you use them? If you can, and the username is the only you need, you can retrieve it also with shell: `user = new ActiveXObject('WScript.Shell').ExpandEnvironmentStrings('%username%');` – Teemu Jul 16 '15 at 06:02
  • Can you show more of your hta, I tried to repro the problem based on your description, but, everytime I run it, it just works. – Stephen Quan Jul 16 '15 at 06:05
  • Deleted my answer which is not relevant here @brock.appleby – SK. Jul 16 '15 at 06:58

1 Answers1

0
<script>
  (function() {
    var shell;

    try {
      shell = new ActiveXObject('WScript.Shell');
    } catch(e) {
      return alert("Couldn't create the script shell :(");
    }

    function getEnv(id){ return shell.Environment("Process").item(id); }

    var username = getEnv("USERNAME");

    alert("Hello, " + username + ".");
  })();
</script>
bosscube
  • 189
  • 10