Executing two commands in VBscript - vbscript

I'm trying to write a VBScript to execute a two commands. The script is as follows:
Set objShell = CreateObject("WScript.Shell")
Set X = objShell.Exec("opcdeploy -cmd ""dbspicao -m 0703 -r 1"" -node fssflx24.fss.india")
Set Y = objShell.Exec("opcmsg a=a o=o msg_text=X severity=Normal node=fssflx24.fss.india")
strIpConfig = objScriptExec.StdOut.ReadAll
WScript.Echo strIpConfig
What I want is when the first command "X" gets executed, it's output shall used as the msg_text in the second "Y" command.
But it is not happening as when the second command gets executed it captures not the output but only word "Y".
What I'm missing.
Kindly assist.
BR,
Ramesh

Well there are a couple of problems.
First X is set to the Execobject not the output of the exec.
Second in the Y Exec you have just a string. To use a variable in a string you have to end the string after msg_text= with a " and then append the variable X and append finally the rest of the string. So the code would look like:
Set objShell = CreateObject("WScript.Shell")
Set execo = objShell.Exec("opcdeploy -cmd ""dbspicao -m 0703 -r 1"" -node fssflx24.fss.india")
X = execo.StdOut.ReadAll
Set Y = objShell.Exec("opcmsg a=a o=o msg_text=" & X &" severity=Normal node=fssflx24.fss.india")
Finally I cannot tell from your code what objScriptExec is supposed to be. It seems to be defined somewhere else so I don't know for sure how it is related...

My first command
opcdeploy -cmd ""dbspicao -m 0703 -r 1"" -node fssflx24.fss.india
it gives following alert.
Report For Database mpaydb1
Wed Mar 11 13:57:28 IST 2015
Metric UDM 0709 (Report 1)
RESOURCE_NAME CURRENT_UTILIZATION MAX_UTILIZATION LIMIT_VALU
processes 118 359 500
sessions 124 366 784
transactions 0 6 UNLIMITED
Second Command shall pick this output and generate alert using second command.
opcmsg a=a o=o msg_text=" & X &" severity=Normal node=fssflx24.fss.india
That is how it should work.
BR,
Ramesh

Related

Lua - popen 2x '&&' chained set /p - stdout takes a vacation

I'm working in an environment that has approximately zero functional ui while a lua script is running. This just isn't acceptable for the script I need to write, which relies heavily on user input. The only way I've found to circumvent this is using io.popen with some rather crafty commands.
I recognize that what I'm trying to do here is strange and very much wrong, and that I've brought this upon myself, but I can't figure out what's going wrong in this code snippet:
local a = 'f'
local p = io.popen(
'echo -~`~-,_,- Editing '..(a == 'f' and 'foreground' or 'background')..' -~`~-,_,- > con && '.. --display text to the user
'set /p f= Find block: > con < con && '.. --user input
'call echo %f% &&' .. --pass f back to the lua script
'set /p r= Replace with: > con < con && '.. --user input
'call echo %r% &&' .. --pass r back to the lua script
'pause < con', "r")
local f = p:read("*a") --read what was passed back, later parse it back into 2 variables
p:close()
What I expect to happen:
A 'command prompt window' is displayed to the user, asking for input.
The user enters 2 values.
The values are echoed back to the lua script as they are entered.
The values are read from the pipe and stored for later use.
The command line waits for a keypress, and then closes.
What actually happens:
A 'command prompt window' is displayed to the user, asking for input.
The user enters a value for f.
f is echoed back to the lua script.
The user enters a value for r.
r is echoed back to the console. (!!!)
f is read from the pipe. r is not present.
The command line waits for a keypress, and then closes.
This very similar code sample works just fine, but only returns 1 variable:
p = io.popen(
'echo What do you want to do? > con && '..
'echo G: remove girders > con && '..
'echo F: swap foreground > con && '..
'echo B: swap background > con && '..
'echo U: undo all edits > con && '..
'echo C: cancel > con && '..
'set /p a= Choose an option: > con < con && '..
'call echo %a%', "r")
a = string.lower(p:read("*a"):gsub("\n",""))
p:close()
What am I doing wrong, and how can I rewrite this to work for my purposes?
What in the world have I unleashed, and how do I put the genie back into the bottle?
After a good while googling, I found this:
(
echo some output
echo more output
)>"Your logfile
Redirecting Output from within Batch file
I had no clue you could wrap commands like that, and I've been tinkering with the Windows CLI since I first discovered it.
local a = 'f'
local p = io.popen(
'echo -~`~-,_,- Editing '..(a == 'f' and 'foreground' or 'background')..' -~`~-,_,- > con && '.. --display text to the user
'( set /p f= Find block: && '.. --user input
'set /p r= Replace with: ) > con < con && '.. --user input
'call echo %f% &&' .. --pass f back to the lua script
'call echo %r% &&' .. --pass r back to the lua script
'pause < con > con', "r")
local f = p:read("*a") --read what was passed back, later parse it back into 2 variables
p:close()
Wrapping the two set /p statements as above works - I get the expected output of f, followed by a newline, and then r, all sent back to the lua script, where they belong.
Still, if anyone can clue me in on why this was a problem in the first place, I would be very much interested in the explanation.
local p = io.popen(
-- create temporary .bat-file
'set tmpf="%TEMP%\\TMP%RANDOM%.BAT" &&'..
'cmd /c"(echo #set p=^%1&echo #set /p x= ^%p:~1,-1^%^>con&echo #call echo ^%x^%)>%tmpf%" &&'..
-- Your main program
'echo. -~`~-,_,- Editing '..(a == 'f' and 'foreground' or 'background')..' -~`~-,_,- > con &&'.. --display text to the user
'call %tmpf% "Find block:" < con &&'.. -- pass f back to the lua script
'call %tmpf% "Replace with:" < con &&'..-- pass r back to the lua script
-- delete temporary .bat-file
'call del %tmpf% > con &&'..
'pause < con', "r")
local f = p:read'*a'
p:close()

Waiting till the particular message is displayed on console

Here is my VBS code
Set wshshell = wscript.CreateObject("WScript.Shell")
Wshshell.run "C:\Temp\Executable.exe -c -dir C:\Productdir"
'Wait till "This will install the product on your computer. Press OK, Cancel" appears
WScript.Sleep 10000
WshShell.SendKeys "~"
Is it possible "rather than hard-coded sleep of 10 secs" to add something like this for e.g. if consolemessage="This will install the product on your computer. Press OK, Cancel" then WshShell.SendKeys "~"?
Can WScript.StdOut be used to capture the console messages in the above case? I was not able to do it.
You can read StdOut of a process when you execute the program using the Exec method.
Set wshshell = wscript.CreateObject("WScript.Shell")
Set p = Wshshell.Exec("C:\Temp\Executable.exe -c -dir C:\Productdir")
Do While p.Status = 0
output = ""
Do Until p.StdOut.AtEndOfStream
c = p.StdOut.Read(1)
WScript.StdOut.Write c 'write read characters to the command prompt
output = output & c
If InStr(output, "This will install the product") > 0 Then
'do stuff
Exit Do
End If
Loop
WScript.Sleep 100
Loop

capture command line output in vbscript

Got a simple script that executes a command to a server - briefly:
//Create shell
set WshShell=CreateObject("WScript.Shell")
WshShell.run "cmd.exe"
//send commands
WshShell.SendKeys "telnet IP_ADDRESS"
WshShell.Sendkeys "dir"
Server offers feedback which I want to capture. I just need to capture the first line into a variable, and then just print that variable out to confirm.
Can you help? Thanks.
Do not use the Windows telnet client for automation purposes. The telnet client that ships with Windows was made for interactive use only.
I'd use plink (from the PuTTY suite) in batch mode for this:
plink.exe -telnet -batch IP_ADDRESS dir
The tool doesn't require installation, so you can simply deploy it alongside your script.
Run it either in a batch file using head/tail, or in a VBScript using the Exec method, so you can read from StdOut:
addr = "62.39.x.x"
port = 24
timeout = 300 'seconds
timedOut = False
cmdline = "echo ""mute near get"" | plink.exe -telnet -batch " & addr & " -P " & port
Set sh = CreateObject("WScript.Shell")
'change working directory to directory containing script and plink executable
Set fso = CreateObject("Scripting.FileSystemObject")
sh.CurrentDirectory = fso.GetParentFolderName(WScript.ScriptFullName)
'wait until command completes or timeout expires
expiration = DateAdd("s", timeout, Now)
Set cmd = sh.Exec("%COMSPEC% /c " & cmdline)
Do While cmd.Status = 0 And Now < expiration
WScript.Sleep 100
Loop
If cmd.Status = 0 Then
cmd.Terminate
timedOut = True
End If
WScript.Echo cmd.StdOut.ReadAll
If cmd.ExitCode <> 0 Then
WScript.Echo "Command terminated with exit code " & cmd.ExitCode & "."
WScript.Echo cmd.StdErr.ReadAll
WScript.Quit 1
ElseIf timedOut Then
WScript.Echo "Command timed out."
WScript.Echo cmd.StdErr.ReadAll
WScript.Quit 2
End If
It might not be the best method, but worked for me:
Windows telnet command can save the output in client side using -f arguments. Therefore, you could use:
WshShell.SendKeys "telnet -f D:\output\telnet.out IP_ADDRESS"
and at the end of your script, simply process the content of telnet.out

Get part of string in Applescript

I try to make a applescript that read files in a folder and takes only part of the filename. The files would look like this: the.name.of.a.tv.show.s01e01
I could search for s01 but then i have to make a rule for every season that can come.
Is there some way to look for s--e-- and then take the part of the filename before that?
Try:
set xxx to "the.name.of.a.tv.show.s01e01 etc etc"
set yyy to (do shell script "echo " & xxx & " | sed 's/.s[0-9][0-9]e[0-9][0-9].*//'")
return yyy
Incorporating your previous question:
set seasonList to {}
repeat with aShow in listOfShows
set aShow to aShow as string
set end of seasonList to (do shell script "echo " & aShow & " | sed 's/.s[0-9][0-9]e[0-9][0-9].*//'")
end repeat
return seasonList

What's the equivalent of the bash in windows batch?

while [1 = 1]
do
eject
sleep 1
eject -t
sleep 1
done
And this is said to be the same:
watch -n 1 eject -T
What does it do?What's the equivalent in batch?
you can try this vbscript
Const CDROM = 4
Set objFS=CreateObject("Scripting.FileSystemObject")
Set shell = CreateObject("Shell.Application")
For Each drives in objFS.Drives
If drives.DriveType = CDROM Then
cd=drives.DriveLetter & ":\"
'Eject
End If
Next
Do
shell.Namespace(17).ParseName(cd).InvokeVerb("E&ject")
WScript.Sleep 1000 'in ms
Loop While 1=1
save as eject.vbs run it as
c:\test> cscript //nologo eject.vbs

Resources