I've been using this VBS to start a batch script hidden for over 2 years now with no issues:
Dim WinScriptHost
Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "C:\Dev\broker\boot\boot.cmd" & Chr(34), 0
Set WinScriptHost = Nothing
Two days ago I started my PC and this just stopped working. It calls the batch script but also shows a command prompt window. I did not fiddle with the OS in any way and I did not update it either. I'm on Windows 10 1903 build 18362.53 installed on 21th of March (a month ago).
What could have changed and how do I make the above script work again?
Related
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.
I have a script on a Server 2008 R2 VM which calls to the shell to robocopy a bunch of files into a local directory and then, several seconds later, checks if a certain 2.5 MB .msi exists. It kept returning false even though robocopy successfully completed and visible inspection reveals the file is there on time. I reproduced this bug on my local Windows 7 with a script:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWshShell = CreateObject("WScript.Shell")
Dim file : file = "LargeFile.exe"
objWshShell.Run "xcopy ""..\" & file & """ .", 1, True
WScript.Echo "EXISTS: " & objFSO.FileExists(file)
LargeFile.exe is an executable around 7MB. I place it one directory above my script, execute the script, and it prints EXISTS: False. I ran the script several times with this result before some switch flipped and it started reporting True consistently.
It still happens intermittently on my VM. It will work correctly several times, I'll do something else for a few minutes, and when I come back it stops working again.
What's going on here? I need to use the shell for copying because the network is iffy and causes objFSO.CopyFolder() to crash.
I was writing a vbs script which had an output to the console using the WScript.Echo command to let the user know what iteration the program was at. This worked fine until I was working on it again today when all of a suddent the script is echoing the message as a popup box. I stripped the code down to the bare bones and its still doing it and I am completely baffled.
Option Explicit
Call F
Sub F()
Dim TimeInterval
Dim CycleCount
Dim CycleCounter
TimeInterval=FormatNumber(WScript.Arguments(0),0)
CycleCount=WScript.Arguments(1)
CycleCounter = 0
For CycleCounter = 1 To CycleCount
WScript.Sleep(TimeInterval)
WScript.Echo "Iteration " & CycleCounter & " / " & CycleCount
Next
End Sub
I don't see it being the code that is causing it but rather the environment it is being run in but I'm not sure how I'd go about checking where to alter the settings or how the settings were even altered in the first place.
It will display a popup if you're using Wscript.exe to run your script. It will display in the console if you're using CScript.exe to run your script.
See this (among many other resources on the web): Should we use CScript.exe or WScript.exe to run .vbs files?
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.
ong story with code samples:
I'm doing automated test builds using vb scripting and I see the following line execute differently in 32-bit and 64-bit Windows 7:
CmdLine ("cmd /c C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe C:\Users\tester_kafka\Documents\VisualStudio2012\Projects\CFX_Manager_UITest\CFX_Manager_UITest.sln")
where CmdLine is a function like this:
Function CmdLine(strCommand)
Set WshShell = CreateObject("WScript.Shell")
Set WshShellExec = WshShell.Exec(strCommand)
Do While WshShellExec.Status = 0
WScript.Sleep 100
Loop
End Function
In the 32-bit Windows 7 system, the build completes and the vb script proceeds to completion. In the 64-bit Windows 7 system, the cmd window remains open until closed manually, then the script proceeds to completion. The goal is, of course, to automate the script in 64-bit to proceed automatically.
I have tried adding the & exit to the line, and the /nr:false switch to MSBuild.exe and messing with the quotes (not sure what else to try, frankly...). Should I be doing something simpler? And why should the OS make that much difference?
Any information would be welcome, even if it's not a solution. Thank you all.
Try this:
Function CmdLine(strCommand)
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run strCommand, , true
End Function