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

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 --
mac_gAdmin - Oracle Fusion Middleware suiteAsked:
Who is Participating?
 
RobSampsonConnect With a Mentor Commented:
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
 
Aaron TomoskyConnect With a Mentor SD-WAN SimplifiedCommented:
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
 
ComputerTechieConnect With a Mentor Commented:
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
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

 
RobSampsonConnect With a Mentor Commented:
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
 
dan_blagutConnect With a Mentor Commented:
Hello

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

Dan
0
 
mac_gAdmin - Oracle Fusion Middleware suiteAuthor Commented:
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
 
Robberbaron (robr)Connect With a Mentor Commented:
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
 
RobSampsonConnect With a Mentor Commented:
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
 
Aaron TomoskyConnect With a Mentor SD-WAN SimplifiedCommented:
I'm confused, do you want it hidden or not?
0
 
mac_gAdmin - Oracle Fusion Middleware suiteAuthor Commented:
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
 
RobSampsonConnect With a Mentor Commented:
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
 
mac_gAdmin - Oracle Fusion Middleware suiteAuthor Commented:
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
 
RobSampsonConnect With a Mentor Commented:
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
 
mac_gAdmin - Oracle Fusion Middleware suiteAuthor Commented:
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
 
RobSampsonConnect With a Mentor Commented:
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
 
Robberbaron (robr)Connect With a Mentor Commented:
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
 
RobSampsonConnect With a Mentor Commented:
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
 
Robberbaron (robr)Connect With a Mentor Commented:
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
 
mac_gAdmin - Oracle Fusion Middleware suiteAuthor Commented:
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
 
RobSampsonConnect With a Mentor Commented:
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
 
mac_gAdmin - Oracle Fusion Middleware suiteAuthor Commented:
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
 
RobSampsonConnect With a Mentor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.