Search code examples
javascriptvariableshta

Include variable string inside JS function (HTA app)


I am trying to include variable inside cmd /c runas command, I use JS function and it gives me an error that specified file was not found.

I guess I am using the wrong variable calling or something.

Here is my function:

function runasSRVhta(){
var WShell = new ActiveXObject('WScript.Shell');
// var srvDude = document.getElementById("SRVinput").value;
var SRVguy = "someadmin.srv";
WShell.Run('cmd /c runas /u:sa.local\\SRVguy "c:\\windows\\system32\\mshta.exe """\\\\fs\\FIle Share\\SA Support\\ZverTools\\giveMeIPsPLZ.hta""""', 1, true);
}

Instead of running following command as someadmin.srv it runs its variable name, without taking value.

Problem is here - cmd /c runas /u:sa.local\\SRVguy, SRVguy should be variable taken from var SRVguy = "someadmin.srv";

  • Update

I tried following function as well, it detects variable value, but my HTA gives me another error saying that it can't find the file specified.

function testsrv() {
  var shell = new ActiveXObject("WScript.Shell");
  var SRVguy = "someadmin.srv";
  var SRVguyaftertext = 'c:\\windows\\system32\\mshta.exe """\\\\fs\\FIle Share\\SA Support\\ZverTools\\giveMeIPsPLZ.hta"""';
  var satavesrv = 'cmd /c runas /u:sa.local\\';
  var theend = "'" + satavesrv + SRVguy + ' "' + SRVguyaftertext + '"' + "'";
  document.write(theend);
  var path = theend;
  shell.run(theend,1,false);
}

Oh, and I used document.write to check output if it is correct, here is the output:

'cmd /c runas /u:sa.local\someadmin.srv "c:\windows\system32\mshta.exe """\\fs\FIle Share\SA Support\ZverTools\giveMeIPsPLZ.hta""""'

Everything seems correct with second function, but HTA pops up an error message saying that it cannot find the file specified...


Solution

  • try using template strings to include the variable in the string like this

    WShell.Run(`cmd /c runas /u:sa.local\\${SRVguy} "c:\\windows\\system32\\mshta.exe """\\\\fs\\FIle Share\\SA Support\\ZverTools\\giveMeIPsPLZ.hta""""`, 1, true);
    

    - Update

    WShell.Run('cmd /c runas /u:sa.local\\' +SRVguy + ' "c:\\windows\\system32\\mshta.exe """\\\\fs\\FIle Share\\SA Support\\ZverTools\\giveMeIPsPLZ.hta"""', 1, true);