I want to write a simple vb script to automate shutdown in windows.
the code I am using is :
Dim ti
ti=InputBox("enter time in minutes")
ti=ti*60
Set objShell=CreateObject("WScript.Shell")
objShell.Run "cmd shutdown /s /t "& ti & " "
but when I enter the time and press enter , all I get is an command prompt window and nothing happens
I even tried by setting a default value for time and specifing the complete path for shutdown.exe ,but nothing seems to be working
Set WshShell = WScript.CreateObject("WScript.Shell")
Command = "C:\Windows\System32 shutdown.exe -s -t 600 "
WshShell.Run Command
can u please correct me and guide me towards the right code ....
It looks like you're missing a backslash in your path:
Set WshShell = WScript.CreateObject("WScript.Shell")
Command = "C:\Windows\System32\shutdown.exe -s -t 600 "
WshShell.Run Command
If you want to run commands in cmd you have to use either /k (keep cmd window open after command finishes) or /c (close cmd window after command finishes). Here's the canonical way to do this:
ti = InputBox("enter time in minutes")
ti = ti * 60
CreateObject("WScript.Shell").Run "%COMSPEC% /c shutdown -s -t " & ti
%COMSPEC% is a system environment variable with the path to cmd.exe.
Related
Can we write a VBScript to pass Y as an answer when asked during execution of command line. The scenario is as shown below.
Example output from command line
C:\Users>ctrk -h datafile
Do you want to copy the test generated files to inbox(y/n):y
Trying to generate beat file ...
Checking file creation.....
Heart file : C:\Program Files (x86)\FWI\data\beat.zip created
Press 'ENTER' to exit.
I tried the following solution
Set WSHShell = CreateObject("WScript.Shell")
WSHShell.Run "%comspec% /c "& " echo y | ctr -h datafile , 1, true"
But it is giving the following issue-
Example output from command line
C:\Users\>echo y | ctrk -h datafile
Do you want to copy the test generated files to inbox(y/n):Trying to generate beat file ...
Checking file creation....
Heart file : C:\Program Files (x86)\FWI\data\beat.zip created
Press 'ENTER' to exit.
Exception occurred. Refer to the error log for details.
Any help will be appreciated..
Try the following
Set WSHShell = CreateObject("WScript.Shell")
WSHShell.Run "%comspec% /c "& "ctr -h datafile , 1, false"
wait(2)
WSHShell.Sendkeys "y"
wait(2)
WSHShell.SendKeys "{ENTER}"
I have query related to this topic. Here is my script. I'm using below script to edit users on HP ILo board, it works fine, no error at all.
BUT here I can see cmd prompt, how do I run it in silent mode, i.e I do not want to see any cmd prompt, because I use BMC, so let it run in background, I will check the output later.
As you said I can simply change WScript to CSrcipt. But that does not work.
Any help please, please let me know where to modify.
Set wshShell = WScript.CreateObject ("WScript.Shell")
WshShell.Run "cmd.exe /v:on /k (set MYDIR=C:\Program Files\HP\hponcfg) & cd /d ""!MYDIR!"" & HPONCFG.exe /f Add_User1.xml /l log1.txt > output1.txt"
WScript.Sleep 1*60*1000
WshShell.Run "cmd.exe /v:on /k (set MYDIR=C:\Program Files\HP\hponcfg) & cd /d ""!MYDIR!"" & HPONCFG.exe /f Add_User2.xml /l log2.txt > output2.txt"
Set wshShell = Nothing
Wscript.quit
Regards
Use the second parameter of the .Run method
intWindowStyle Optional. Integer value indicating the appearance of
the program's window. Note that not all programs make use of this
information.
As I know nothing about BMC, I'd start with minimized (7, 6) before I'd try hidden (0).
Set oShell = CreateObject("WScript.Shell")
oShell.Run "cmd /c c:"
This line executes perfectly fine. Now I need to enter a text.
For example: c:\users> "abcd"
How do I go about it in the already opened cmd prompt.
You must add & after each command and change cmd /c to cmd /k
The first command is : CD /D c:\
The second command is : Dir
The third command is : ping 127.0.0.1
Try like this :
Set oShell = CreateObject("WScript.Shell")
Command = "cmd /K cd /d c:\ & Dir & ping 127.0.0.1"
oShell.Run Command,1,True
Hi I'm trying to run following command in VBA
Set wshShell = CreateObject("WScript.Shell")
wshShell.Run "cmd.exe runas some commands"
It's opening multiple command prompt windows.
If I run
wshShell.Run "cmd.exe"
Only this then it opens a single window.
Am i doing anything wrong in the 1st scenario.
Try using the /K switch and keep the quotes aroung cmd.exe before you run the rest of your commands
wshShell.Run " 'cmd.exe /K' 'commands here'"
When I execute a VBScript, the command window that it creates closes quickly before the user gets a chance to read the output. How can I get the window to stay open without modifying windows registry?
This is the code:
Set objShell = WScript.CreateObject("WScript.shell")
objShell.Run "SyncToyCmd.exe -R", 1, True
You can send your execution command through the cmd.exe command interpreter, along with a pause command which will give the user a Press any key to continue . . . prompt to close the window.
objShell.run "%comspec% /c ""SyncToyCmd.exe -R & pause""", 1, True
Or to keep the window alive, use the /k flag instead of /c:
objShell.run "%comspec% /k SyncToyCmd.exe -R", 1, True
But beware, your VBScript will not continue (or terminate) until this cmd window is manually closed.
The %comspec% environment variable refers to the correct command to open the command interpreter as per your operating system. On my XP machine, for instance, %comspec% is equal to C:\WINDOWS\system32\cmd.exe.
See cmd.exe documentation here: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/cmd.mspx?mfr=true
More info on the use of the & versus the && command separators here.
Assuming that it's the popped-up command window that you want to keep open (rather than the one running your VBScript), you can use CMD.exe's Pause command to achieve this:
Set objShell = WScript.CreateObject("WScript.shell")
objShell.Run "cmd.exe /C ""SyncToyCmd.exe -R & Pause"" ", 1, True
Make it sleep for a while, maybe tell the user it will close in 5 seconds?
Set WScript = CreateObject("WScript.Shell")
WScript.Sleep 5000