How to disable ECHO DIALOGS for Windows Script Host running a vbs file? - windows

Ran into a problem today. I have a Windows Server 2003 with a bunch of .bat files that essentially start .vbs scripts. Every time an ECH is used in the script I get that annoying dialog box that contains content of an echo and requires to click ENTER all the time. How can I just disable the dialogs and keep ECHOs in the command prompt window only?

Force it to run under cscript instead of wscript
If you are running the script manually, just put cscript in front of it.
Otherwise, this might give you an approach:
http://www.robvanderwoude.com/vbstech_engine_force.php

Redirect the output somewhere else, i.e.
scriptname.vbs > textfile.txt

Related

Activate Windows License

When I use slmgr /skms <URL> | cmd command in PowerShell it shows me a pop up that I must press the ok button after that I run slmgr /ato | cmd and again shows me another pop up that I press the ok button.
I don't want to press the ok button manually, can anybody help me if there is a solution for it, please?
I just disabled the uac in windows, but it does not help me.
What you are looking for is cscript. By just executing a vbs script file, output gets sent to dialog boxes, which need your interaction.
Using cscript, the desired output gets printed to your current stdout:
cscript C:\Windows\System32\slmgr.vbs /ato
That doesn't have anything to do with UAC and you might consider re-activating it.
But why is that?
slmgr is not a command nor is it an executable in windows. It's a script file written in VBScript. VBScript is a kind-of scripting version of VB, So it's good for your peace of mind not to deal with it too much.
If you type in slmgr /skms, what windows does is looking for a file named slmgr in its search paths (%PATH%), finding C:\Windows\System32\slmgr.vbs and deciding that, as its a .vbs file, executing wscript.exe with the file path and your arguments as parameter is the right thing to do.
WScript is the default interpreter for vbs files and just interprets the file and executes its code. On the other hand, there is cscript for console scripts.
If the author of the .vbs file decides to write a message to the user of their script, they usually use a statement like
Wscript.Echo "Hello, World!"
And thats where your confusion starts:
Executing this script in cscript means that Hello, World! is written to the console. (That's what you want to do)
Executing the same script using wscript renders a message box with a OK button. You can easily reproduce it yourself by creating a vbs file with the above statement.
The difference between cscript and wscript is also discussed in this question:
Difference between wscript and cscript

Windows Task Scheduler Doesn't Run VBScript

I am trying to automate a VBScript by using Windows Task Scheduler. However, I tried to use cscript.exe + "C:\...\script.vbs" but it didn't run. I also tried to directly run the same command in CMD (cscript.exe "C:\...\script.vbs") and it worked.
What might be the problem?
EDIT :
I just tried to switch the setting to "Run only when user is logged on" from "Run whether user is logged on or not" and it worked. I am wondering if there is a way to make my task scheduled run even when the user is logged off.
After hours of research, one of Blake Morrison (from Microsoft)'s blogs came up; it mentioned that
If running a .vbs / .ps1 script, try launching it from a .cmd / .bat script
see Help! My Scheduled Task does not run…
That blog also explains a lot of rules/tips when using Task Scheduler.
So please create a .cmd/.bat file that calls for your VBScript. For example: cscript.exe YourScript.vbs would be in your .cmd/.bat script.
Write a batch file like this:
echo "Started!" > c:\foldergoeshere\log.txt
cscript.exe "C:\...\script.vbs" > c:\foldergoeshere\log.txt
echo "Stopped!" > c:\foldergoeshere\log.txt
Then schedule the batch file instead of the vbs. That will allow you to see what is happening that is preventing it from running. Any error that you would have seen executing in the console (CMD), will be instead output to that log file between "Started!" and "Stopped!"
What's the hassle all about? I don't use .cmd/.bat and script works! (Windows7 here)
My VBS script (as a scheduled task) runs well on any scenario of these 4:
cscript and option "Run only when user is logged on"
cscript and option "Run whether user is logged on or not"
wscript and option "Run only when user is logged on"
wscript and option "Run whether user is logged on or not"
It's only that on the 1st scenario I encounter the black command window flashing on my screen.
Action settings:
or
My script, which simply creates a file:
Set objFSO = CreateObject("Scripting.FileSystemObject")
filename = "C:\Temp\" & Hour(Time) & Minute(Time) & Second(Time)
Set objFile = objFSO.CreateTextFile(filename)
Greg answered this https://superuser.com/a/816073
Basically you need to create 2 folders:
You have to create a folder (or two on a 64bit-windows):
(32Bit, always) C:\Windows\System32\config\systemprofile\Desktop
(64Bit) C:\Windows\SysWOW64\config\systemprofile\Desktop
Fixed the issue for me (and I could point to the .vbs file, not bat needed).
The .vbs file is running invisibly, which is a consequence of running it with the 'logged on or not' option.
You will not be allowed to interfere with a user using the computer, which means you will be able to help yourself, but not others.
Please read the following text from the Task Scheduler Help menu:
Task Security Context
You can specify that a task should run even if the account under which the task is scheduled to run is not logged on when the task is triggered.
To do this, select the radio button labeled Run whether user is logged on or not.
If this radio button is selected, tasks will not run interactively.
To make a task run interactively, select the Run only when user is logged on radio button.
You can do this.
cmd as the Program.
/c start cscript //nologo "W:\Pathto with spaces\VBScript.vbs"
/c send the following to the cmd
start starts the next parameter
cscript //nologo "W:\Path with spaces\VBScript.vbs" self explanatory. Path wrapped in quotes to allow spaces in the filename.
Screenshot
Have experienced more than once that a VBScript running as planned task worked fine for months and years but suddenly would not work any more despite nothing was changed. Have tried to reactive the task using all the recipes given here and elsewhere, but no success. My workaround was to create a new planned task with all settings copied from the original one.
I tried this on Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
AKA Window 7, But in my case i first cd to the dir and then ran the .bat file to execute the .vbs file
Go to start menu
Search for run
Once it opens, Type "shell:startup" and press Enter
You'll be directed to the Start Up folder. In this folder you can put create a .bat file which you want to execute the .vbs file (You'll do this in a special way)
cd C:\the\path\were.vbs\file\located && wscript.exe start_app.vbs
What your doing is cd to the directory cd C:\the\path\were.vbs\file\located
And then run the .vbs file wscript.exe start_app.vbs

How to start a program with admin privileges in a batch file

How do I start/call a batch file from another, but with administrative privileges, so that it doesn't give me errors like the following?
Access is denied error code 5
Here is something like what I would like it to be.
echo PLEASE TYPE YOUR USERNAME AND PASSWORD IN THE FIELDS BELOW.
echo.
echo.
echo.
echo.
set /p u=Username:
echo.
set /p p=Password:
start next.bat %u% %p%
No matter which way you chose, You must accept run it with admin privilege, so the point is which way is shorter? You could Right Click > Run as administrator as jean-Michael said although I prefer james approach (using vbScript) but if you don't want to use another vbScript file and just want benefit of just click on batch file and accept run with admin privilege (Note you have one right click and left click less) I suggest you this:
create a shortcut from your batch file and right click on shortcut > Properties > Shortcut Tab > Advanced now check the Run as administrator check-box. every time you execute it from the shortcut you just have one click to accept run it with admin privilege.
Hope this help.
Sometimes third party utilities like AutoIt (see runas function) are not an option - but if you do have that option, check it out as that will let you do exactly what you're aiming to. You can then call the AutoIt script from your script and use its runas function.
Windows runas doesn't support providing a password unless you're happy with the /savecred option - which is fine if you're only running the task from a single computer. The first time it will ask you for a password, but after that it won't (though you still have to use /savecred option each time you use it). I've got a feeling using this could be a huge security hole. But since it seems this is for your own machine, in your batch use this:
runas /user:computername\username /savecred yourcommand.exe
Another way is to make a scheduled task that can be called by your script. You can make it using the GUI or from an elevated command line as described here.
You can then call it from your script like this:
SCHTASKS /Run /TN yourtaskname
Simply put the bat file into the Windows directory, and it will run as administrator. I tried this myself, and it worked:
C:\Windows\batch_file.bat
It should work like that.
Within the batch file itself there is no way to run as an administrator, however if you launch the batch file from within a .VBS file, you are able to specify a 'runas' parameter.
set shell=CreateObject("Shell.Application")
shell.ShellExecute "your_batch_file.bat",,"C:\path\to\thedirectory", "runas", 0
set shell=nothing
This will launch your batch file as an administrator, and you can enable or disable the shell display (this example hides it as i wanted my program to run in the background without being seen).
Right click -> Run as administrator.
I think microsoft made as much as they could to prevent batch script to get administrative privileges on their own.
#cmd, I posted an example (How can I test effective permissions of a user from a batch script?) to run another bat file with ShellExecute and elevated rights (only when it's needed).
Take a look if it's what you looking for and what you need. If not, let me know and we could adapt your script to make it work.
good luck
I actually joined just to answer this, the simplest way by far is to create a shortcut to the program you want to run, then set the shortcut to run as administrator and just call the shortcut from the batch file. This will run with the settings specified in the shortcut and you could place this shortcut in the same folder as your batch file or just call it from the start menu.
Example:
"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Folder\Shortcut Name.lnk"

Voodoo with DOS Batch files

I've created a simple batch file that kicks off my *.msi installer within our company, creating a log file of the process, then displays the log file after the installer has completed.
installAndLog.bat:
msiexec.exe /i "\\FileServer2\setup.msi" /l*v "C:\setupLog.txt"
"C:\setupLog.txt"
It works, but there are two (2) glitches that annoy me:
The black console box shows in the background the whole time the installer is running and the log file is being displayed. Q1: How do I hide that?
and
The console box will not close until the log file is no longer being viewed (i.e. notepad.exe is closed). Q2: Can I call the text file in a new process and simply exit?
I was a DOS lover back in the day, but that was too many years ago.
I don't think you can hide the console window when running a batch file. However you can use vbscript instead which will by default not create a console window.
Take the below and put it in a file with a .vbs extension:
Dim wshShell
Set wshShell = CreateObject("WScript.Shell")
wshShell.Run "msiexec.exe /i ""\\FileServer2\setup.msi"" /l*v ""C:\setupLog.txt""", 1, true
wshShell.Run "C:\setupLog.txt"
All the double double quotes are there because the entire command must be surrounded by "'s and doubling them escapes them. The the documentation for WshShell.Run for more info.
Q1 - AFAIK you can't really hide the console window.
Q2 - Use the start command. This will launch the specified program (notepad) outside of the shell. It will also prevent the shell from waiting until the application closes to continue processing the batch script.
You might be better off changing the batch script to launch the MSI installer using the start command and having the installer launch notepad to view the log file once installation is complete.
If you really want to get these batch windows away, you'll have to switch over to something else. One simple alternative could be one of the scripting languages supported by the windows scripting host.
Or you try HTA (HTML applications) see here and here.
Run the dos script as a different user by scheduled task or as a service.

How to change cscript.exe to wscript.exe execution for all VBScript files?

After installing the new server, I am facing an issue.
I have lot of .vbs files, all need to run in wscript, reason, I use all those command like WScript.Echo "hello"
I want to be able to see the output when I double click the VBScript file.
But when I right click on the vbs file, I see console, I want to change the default to Windows host, globally!
How can I do that?
You can change the default scripting host to wscript.exe like so:
wscript.exe //H:wscript
If you wish to set cscript as the default host, that works in the same way:
wscript.exe //H:cscript
You can execute cscript.exe with the same arguments for the same result.
You can switch the default script engine with:
wscript //H:Wscript
Good luck!
I was able to solve it by using the following steps:
selecting the VBScript file that I want to open,
right click to select default program for this,
browse to C:/windows/windows32/wscript.exe, and select this.
In command prompt (as administrator):
To set windows script host as default script host enter:
wscript.exe //H:WScript
To set command line based script host as default script host enter:
cscript.exe //H:cscript
Check the Windows Explorer settings for the filetype *.vbs (something like tools->options->file types etc.) and change the "open with" setting to cscript.
Edit: I now advise caution with the recommendations I give below. After continuing to toggle and test my settings, I find I am unable to re-establish cscript as my default script host. (Note that I also retried the procedures given by other answers to this question.)
In addition, I tried using Process Monitor (a.k.a., "ProcMon") to find the reason for my difficulties, but unfortunately have not been unsuccessful.
Finally, I also considered going back to an earlier Windows 7 restore point, but this was complicated by the fact that I just yesterday changed my domain password. So, for now, I'm going to have to put my investigation to rest as other tasks are pressing.
On last thought...I have also considered the possibility that there are network policy security settings that are thwarting my efforts.
Original Answer: Enter the following line in a Windows batch file:
ftype VBSFile="%%SystemRoot%%\System32\WScript.exe" "%%1" %%*
Then, run a Command Prompt as an Administrator and run the batch file.
I believe the above will make the change for all users on the system. To make the change for only the logged in user, do the following (on Windows 7):
Control Panel => Programs => Default Programs => Associate a file type or protocol with a program
Then, in the Name column, scroll down to .vbs and click Change program... in the upper right. Then choose one of the Recommended Programs. If you do not see Microsoft ® Windows Based Script Host, browse to the following file:
C:\Windows\System32\wscript.exe
The guy above who right-clicked to choose the default program was right, however the path should be: C:\Windows\System32\wscript.exe

Resources