Include variable string inside JS function (HTA app) - javascript

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...

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);

Related

Mirth originalFilename rename

I have been trying to remove .dat from ${originalFilename} in destination when i tried to do this ${originalFilename}.txt it gave me like 1652807798759.dat.txt how can i get 1652807798759.txt only without .dat in it
I have tried to do this in Destination's Transformer but no luck
channelMap.put('OrigFilename', sourceMap.get('originalFilename'));
var outFile = channelMap.get('OrigFilename');
logger.info('outFile ' + outFile ); // i am getting outFile as null here
//outFile=outFile.replace('.dat','');
A simple replace should work:
//test it with a static string first, in real code use sourceMap.get('originalFilename').toString();
var outFile = '1652807798759.dat.txt' ;
outFile = outFile.replace(/\.dat/g, "");
logger.info('outFile ' + outFile );

fix doScript function error in InDesign script

I wrote a script for InDesign and used doScript to run a bat file. This script works on some systems and not on others.
The error is in the image.
I run as admin InDesign. But it gives another error.
How can I fix the error?
function batFile(str) {
var path = "~\\AppData\\Roaming\\test\\";
var filename = 'b1.bat';
var file1 = new File(path + filename);
file1.encoding = 'UTF-8';
file1.open('w');
var txt = "systeminfo | findstr /B /C:\"OS Name\" /C:\"OS Version\">%appdata%\\test\\t1.txt"
file1.write(txt);
file1.close();
var cmdcode = "CreateObject(\"WScript.Shell\").Run \"%appdata%\\test\\b1.bat\", 0, True";
app.doScript(cmdcode, ScriptLanguage.visualBasic, undefined, UndoModes.FAST_ENTIRE_SCRIPT);
var result = path + "t1.txt";
var arry = openFile2(result);
if (arry.length != 0) {
return arry;
} else {
return "null";
}
}
**
Update
I find the problem.
When a user name is composed of two parts, such as "your name" causes this problem. To address this issue, we need to put the address in two double quotations.
var txt = "systeminfo | findstr /B /C:\"OS Name\" /C:\"OS Version\">\"%appdata%\\test\\t1.txt"\"
**
Update 2
In Windows, when the user uses OneDrive, the AppData path also changes, which causes doScript not to run. for example:
c: \ users \ username \ appdata
Changes to:
c: \ users \ username \ onedrive \ appdata
This is a known bug reported in the UserVoice: https://indesign.uservoice.com/forums/601180-adobe-indesign-bugs/suggestions/41072476-type-library-is-not-automatically-created-by-cc202. A common trick to solve this is, run InDesign at least once as administrator on the client machine. This activates VBS support.
I can't tell what the problem with your script. But just in case, you can run bat file this way:
var bat_file = File("your_file.bat");
bat_file.execute();
Update
Based on your code I can offer the workaround: add & echo ok > %appdata%\\test\\ok.txt in your bat file and check if the file ok.txt exists before going further.
function batFile(str) {
var path = "~\\AppData\\Roaming\\test\\";
var filename = 'b1.bat';
var file1 = new File(path + filename);
file1.encoding = 'UTF-8';
file1.open('w');
var txt = "systeminfo | findstr /B /C:\"OS Name\" /C:\"OS Version\">%appdata%\\test\\t1.txt";
// add the file 'ok.txt' after the previous command is finished
txt += " & echo ok > %appdata%\\test\\ok.txt";
file1.write(txt);
file1.close();
// run the bat file
file1.execute();
// check if the file 'ok.txt' exists before going further
var ok = File(path + "ok.txt");
while (!ok.exists) $.sleep(100);
ok.remove();
// do stuff
var result = path + "t1.txt";
var arry = openFile2(result);
if (arry.length != 0) {
return arry;
} else {
return "null";
}
}

How to modify environment variables using WMI and JavaScript?

I need to access an environment variable and modify its value. I can access the variable using WQL ==>
wmi.ExecQuery("Select * from Win32_Environment Where name='Path' And UserName='<System>'");
However, I am not sure how to modify and save the value. I am using:
var reg = GetObject("winmgmts:/root/cimv2");
var paths = wmi.ExecQuery("Select * from Win32_Environment Where name='AA' And UserName='<System>'");
var items = new Enumerator(paths);
var path = items.item();
path.VariableValue = path.VariableValue + ";" + "random";
path.Put_(); //(as per first answer received)
But, I get this error:
Access denied
Code 80041003
Source SWbemObjectEx
I have UAC disabled, not sure what to do here.
Any help will be appreciated.
Thanks.
After you change the VariableValue, call Put_ to apply the changes:
path.VariableValue = path.VariableValue + ";" + "random";
path.Put_();

javascript giving unexpected results

I have the following javascript:
#!/usr/bin/env node
var fs = require(’fs’);
var outfile = "hello.txt";
var out = "Modify this script to write out something different.\n";
fs.writeFileSync(outfile, out);
console.log("Script: " + __filename + "\nWrote: " + out + "To: " + outfile);
on executing the following commands:
node test.js
cat hello.txt
i get the following output:
[object Object]
What am i doing wrong?
Your getting an object returned as a string. Your going to need find out what properties the object has and return them, or loop through the object and show the properties, or convert it to a string...
console.log(JSON.stringify(objToJson));

Quicker searching in JScript using the Bash

I am using the following JScript code to search for a string inside a file:
var myFile = aqFile.OpenTextFile(fileToSearchIn, aqFile.faRead, aqFile.ctANSI);
while(!myFile.IsEndOfFile())
{
s = myFile.ReadLine();
if (aqString.Find(s, searchString) != -1)
Log.Checkpoint(searchString + " found.", s);
}
myFile.Close();
This is rather slow. I was thinking about using bash commands in order to speed up the search in file process:
var WshShell = new ActiveXObject("WScript.Shell");
var oExec = WshShell.Exec("C:\\cygwin\\bin\\bash.exe -c 'cat \"" + folderName + "/" + fileName + "\"'");
while (!oExec.StdOut.AtEndOfStream)
Log.Checkpoint(oExec.StdOut.ReadLine());
while (!oExec.StdErr.AtEndOfStream)
Log.Error(oExec.StdErr.ReadLine());
Since every time bash.exe is started a new window opens the searching is not faster than before. Is there a possibility to have the bash run in the background using another switch?
Every invocation of WshShell.Exec will start a new process which is costly. If your textfile is not too big this will prevent spawning a new process:
var myFile = aqFile.OpenTextFile(fileToSearchIn, aqFile.faRead, aqFile.ctANSI);
var myFileData = myFile.Read(myFile.Size);
var index = myFileData.indexOf(searchString);
if(index>0)
{
Log.Checkpoint(searchString + " found.", index);
}
myFile.Close();
This will not print the whole line but the index of the found location. If you want the whole line, search for line endings from there.

Categories