I have a VB script that takes in several parameters that could include spaces using cscript, and I make the call using:
nsExec::exec 'cscript.exe "$PATH_TO_FILE\program.vbs" "Something with spaces" "Something else"'
Now, I want one of the "Something else" strings to include a double quote character, where the string is
Something " else.
I have tried
nsExec::exec 'cscript.exe "$PATH_TO_FILE\program.vbs" "Something with spaces" "Something "" else."'
with an escaped " but that did not work, it simply used "Something else" as the string passed in.
Basically, there is not a way to deal with these quotes, so you need a workaround (use QUOTE and then replace in program with a ').
You can read the entire process command line as one string like this (JScript code, sorry):
// Read process command line
var WshShell = WScript.CreateObject("WScript.Shell");
var objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\cimv2");
var childProcess =
WshShell.Exec
(
'"' + WshShell.Environment('PROCESS')('ComSpec') + '"'
+
" /C Echo \"Text lines\" && Set /p VarName="
);
childProcess.StdOut.ReadLine();
var current_pid =
objWMIService.ExecQuery
(
"Select * From Win32_Process Where ProcessId=" + childProcess.ProcessID
);
current_pid = (new Enumerator(current_pid)).item().ParentProcessId;
if (current_pid)
{
childProcess.StdIn.WriteLine("value"); // child process should now exit
}
else
{
WScript.StdErr.WriteLine("Get current PID from WMI failed.");
WScript.Quit(7);
}
var cmd_line = objWMIService.ExecQuery("Select * From Win32_Process Where ProcessID=" + current_pid);
cmd_line = (new Enumerator(cmd_line)).item().CommandLine;
WScript.Echo(cmd_line);
but than you will have to parse the string into separate arguments yourself.
Related
I have a TCL proc like this, & want to add a line after the start of the proc, the puts "puts " entered myproc" " line
proc myproc { {filename "input.txt"}
{var1 "x"}
{var2 "y"}
{var3 "z"}
{var4 ""}
{var5 "0"}
{var6 "0"}
{var7 0}
} {
puts " entered myproc"
Can you help?
& it should also work for
proc myproc2 { N val } {
puts " entered myproc"
# comment line
set ret {} for { set i 0 } { $i < $N } { incr i } { lappend ret $val }
return $ret
}
If all you want to do is get an execution trace of your code, such as a call stack dump etc, then you don't need to modify your source code at all. You can use tcl itself to do it for you.
Tcl has no reserved keywords, none at all. Not even proc is reserved. You can therefore redefine it:
rename proc _proc
# Now proc no longer exists but we have _proc instead.
# Use it to redefine "proc":
_proc proc {name arguments body} {
set body "puts \"entered $name\";$body"
_proc $name $arguments $body
}
Just do that before running any of your own code and you'll find that every proc prints out when it's being entered on each call.
This is how a lot of tcl debuggers and profilers work - using tcl to redifine itself.
From your comments it looks like you're trying to also print how deep the stack is with each call. To do that you need to add more code to each proc definition. The most straightforward way is of course something like this:
_proc proc {name arguments body} {
set preamble"set dist2top \[info level\];puts \"\$dist2top entered $name\""
set body "$preamble;$body"
_proc $name $arguments $body
}
But as you can see, writing code inside strings can quickly become unmanagable. There are several tricks you can use to make it more manageable. One of the more common is to split $body by line and use list commands to manipulate code. It should reduce at least one level of quoting hell. My favorite is to use a templating technique similar to how you'd write html templates in MVC frameworks. I usually use string map for this:
_proc proc {name arguments body} {
_proc $name $arguments [string map [list %NAME% $name %BODY% $body] {
set dist2top [info level]
puts "$dist2top entered: %NAME%"
%BODY%
}]
}
The last argument in the _proc definition is just a string but it looks like a code block which makes it easier to read. No nasty quoting hell with this technique.
Using awk you can do:
awk '/^ *proc/ {$0 = $0 "\nputs \" entered myproc\""} 1' RS= proc-file.tcl
Gives this file:
proc myproc { {filename "input.txt"}
{var1 "x"}
{var2 "y"}
{var3 "z"}
{var4 ""}
{var5 "0"}
{var6 "0"}
{var7 0}
} {
puts " entered myproc"
Currently, I am reading some data from Perl, I want to retrieve messages from Perl for each and every step. I tried using this code,
Process process = Runtime.getRuntime().exec(new String[] {"perl","C:\\Perl\\bin\\try.pl", "uname=test"});
BufferedReader reader =new BufferedReader(new InputStreamReader(process.getInputStream()));
String s = reader.readLine();
System.out.println("perl said [ " + s + " ]");
My perl script as follows,
$path_to_file = $ARGV[0] or die("argument not passed") ;
print "success";
# some code
If I dont receive any argument, then we need to retrieve the message, that there is no argument passed. like that I want log all the information. I tried giving print data. That is working. But if the argument is not passed. I want the data that argument not passed.
is it possible to retrieve?
I hereby found the answer for my above question,
Process process = Runtime.getRuntime().exec(new String[] {"perl","C:\\Perl\\bin\\try.pl", "uname=test"});
BufferedReader readError = new BufferedReader( new InputStreamReader(process.getErrorStream()));
s = readError.readLine();
System.err.println("perl said [ " + s + " ]");
By this way i capture the error details. Thanks for all your suggestions. Also, i changed my perl script as follows,
my $path_to_file = $ARGV[0] or {print " Argument not passed! " };
$keywords = explode("\n",$_POST['keywords']);
//Inserting into Database
foreach($keywords as $key => $keyword){
$keyword = strtolower(trim($keyword));
$keyword = preg_replace("/[^A-Za-z0-9' ]/", "", $keyword);
$keyword = preg_replace("/\s+/", " ", $keyword);
$insertkeywords = "INSERT IGNORE INTO kwdb (keyword) VALUES ('$keyword')";
mysql_query($insertkeywords);
}
I can't for the life of me figure out why this code won't insert the keywords into the database when I have an apostrophe in the:
"/[^A-Za-z0-9' ]/"
But when I remove it to be:
"/[^A-Za-z0-9 ]/"
This script inserts records into the database.
What am I trying to do?
I have a textarea on a form. For each keyword on a new line, I explode based on the \n. I want to remove all non-letter and non-number characters from the keywords but don't want apostrophes to be removed.
I basically want all keywords to be lower-case with leading, trailing and extra white spaces - 2 or more blank spaces trimmed down to one - removed along with any non-letter and non-number characters except for apostrophes.
You need to escape your keyword: $insertkeywords = "INSERT IGNORE INTO kwdb (keyword) VALUES ('".mysql_real_escape_string($keyword)."')";
Thank you clover. Without your help, it would have taken me many hours to figure that one out! I ended up using mysqli_real_escape_string instead of mysql_real_escape_string because mysql_real_escape_string says it is deprecated as of PHP 5.5.0. As a result, I had to change it up a bit but the below code seems to work great for me so far (in case anyone else runs across this).
$con = mysqli_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysqli_error());
}
else
{
echo "Connected Successfully!";
}
//select database
mysqli_select_db($con, "test");
$keywords = explode("\n",$_POST['keywords']);
//Inserting into Database
foreach($keywords as $key => $keyword){
$keyword = preg_replace("/[^A-Za-z0-9' ]/", " ", $keyword);
$keyword = preg_replace("/\s+/", " ", $keyword);
$keyword = strtolower(trim($keyword));
//$insertkeywords = "INSERT IGNORE INTO kwdb (keyword) VALUES ('$keyword')";
$insertkeywords = "INSERT IGNORE INTO kwdb (keyword) VALUES ('".mysqli_real_escape_string($con, $keyword)."')";
mysqli_query($con, $insertkeywords);
}
Scenario: I want to check if a given process is running, and if so then wait for it to close.
My Perl script is using WMI-->Win32_Process to get the list of running process. I am able to find the running process with the below code
my $objWMIService = Win32::OLE->GetObject("winmgmts:\\\\localhost\\root\\CIMV2");
my $sqry = "SELECT * FROM Win32_Process where Name = \"$processName\" ";`
where $processName is set in the script.
The above code works fine on desktops but the code works in a different way in terminal servers because it the above code pick's up other user's running process.
Question: How can I add a username/id filter in the above query? Is there any easy way to get the process for current user?
Got the solution (workaround at least)..in case any one is interested..
use Win32::OLE::Variant;
my $useridval;
my ($strUser, $strDomain) = ( Variant(VT_BSTR|VT_BYREF, '<undef>'), Variant(VT_BSTR|VT_BYREF, '<undef>') );
foreach my $objItem (in $colItems) {
$useridval = $objItem->GetOwner($strUser, $strDomain);
if (length($useridval) > 0) {
if ($useridval == 0 ){
print ("Process " . $objItem->{Name} . " id: " . $objItem->{ProcessId} . " session id: " . $objItem->{SessionId}) . " owner: " . $strUser->Value . "\n";
}
}
}
Edit: added modified code as the simple variable didn't work. I've removed all error checking to keep it simple
thank you all
I used to work with array in VBSCRIPT like below… I am not sure how I should do it in PowerShell… Can any help me out…?
CODE --> VBSCRIPT
dim arrErrors(12)
arrErrors(0) = "APP0"
arrErrors(1) = " APP1"
arrErrors(2) = " APP2"
arrErrors(3) = " APP3”
arrErrors(4) = "APP4"
arrErrors(5) = "APP5"
arrErrors(6) = "APP6"
arrErrors(7) = "APP7"
arrErrors(8) = "APP8"
arrErrors(9) = "APP9"
arrErrors(10) = "APP10"
arrErrors(11) = "APP11"
arrErrors(12) = "APP12"
for i = 0 to ubound(arrErrors)
strError = arrErrors(i)
if (left(lcase(strErrorLine), len(strError)) = lcase(strError)) then
objErrorLog.WriteLine strErrorLine & vbTab & strComputer & vbTab & "Found Error number" & vbTab & i
exit for
end If
Create a hash table. Populate it with error names and values. Parse the error string and look if the hash table contains it. Like so,
$htErr = #{ "app0" = 0; "app1" = 1; "app2" = 2 } # Populate hash table
$error = ... # get the error string from somewhere.
if($htErr[$error]) {
"Found error, code " + $htErr[$error] # Get code based on error string
}