vbs sometimes hangs during executing - cmd

I have vbs file myscript.vbs
Set objFSO=CreateObject("Scripting.FileSystemObject")
outFile="c:\temp\txt.txt"
Set objFile = objFSO.CreateTextFile(outFile,True)
objFile.Write "test" & vbCrLf
objFile.Close
Set objFSO=Nothing
when i executing it on Windows 7 x64 by calling
cscript //B //NOLOGO "myscript.vbs"
sometimes it not executing and in task manager there is cscript.exe proccess that does not terminate
also i try add //T:2 or use wscript but it behaves the same
this happens with other vbs scripts too
sfc /scannow found no errors
how can this be resolved?

this is problem of windows. it is tests with new windows image on virual (no antivirus and some other thing). Create new image of virtual windows and it is work great. windows for some "skynet" reason behaves like this

Related

Why does VBS always tell me file not found even though the file is right there? (At startup)

I have a .vbs file, which I have set to run at startup through regedit. Basically what the vbs does, is execute another program in the same directory (I will paste the vbs script below). Normally, the vbs script works great and everything is good. However, whenever the vbs script run at startup (i.e auto running right after the computer is booted), I always get a error message, telling me that Windows cannot find my file (i.e hello.exe), even though the exe file is right there.
I have tried setting a delay to the script, but that resulted in the same problem. I am extremely confused because everytime I run the vbs manually (like double click it), everything works fine, no problem.
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "hello.exe" & Chr(34), 0
Set WshShell = Nothing
The expected result is that the vbs script will just run normally, like how it runs everytime i manually launch it. The error message is "Line 2: File cannot be found", or something along the lines of that.
As Hackoo is getting to, use the full path to the EXE you are running:
WshShell.Run chr(34) & "C:\My Hello App\hello.exe" & Chr(34), 0
This will run the exe file if its in the same folder as your script.
strPath = Left(WScript.ScriptFullName, InStrRev(WScript.ScriptFullName,"\"))
WshShell.Run chr(34) & strPath & "hello.exe" & Chr(34), 0

Is there a way to start a program minimized with VBScript using WScript.Shell?

Here is some example code I have right now to launch an app:
Set objShell = Wscript.CreateObject("WScript.Shell")
objShell.Run """C:\Program Files\Handbrake\HandBrakeCLI.exe"""
I tried the following to launch the app minimized but it didn't work. I'm assuming this only works from a normal command prompt?
Set objShell = Wscript.CreateObject("WScript.Shell")
objShell.Run "start /MIN ""C:\Program Files\Handbrake\HandBrakeCLI.exe"""
I've also tried launching a shortcut (which just gave a null error and the script couldn't run) as well as trying to do a sendkeys to minimize it which didn't work at all.
This is from a VBScript running via cscript.exe by the way.
Does anyone know how I can start this app minimized within VBScript?
Check the docs and use the second parameter of the .Run method.
Evidence:
set s = createobject("WScript.Shell")
s.run "notepad", 2
starts Notepad minimized.

VBScript- Single line as administrator

Is it possible to use the shell.run command to run the specified program as an administrator?
for instance:
shell.run(cmd.exe) <---Run this as admin
shell.run(Notepad) <---Run this as normal
I know i can execute the script to run as admin but that means everything inside that script is executed as an administrator.
My other option was to seperate the scripts and run one as admin and include what needs to be ran as admin in that script, then call another script to run and run that one as normal.
Set oShell = CreateObject("Shell.Application")
oShell.ShellExecute "cmd.exe", , , "runas", 1
oShell.Run "nodepad.exe"
(Source: http://social.technet.microsoft.com/Forums/eu/ITCG/thread/310e57f9-2367-4293-9f97-53d0e077aacc)
(More Info: http://ss64.com/vb/shellexecute.html)
Windows (from XP through to Win7 at least) has a runas command which does what you need. See here for details.
So, instead of running cmd.exe, you would run runas.exe, giving cmd.exe as the program to run.
Try this:
Dim ObjShell
Set ObjShell = CreateObject ("WScript.Shell")
ObjShell.Run "runas /K (command goes here) "
ObjShell.Run "notepad.exe"

Windows script - run silent but wait for completion / return right code

Here's my situation:
I have a BAT file that takes a long time to run (1minute to 70 minutes)
I schedule it with Windows scheduler to run every 10 minutes
If it schedules again while it is still running, nothing happens (this is good)
My problem is that I need my BAT to run silently, and it doesn't. So, I want to launch it with a Windows script like the following:
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\Batch Files\syncfiles.bat" & Chr(34), 0
Set WshShell = Nothing
Unfortunately, when I schedule this script, it does the job but returns instantly, making Windows scheduler think the task is finished when in reality the BAT is just running off by itself somewhere.
Because of this, Windows will reschedule the job again 10 minutes later and make multiple run.
What I need:
Is there a way to tell the Windows script file to wait for the target of the .Run command to complete before progressing/exiting? Basically, I want it to act like I launched another thread and then called join on it, so it does the same thing the BAT would have, but without displaying the console window.
Other Solutions
Tell me any other way to get this BAT to execute silently (powershell commands, whatever) and I'll accept it as a solution as well. Just don't tell me to write a full on C++/C# appliation around it, that's overkill :)
Running: Windows Server 2008 R2
I think all you need is TRUE for the optional 3rd argument
WshShell.Run chr(34) & "C:\Batch Files\syncfiles.bat" & Chr(34), 0, TRUE
Here goes the madness...
Create a file invisible.vbs:
CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, True
Then in the task scheduler, call your script like this:
wscript.exe "C:\Batch Files\invisible.vbs" "C:\Batch Files\syncfiles.bat"
I tested it and this does really work: No command window, and the task is considered running until the batch script ends.
Credit goes to https://superuser.com/a/62646/40362.
For 2008 R2 make invisible.vbs have this content, and just execute it directly. It will run the BAT silently and wait for completion.
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "D:\IUpdateScript.bat", 0, true

Running a VBS within a msi setup

I am trying to create a setup for an application that I'm developing using the Visual Studio 2010 setup.
One of the things I need to do is run some exe programs.
I am using a custome action to run a VBS.
This the method that im using to execute:
Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute """c:\prog.exe""","-parm bla" ,"","",""
The problem with this is that I cant wait for the program to finish using this method.
So I tried using this method:
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "c:\prog.exe -parm bla",1,True
But is seems that when the MSI runs the script is dosnt have the WScript object.
So my question is can i somehow get acess to the WScript object from the MSI or is there some better way to do this?
Indeed, Windows Installer does not support WScript objects directly. Have you tried to use the "CreateObject" function directly?
Set objShell = CreateObject("WScript.Shell")
Yes you cannot use WScript object in scripts that are called by MSI. As a workaround what you can do is create a new custom action with Action = NewAction, type =38, Source = (blank) TArget = add the vb script file as TARGET by running the following commands
CScript WiTextIn.vbs mymsi.msi CustomAction NewAction Target YourVBscript.vbs.
WiTextIn file is located in C:\Program Files\Microsoft SDKs\Windows\v7.0\Samples\sysmgmt\msi\scripts
(PS: When you try to run VBScript it might fail because vbscripts are disabled and you might have to delete the key from registry and enable vbscript)
This is what I did in my vbs script to open a firewall exception for the service. I couldn't use the standard interactive pop-up for a service (that asks for permission to open the firewall), since it doesn't have a UI.
set oShell = CreateObject("WScript.shell")
oShell.run "cmd /C netsh advfirewall firewall add rule program=""C:\Program Files (x86)\foo\bar\prog.exe"" name=""my-service"" dir=in action=allow"
I added this vbs script to the "Commit" CustomAction of the Setup&Deployment Project, leaving the properties as defaults.
To debug problems with the vbs stage, I ran the msi from DOS using
msiexec /i mysetup.msi /L* install.log
Note that I originally used "Wscript.CreateObject" but that failed. This worked.

Resources