Running DOS commands from Vbscript .Help needed with HTA - user-interface

I have a vbscript that calls another from within itself. It works fine wen i run it from command line with cscript. For example the master vb script has a code such as
cmd /k cd "Path having the vbs" && cscript "slave_temp.vbs"
Now i have an HTA where the master vbs resides when i click on a button on the HTA this command is executed to call the slave.vbs
This is the command i use to call the slave.vbs
Set objShell = CreateObject("WScript.Shell")
runpath = "cmd.exe /K"& Chr(32) &"cd " & Chr(34)&final_path&Chr(34)&Chr(32)&Chr(38)&Chr(38)& Chr(32)& "cscript " &Chr(34)&Name1 &"_temp.vbs"&Chr(34)
objShell.Run runpath
Here final_path has the path name and Name1 has the file name of the slave.
I tried checking out the runpath value using Msgbox and it seemed all fine. But im dont understand why this doesnt work.
On a Final note the slave.vbs has SAP GUi automation code. and it throws this error when i use HTA to trigger
C:\path of slave\slave_temp.vbs(2, 4) (null): Invalid syntax
But the file executes properly when the slave_temp.vbs is executed separately. What can be the problem here.? Does this mean SAP objects dont have support wen run from HTA ?...
Help is much appreciated.
The following are the first few lines of the slave_temp.vbs having the SAP automation code
If Not IsObject(application) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(connection) Then
Set connection = application.Children(0)
End If
If Not IsObject(session) Then
Set session = connection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject application, "on"
End If
Sorry for the Long question. But i think the issue is when calling SAPGUI object from HTA. Kindly help me tackle this.

Related

Failure to run VBS script with task scheduler

we have this vbs script we use to update certain documents with SyncToy.
This is the script as it is currently written:
'--------------------------------------------------
Set oShell = CreateObject("WScript.Shell")
sFilePath = chr(34) & "C:\Program Files\SyncToy 2.1\SyncToyCmd.exe" &
chr(34) & "-R"
iRC = oShell.Run(sFilePath, 0, True)
' Return with the same errorlevel as the batch file had
Wscript.Quit iRC
'---------------------------------------------------
I didn't write this script, and I have very little experience with scripting.
I have a task set up in task scheduler that runs this script anytime the device connects to a network. The script should run SyncToy and then synchronize the folder pair that is set up. I have tried running the script through command prompt with the cscript command but nothing happens as far as I can tell. At least the folders aren't syncing.
The script is running on a Windows 10 pro tablet
I have verified that the task is indeed running when it is supposed to. I'm just not sure if it is an issue with the way the script is written or if the task settings need to be changed. Is there anything wrong with the script as far as you can tell?
I was unsure whether to post this here or over in serverfault. If this doesn't belong here please move the question over to serverfault
Update: I've verified that this isn't a problem with the script. This problem apparently arose only after the update from SyncToy 2.0 to 2.1.
Thanks Guys.
There is a error with the sFilePath lines.
First, I don't know if this was originaly on a single line but it should (or add "_" before changing line).
Then, this (...)& >"-R" would not work. The ">" symbole is outside the quotes and generate a error.
If you want to execute this command: "C:\Program Files\SyncToy 2.1\SyncToyCmd.exe" -R,
this is the way to do this:
sFilePath = chr(34) & "C:\Program Files\SyncToy 2.1\SyncToyCmd.exe" & chr(34) & " -R"
You can also add msgbox sFilePath to show a popup with the value of sFilePath.
To test/run the script, you just need to double-click on it.

Create shortcut which runs in minimized mode via script

How do I create a shortcut of a batch file and configure if to run in minimized mode? When I create a shortcut to a batch file I have to manually configure it to run in minimized mode manually. Any idea how do I write a script to change it to run as "minimized" mode
#npocmaka's shortcutjs.bat is a complete solution but it has about 200 lines. So, I have created a small VBScript for the purpose. You need to modify it according to your purpose.
'======PART 1: elivate to admin. required to save the batch file from part 2 in C drive
If Not WScript.Arguments.Named.Exists("elevate") Then
CreateObject("Shell.Application").ShellExecute WScript.FullName _
, WScript.ScriptFullName & " /elevate", "", "runas", 1
WScript.Quit
End If
'======PART 2: create the test batch file on the fly
Set objFSO=CreateObject("Scripting.FileSystemObject")
outFile = "c:\test.cmd"
Set objFile = objFSO.CreateTextFile(outFile,True)
objFile.Write "pause" & vbCrLf
objFile.Close
'=======PART 3: create the shortcut of the batch file
set WshShell = CreateObject("Wscript.shell")
strDesktop = WshShell.SpecialFolders("Desktop")
set oMyShortcut = WshShell.CreateShortcut(strDesktop + "\test.lnk")
oMyShortcut.WindowStyle = 7
OMyShortcut.TargetPath = "c:\test.cmd"
oMyShortCut.Save
Part 1 and 2 are optional and they are just to give an idea about what to do if you also want to create the batch file on the fly. Part 3 is the required code to create a shortcut using VBS.
You can run VBS script from cmd: cscript shortcut.vbs after you save the code above as shortcut.vbs
If you want to pass some argument about your batch file location, see this question, Can I pass an argument to a VBScript (vbs file launched with cscript)?
Then you can also use your code like cscript shortcut.vbs "C:\test.cmd" and reuse the same VBScript to create different shortcuts.
For other available options like adding an icon to your shortcut, adding hotkey support, setting Working Directory etc. please see this link
If I understand you correctly. You will need to use VB script to create shortcut. I don't believe batch script can create shortcut
https://support.microsoft.com/en-us/help/244677/how-to-create-a-desktop-shortcut-with-the-windows-script-host
see example 2: the WindowsStyle parameter define the windows size.
oMyShortCut.WindowStyle = 7 <-- 7= minimized.
Good luck
Binh
Try with shortcutjs.bat:
shortcutjs.bat -linkfile tst6.lnk -target "%cd%\myscript.bat" -windowstyle 7 -adminpermissions yes
-adminpermissions yes is optional if you want to run the bat as administrator. You'll need the full path to your script. possible modes are 1 for normal, 3 for maximized and 7 for minimized.

How to wait for Word Object to complete?

I'm trying to run a VBScript to do some operations within a Word file (heavy).
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open(fileName)
objWord.Visible = False
' Do heavy work here.
' Do heavy work here.
objDoc.SaveAs(file.ParentFolder & "/" & theBaseName & " _compiled")
When this script is called from the command line, there is no way to tell when it has completed as it runs "asynchronously".
How do I wait for process completion?
You seem to be running the script with wscript.exe. Use cscript.exe to run VBScripts synchronously from a console or batch script.
cscript.exe //NoLogo "C:\path\to\your.vbs"
Try WScript.Echo Now() at the top and bottom of the script; you'll see the code runs synchronously. When you get the second message box, instead of clicking OK, check for the file you're saving as; it will be present.

Can I pause execution of an application that was started by a VBscript?

I launch the Unified Functional Testing application and tell it to run a script:
Dim uftApp
Dim WshShell
Set uftApp = CreateObject("QuickTest.Application")
If NOT uftApp.Launched Then
uftApp.Launch
End if
uftApp.Visible = True
uftApp.WindowState = "Minimized"
uftApp.Open "C:\Users\smithjohn\Desktop\UFT Repository\Project 1\MyUFTScript", False
uftApp.Test.Environment.Value("ForTeam") = "TEAM A"
uftApp.Test.Run
uftApp.Test.Close
uftApp.Quit
Set uftApp = Nothing
This launches UFT and begins to run MyUFTScript. Since this script be can be quite long and take hours to run I want to be able to pause the execution. I've written another small vbscript file that looks like this:
Dim qtApp
Set qtApp = CreateObject("QuickTest.Application")
qtApp.Test.Pause
Set qtApp = Nothing
I then create a shortcut to that vbs file and give it a shortcut key, "cntl + alt + p". When I lauch UFT myself my pause script works perfectly, but when the top most vbs file is used to launch and run UFT my pause.vbs file wont run at all - I am not able to pause execution.
Am I doing something wrong?
Use GetObject instead of CreateObject. this should work for you.
Dim qtApp
Set qtApp = GetObject("","QuickTest.Application")
qtApp.Test.Pause
Set qtApp = Nothing

.vbs script won't run batch / how to run batch silently

I have a batch file that I want to run silently.
So, i was goolging around and come up with following code...
invMybatchfile.vbs
Dim curPath
curPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
Set WshShell = CreateObject("WScript.Shell")
cmds=WshShell.RUN(sCurPath & "\Mybatchfile.bat", 0, True)
This code DOES work on my desktop (Window 7 32bit), but for some reason, this script does not work on server computer(Windows Server 2008 R2). When I click invMybatchfile.vbs, dos windows pops up and closes right away and Mybatchfile.bat is not executed at all. (If i just run Mybathfile.bat on server computer, it works fine, so batch file is not the problem here.)
So my question is, does anyone know why I am having this problem?
OR
is there any other ways to launch batch file silently(not minimized, i know this way) with out using .vbs file or installing other software?
Thanks for your help
JB
The code of your example never will run in any machine because you are declaring the variable name as "curPath" and assign the vlaue to "CurPath" but you are trying to use it with the name "sCurPath" (And that variable doesn't exist on your code).
Also you don't need to set the current working dir because when you launch a file, the shell searchs for the file in the working dir, so this is all the code what you need, in only one line:
CreateObject("WScript.Shell").RUN "Mybatchfile.bat", 0, True
But if you are interested to store or to use the current working directory for any strange reason you have a method that returns the current dir:
.CurrentDirectory
Then you can use the method this way:
Set Shell = CreateObject("WScript.Shell")
Shell.RUN Shell.CurrentDirectory & "\Mybatchfile.bat", 0, True
...Or store the current dir in a var this way:
Set Shell = CreateObject("WScript.Shell")
CurDir = Shell.CurrentDirectory & "\"
Shell.RUN CurDir & "Mybatchfile.bat", 0, True
MSDN: http://msdn.microsoft.com/en-us/library/3cc5edzd%28v=vs.84%29.aspx
EDIT:
About running silent files, you can do it too using NirCMD commandline application.
NirCMD exec hide "Path to Batch File\Batch File.bat"
Official site: http://www.nirsoft.net/utils/nircmd2.html
I think you need to run it through cmd.exe:
WshShell.Run("%COMSPEC% /c " & CurPath & "\Mybatchfile.bat", 0, True)
Check similar questions here and here for more information.
Have you tried using RunAs?
cmds=WshShell.RUN("runas /noprofile /user:mymachine\administrator " _
& sCurPath & "\Mybatchfile.bat", 0, True)
Since it works on Windows 7 but not on Server 2008 R2, It sounds like a permissions issue to me.

Resources