Wscript.Shell Run doesn't work consistently - vb6

I'm trying to run the following bit of code in a vb6 dll:
Dim objWSShell As Object
Set objWSShell = CreateObject("Wscript.Shell")
objWSShell.Run strPath & "test.bat", 0, True
The dll process gets hung up. The batch file will not run, no matter what its contents. I even tried an empty batch file and it still hung up. However, if I try this same piece of code, with this change:
Dim objWSShell As Object
Set objWSShell = CreateObject("Wscript.Shell")
objWSShell.Run "calc", 0, True
It works fine. I can't figure out why exe files work and bat files don't. Any ideas?

You don't need to use the shell scripting stuff, you can make things simpler & use the built in Shell() function:
shell environ$("COMSPEC") & " /C c:\xxx\yyy.bat", vbNormalFocus
Ditto for:
shell "calc", vbNormalFocus

You need to run cmd.exe and pass your BAT file to it.
objWSShell.Run "%COMSPEC% /c " & strPath & "test.bat", 0, True

I had a similar issue where batch files couldn't be run directly from WScript.Shell, but I didn't have access to modify the VBScript. It turns out there was a registry override on the .bat extension.
While using COMSPEC worked for me, deleting the registry key actually fixed more than just the WScript problem.

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.

VBS CurrentDirectory trouble

I have a very simple script, which only prints current directory. That's the code:
set WshShell = WScript.CreateObject("WScript.Shell")
Wscript.Echo (WshShell.CurrentDirectory)
This script is called from .exe file. It works fine until the calling executable was run directly. If I create a link to exe-file and launch it, then it runs my .vbs and it prints the directory of link, not the .exe itself! How can I fix this?
Get help from FileSystemObject, (vbscript example) :
scriptdir=CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
Wscript.Echo scriptdir
OK, maybe it's somehow clumsy, but I've discovered a workable solution. The idea is simple: get full script name and a short one. Then subtract the second from the first.
set WshShell = WScript.CreateObject("WScript.Shell")
Wscript.Echo (Left(WScript.ScriptFullName, Len(WScript.ScriptFullName) - Len(WScript.ScriptName)))

VB Using "Runas"

I am having a heck of a time getting some syntax correct here.
I have a small VB Script which prompts for credentials and then uses those credentials to run another vb script:
set objShell = WScript.CreateObject("WScript.Shell")
strAdminName = inputBox("What is your username (Domain\Username)")
objShell.Run "runas /user:" & strAdminName & " ""Wscript.exe \\xxx.xx.xxx\dfs\Tumw-IS\Juniper Tools and Utilities\Juniper Removal Tools\delete_folders.vbs"" "
When I run the script everything works, except it can't find the script I am calling. I get an error stating "There is no file extension in "\xxx.xx.xxx\dfs\tumw-is\Juniper"
Obviously the problem is in the fact that there are spaces in the file location and also that I am using a DFS link. When I run this and target a VBS file with no spaces in the location it works fine.
I just can't get the syntax down to handle the DFS link or the spaces in the name. I assume it's pretty straightforward, but I just can't get it to work.
Any ideas?
Thanks
-John
Try this:
objShell.Run "runas /user:" & strAdminName & " ""Wscript.exe \""\\xxx.xx.xxx\dfs\Tumw-IS\Juniper Tools and Utilities\Juniper Removal Tools\delete_folders.vbs\"""""

.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.

Calling a .vbs script from Excel VBA

Is it possible to call a .vbs script from a VBA code whenever needed? If possible then can you give me a sample code of how to do so?
To run a file:
Shell "wscript c:\null\a.vbs", vbNormalFocus
replacing wscript with cscript if the VBS wants to use the console.
Or you can add a reference to the Microsoft Script Control and interact with the VBScript runtime directly to execute VBS code, procedures etc;
Dim scr As ScriptControl: Set scr = New ScriptControl
scr.Language = "VBScript"
scr.AddCode "sub T: msgbox ""All Hail Cthulhu"": end sub"
scr.Run "T"
I only want to add to Alex' answer, that in some environments the object must be created in the following way:
set scr = CreateObject("MSScriptControl.ScriptControl")
If Alex adds this to his answer, I will delete this one.
Try somthing like this
ChDir ThisWorkbook.Path
Shell "wscript " & ThisWorkbook.Path & "\your.vbs", vbNormalFocus
It helped me.
You may want to use """ if your path name contains a - For me the following solved this issue:
Shell "cscript """ & ActiveWorkbook.Path & """\your.vbs", vbNormalFocus

Resources