Solved

VBS script :  how to run cmd script in background using vbs script

Posted on 2014-04-21
23
3,873 Views
Last Modified: 2014-05-27
I want to run below script  -

C:\Oracle\FR\user_projects\domains\ClassicDomain\bin\startWebLogic.cmd

as background.

please advice ........
1- how to write VBS script
2- how to run the script
3- how to confirm that it is running

Thanks for your help --
0
Comment
Question by:mac_g
  • 9
  • 6
  • 3
  • +3
23 Comments
 
LVL 38

Assisted Solution

by:Aaron Tomosky
Aaron Tomosky earned 63 total points
ID: 40013499
save this as something.vbs

Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\Oracle\FR\user_projects\domains\ClassicDomain\bin\startWebLogic.cmd" & Chr(34), 0
Set WshShell = Nothing

to run, just double click like an executable

you can see it in task manager
0
 
LVL 23

Assisted Solution

by:ComputerTechie
ComputerTechie earned 32 total points
ID: 40013504
You could use task scheduler the call the command as needed.

Make sure you create an account that is allow to run as a batch job and it should work fine.

CT
0
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 281 total points
ID: 40013657
Hi, here is commented code, although there's not much to it.
Set objShell = CreateObject("WScript.Shell")
' Specify the file to run
strFile = "C:\Oracle\FR\user_projects\domains\ClassicDomain\bin\startWebLogic.cmd"
' Run the file hidden
objShell.Run strFile, 0, True

Open in new window


Run the script by double-clicking it, or in a Task Scheduler, run
wscript.exe C:\Scripts\RunCommand.vbs

While it is running, you should see a wscript.exe process in the Task Manager.

Rob.
0
 
LVL 21

Assisted Solution

by:dan_blagut
dan_blagut earned 31 total points
ID: 40014086
Hello

Or you can create a service from your cmd file.
here you have how to:
http://support.microsoft.com/kb/251192

Dan
0
 

Author Comment

by:mac_g
ID: 40014433
Thanks for all your support -

I could see process running from taskbar.

please let us know can I bring  this process to front , and see the status what is happening currently..
0
 
LVL 32

Assisted Solution

by:Robberbaron (robr)
Robberbaron (robr) earned 93 total points
ID: 40014487
see http://www.vb-helper.com/howto_shell_wait.html  which uses the Shell command and OpenProcess(SYNCHRONIZE, 0, process_id) to check its status.

i wrote a version called ShellSync but cant find it right now.
0
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 281 total points
ID: 40014505
I don't understand why you want to run the .cmd script hidden, but then still display progress of it?

It would be easier, and more reliable, to modify the exiting cmd file, adding echo statements in to show where it is up to, and just have that displayed normally.

Rob.
0
 
LVL 38

Assisted Solution

by:Aaron Tomosky
Aaron Tomosky earned 63 total points
ID: 40015291
I'm confused, do you want it hidden or not?
0
 

Author Comment

by:mac_g
ID: 40016686
any weblogic starting script shows the status - "RUNNING, .." at the end. this message convey services get started succesfully


by its nature, this script will be in RUNNING mode as long we want the welogic services  to be Up and to receive request from clients (without comming back to  cmd console )


my intention is

1- to confirm this status before it goes to background.
2- if above is not possible, then how to check status of the script even it goes background.

can either one above made possible .... please advice.

Thanks for your support so far....
0
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 281 total points
ID: 40016693
I'm still not sure I follow how the process works, or what it looks like, You could use VBScript to provide the interactive screen, but it wouldn't show anything more useful that what the startWebLogic.cmd shows.

If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
    strPath = Wscript.ScriptFullName
    strCommand = "%comspec% /k cscript  """ & strPath & """"
    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run(strCommand), 1, True
    Wscript.Quit
End If
Set objShell = CreateObject("WScript.Shell")
' Specify the file to run
strFile = "C:\Oracle\FR\user_projects\domains\ClassicDomain\bin\startWebLogic.cmd"
' Run the file hidden
WScript.Echo "Running startWebLogic.cmd"
objShell.Run strFile, 0, True
WScript.Echo "startWebLogic.cmd has ended"

Open in new window


If startWebLogic is only starting a system service, you could also have the script monitor the WebLogic service (if there is one) and let you know when it enters any state that is not "Running".

Rob.
0
 

Author Comment

by:mac_g
ID: 40016742
Dear RobSampson,
 
& all,

please refer to the screenshot  & above my last update explanation . this  will give more insight exactly what exactly i am looking for ..


Thanks for your help so far..
WL-script.png
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 281 total points
ID: 40016775
So you want something to tell you the service is running, but you don't want it to be the original startWebLogic.cmd file? If so, does my latest code help? The problem is, you can't use VBScript to read the text from that command file, so you can't say more than just "Running".  If you wanted to, you could modify startWebLogic.cmd, and cut out a few Echo statements that you don't want.
0
 

Author Comment

by:mac_g
ID: 40016813
my aplogies for your misunderstanding ...

I want to see script running forefront utill I see running mode-or- else , fixed time of 3min running script forefront (reason is -  sometimes startup fails) , then move it in hiding as service in background.


to put the above explanation in algorithm format...


OPEN ::

start : Open cmd Console  for weblogic start script
start : Run weblogic Script, keep this console open for some time (may be 3 min)
go     :  Hide cmd console in background as process automatically

END ::



hope this is clear now
0
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 281 total points
ID: 40019171
OK, so if you happen to have Microsoft Word installed on the machine that is running the service, this code will run the command file, wait for intTimeInSeconds, and then hide the command window that has the exact same title that you specify in strTitle.

Set objShell = CreateObject("WScript.Shell")
' Specify the title of the window to find
strTitle = "WebLogic Server"
' Specify the file to run
strFile = "C:\Oracle\FR\user_projects\domains\ClassicDomain\bin\startWebLogic.cmd"
' Specify the time in seconds to display the window
intTimeInSeconds = 180

dteStartTime = Now
objShell.Run strFile, 1, False

While DateDiff("s", dteStartTime, Now) < intTimeInSeconds
	WScript.Sleep 1000
Wend

Set objWord = CreateObject("Word.Application")
objWord.Visible = False
For Each objTask In objWord.Tasks
	If objTask.Visible = True Then
		If objTask.Name = strTitle Then objTask.Visible = False
	End If
Next
objWord.Quit

Open in new window


So first you need to know what the title of the command window is, put that in strTitle, and it should then work.

Regards,

Rob.
0
 
LVL 32

Assisted Solution

by:Robberbaron (robr)
Robberbaron (robr) earned 93 total points
ID: 40043682
possible better way that searches the output for the desired text.

my test batch file and vbs launcher attached.

Dim WshShell, oExec, sTmp
Set WshShell = CreateObject("WScript.Shell")

comspec = WshShell.ExpandEnvironmentStrings("%comspec%")
Wscript.echo comspec
' ////////////////////////////////////////////////////////

input = ""
Set oExec = WshShell.Exec(comspec & " /K c:\ee\StartWeblogic.bat")
wscript.echo "started2"

Do While oExec.Status = 0
  WScript.Sleep 10
     If Not oExec.StdOut.AtEndOfStream Then
          input = input & oExec.StdOut.Read(15)
          If InStr(input, "Server started in RUNNING mode") <> 0 Then Exit Do
     End If
     
     WScript.Echo(input)
Loop
WScript.Echo(input)
wscript.Echo "===================================="
wscript.echo "Service is in running state"
wscript.Sleep 15

Open in new window

LaunchWait.vbs
StartWebLogic.bat.txt
0
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 281 total points
ID: 40043690
Robberbaron, when you use the Exec method, it displays the window, so you would have two running here, and not be able to hide the original one, wouldn't you?  At least that's what happened to me when I tried to write it that way.

Rob.
0
 
LVL 32

Assisted Solution

by:Robberbaron (robr)
Robberbaron (robr) earned 93 total points
ID: 40043724
didnt open second window in my testing  (win7/64)....  thats why had to echo the test back to running window...

i ran it from a command window though....

updated with vbs to force cscript mode...
Dim WshShell, oExec, sTmp
Set WshShell = CreateObject("WScript.Shell")

' Specify the file to run
strFile = "C:\Oracle\FR\user_projects\domains\ClassicDomain\bin\startWebLogic.cmd"
strFIle = "c:\ee\StartWeblogic.cmd"   '''<<<TESTING


Sub forceCScriptExecution
    Dim Arg, Str
    If Not LCase( Right( WScript.FullName, 12 ) ) = "\cscript.exe" Then
        For Each Arg In WScript.Arguments
            If InStr( Arg, " " ) Then Arg = """" & Arg & """"
            Str = Str & " " & Arg
        Next
        CreateObject( "WScript.Shell" ).Run _
            "cscript //nologo """ & _
            WScript.ScriptFullName & _
            """ " & Str
        WScript.Quit
    End If
End Sub
forceCScriptExecution



comspec = WshShell.ExpandEnvironmentStrings("%comspec%")
Wscript.echo comspec
' ////////////////////////////////////////////////////////

input = ""
Set oExec = WshShell.Exec(comspec & " /K " & strFile)
wscript.echo "started2"

Do While oExec.Status = 0
  WScript.Sleep 10
     If Not oExec.StdOut.AtEndOfStream Then
          input = input & oExec.StdOut.Read(15)
          If InStr(input, "Server started in RUNNING mode") <> 0 Then Exit Do
     End If
     
     WScript.Echo(input)
Loop
WScript.Echo(input)
wscript.Echo "===================================="
wscript.echo "Service is in running state"
wscript.Sleep 15000   '25 secs
wscript.Echo "===================================="

Open in new window

LaunchWait.vbs
StartWebLogic.cmd
0
 

Author Comment

by:mac_g
ID: 40056988
I had tow scripts  
1) the below one script will run without params .. and it is working fine to me,
as expected .. going background

Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "C:\Oracle\FR\user_projects\domains\ClassicDomain\bin\startWebLogic.cmd" & Chr(34), 0
Set WinScriptHost = Nothing


2)  when I ran With parameter as below,  I am getting error message.

Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "C:\Oracle\FR\user_projects\domains\ClassicDomain\bin\startManagedWebLogic.cmd WLS_FORMS" & Chr(34), 0
Set WinScriptHost = Nothing


....
please advice how to fix this issue ..
Thanks
0
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 281 total points
ID: 40057096
Your second one needs the double quotes chr(34) taken back one word, otherwise WLS_FORMS is considered part of the executable to run.

 Set WinScriptHost = CreateObject("WScript.Shell")
 WinScriptHost.Run Chr(34) & "C:\Oracle\FR\user_projects\domains\ClassicDomain\bin\startManagedWebLogic.cmd" & Chr(34) & " WLS_FORMS", 0
 Set WinScriptHost = Nothing

Open in new window


But I am confused.  Why are you reverting to a two line script when Robberbaron and I have provided reasonably lengthy scripts as options?

Rob.
0
 

Author Comment

by:mac_g
ID: 40057103
Thanks  it works great ...

is it possible below two script to be in same VBS file , or does it need any customization

Thanks in advance ..
------------------------------------
Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "C:\Oracle\FR\user_projects\domains\ClassicDomain\bin\startWebLogic.cmd" & Chr(34), 0
Set WinScriptHost = Nothing

Set WinScriptHost = CreateObject("WScript.Shell")
 WinScriptHost.Run Chr(34) & "C:\Oracle\FR\user_projects\domains\ClassicDomain\bin\startManagedWebLogic.cmd" & Chr(34) & " WLS_FORMS", 0
 Set WinScriptHost = Nothing
----------------
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 281 total points
ID: 40057110
It will work as you have posted it, but if you want the second to wait until the first has completely finished, add
, True
To the end of the first .Run line.

Rob.
0
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 281 total points
ID: 40057113
This is slightly cleaner

Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "C:\Oracle\FR\user_projects\domains\ClassicDomain\bin\startWebLogic.cmd" & Chr(34), 0, True

 WinScriptHost.Run Chr(34) & "C:\Oracle\FR\user_projects\domains\ClassicDomain\bin\startManagedWebLogic.cmd" & Chr(34) & " WLS_FORMS", 0, False
 Set WinScriptHost = Nothing


Rob.
0

Featured Post

Do email signature updates give you a headache?

Do you feel like you are constantly making changes to email signatures? Are the images not formatting how you want them to? Want high-quality HTML signatures on all devices, including on mobiles and Macs? Then, let Exclaimer solve all your email signature problems today.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now