JScript is crashing with a very poor reason given - jscript

I have the following code which works should theoretically work well:
var arg = WScript.arguments(0);
alert(arg.toString);
So when I run it in CMD, like this:
hello_world.js "Sup"
It'll get a runtime error immediately at line 2 with the reason being:
Object expected
But these docs say the alert function expects a string argument:
http://www.w3schools.com/jsref/met_win_alert.asp
And actually.... it gets worse.
This will result in the same crash with the same runtime:
alert("ummm wtf...");
Is Microsoft JScript just useless or am I doing something wrong?

Jscript is case sensitive .
Try with:
WScript.Echo(WScript.Arguments.Item(0));
In WSH there's no alert , and you need to use WScript.Echo
you can call the script like this cscript /e:jscript /nologo script.js
or wscript /e:jscript /nologo script.js .The first will output the messages in the console the second with pop-ups.
If you need to use alert you'll need MSHTA - http://www.brivers.com/resume/scripts/tutorial-hta.php

Related

Bash debug calling stack

I'm using a generic procedure to trap and describe the error or abnormal situations, instead of the usual '2>...' error construct.
The idea will be to have a procedure like this simplified version:
function debug(){
echo "Fatal Error: $PWD:$BASH_SOURCE:$LINENO $*"
....
exit 1
}
and then, used as in this example:
[ -z "$PARAMETER" ] && debug The parameter was not provided
The issues are:
BASH_SOURCE is the running source. The idea is to show the calling source, if the procedure 'debug' is global.
LINENO is the line where the expansion is executed and no the calling address.
Note: BASH_SOURCE[0] and BASH_SOURCE[1] provide 'some help' when the procedure is sourced.
This will be used to notify 'user' errors, on a centralized error message procedure. This may include a post in syslog and on other log files. However, some messages may look alike and knowing where in the source code the error was detected, help the developers.
Is there any method to obtain a calling stack on bash?
You can use bash built-in command "caller" for this.
Link --> http://wiki.bash-hackers.org/commands/builtin/caller
If you add following line in the script, system will show the execution flow of the script:
set -x

How to return information from Excel to the caller

I start Excel from the command line and my add-on does some work. when it's done, I want to return some info to the caller. At least 0/1 for success failure or better also an optional error-message.
By caller I mean the command or process that started Excel. e.g. in a Windows command script I could call excel like this:
Excel.exe SomeWorkbook.xlsx /p C:\Somedir /e
when you call an executable in Windows, it can return an numeric code or set an error.
in a script you could check the result like this:
if %errorlevel% neq 0 (
echo some error occurred...
)
MessageBoxes, etc. are no option, because this whole task should be triggered by another application automatically without any user-interaction.
How can we do that?
You could use the status bar:
Application.StatusBar = “your message here”
As far as I know, the message box requires a button to be clicked: macro will wait...
I ended up using text files: i.e when the add-on finished correctly, it will create an empty file OK.txt and when an error occurred it will create a file named ERR.txt that contains the error-message.
Now it's easy for the calling script to check the result:
OK.txt exists: everything is fine - delete OK.txt
no file exists: a fatal error has happened: show a general error message
ERR.txt exists: an error occured: maybe display the error text (contents of the text-file) to the user, delete ERR.txt

Post build event in VS2012..Running a Batch file

Am trying to run a Batch file in Post build event in Visual studio.
Referred Can we execute a .bat file in post build event command line in visual studio? for reference.
When i post the line
xcopy "$(ProjectDir)bin" "$(SolutionDir)Deploy\bin" /S in postbuild
am getting the expected result
Same line i put in bat and tried calling
call "$(SolutionDir)\Deploy.bat"
or
call "Physical path\deploy.bat"
Am getting excited with code 1. What am i doing wrong here ?
Can i specify macros inside batch file ?
Thanks
You are getting a VS error because it returned an exit code that is not 0. This does not necessarily mean there was an error.
The error code returned means that no files were copied.
These are the return codes for Xcopy:
Exit Code
0 Files were copied without error.
1 No files were found to copy.
2 The user pressed Ctrl+C to terminate xcopy.
4 Various errors including insufficient memory or disk space, an invalid drive name, or invalid syntax.
5 Disk write error occurred.
Try this code in your batch file. Use the /Y so that you will not have to deal with any prompts. You can handle the return code of 1 with another action or just return 0.
VS Post Build command line code:
CALL "$(SolutionDir)"Deploy.bat "$(ProjectDir)bin" "$(SolutionDir)Deploy\bin"
Deploy.bat file
Xcopy %1 %2 /S /Y
If errorlevel 1 #exit 0

cscript - Invalid procedure call or argument when running a vbs file

I've been trying to use check_time.vbs to check the Windows time.
Here's the script: http://pastebin.com/NfUrCAqU
The help message could be display:
C:\Program Files\NSClient++\scripts>cscript //NoLogo check_time.vbs /?
check_time.vbs V1.01
Usage: cscript /NoLogo check_time.vbs serverlist warn crit [biggest]
Options:
serverlist (required): one or more server names, coma-separated
warn (required): warning offset in seconds, can be partial
crit (required): critical offset in seconds, can be partial
biggest (optional): if multiple servers, else use default least offset
Example:
cscript /NoLogo check_time.vbs myserver1,myserver2 0.4 5 biggest
But I get the following error when running:
C:\Program Files\NSClient++\scripts>cscript //NoLogo check_time.vbs 0.asia.pool.ntp.org 20 50
C:\Program Files\NSClient++\scripts\check_time.vbs(53, 1) Microsoft VBScript run
time error: Invalid procedure call or argument
The screenshot:
Manually execute w32tm still works fine:
What might be the cause of this?
IIRC the (53,1) indicates that the error is on line 53. At this point it is expecting an array of regexp matches with at least one item (index 0) and one sub-match (i.e. the object in position 0 in the array has an array property called SubMatches with at least one item in it.
It is not checking to make sure this structure is present and correct before trying to use it.
My assumption is that the regexp call is failing to find anything to match, presumably because the input string is not in the expected format. You could output the content of strOutput before that line to see what it contains - it could be a date/time representation in a different localized form than the regexp is designed for. You could also output the content of input after each call to objProc.StdOut.ReadLine - this would show you if the call to w32tm.exe returned a useful error message that is being skipped over by the script which is just looking for the value returned when all is well and ignoring the possibility of different output.
The culprit is the /nowarn argument:
w32tm /monitor /nowarn /computers:0.asia.pool.ntp.org
The following arguments were unexpected: /nowarn
Exiting with error 0x80070057
Remove it from the script, and now it works:
cscript //NoLogo check_time.vbs 0.uk.pool.ntp.org 20 50
NTP OK: Offset -2.4262131 secs|'offset'=-2.4262131s;20;50;

nmake: can a batch file run as part of a make command block, affect the environment of the nmake.exe process?

I think in nmake if I do this:
example :
set value=77
echo %%value%%
The result will display 77 on the console.
Is there a way for me to invoke a .cmd or .bat file that will affect the environment of the nmake.exe process? Suppose I put the statement set value=77 in a file called "setvalue.cmd". Then change the makefile to this:
example :
setvalue
echo %%value%%
I get:
%value%
Alternatively, if there's a way to set a macro within a command block, that would also work. Or, a way to set the value of a macro from a batch file, even outside a command block.
You can create an nmake snippet during makefile pre-processing, and read that in. Assuming batch.cmd outputs valid nmake syntax, then
!if [batch.cmd >makefile.auto]
!error *** Could not create makefile.auto
!endif
!include makefile.auto
You should ensure batch.cmd sets %errorlevel% appropriately (e.g., exit /b 22).
makefile.auto can contain anything, but you would probably want stuff like value=77. A couple of points:
Dereference value using nmake syntax ($(value))
You can pass parameters to batch.cmd if necessary ([batch.cmd $(OBJECTS) >makefile.auto])
No, I don't think so.

Resources