Trying to run a vbs script at a specific time [duplicate] - cmd

This question already has answers here:
How to shutdown pc in a specific time using vbs or batch script
(3 answers)
Closed 4 months ago.
I have to separate VBS scripts one that executes a showdown command and one that schedules that shutdown script.
Is there away to combine the two scripts to execute a specific time or how do I make the scheduling script compile.
The schedule script is:
schtasks /create /tn "Shutdown Script" /tr C:\Users\102117\Desktop\shutdown script\shutdown cancel script.vbs /sc daily /st 18:21 /ed 2100/01/01
The shutdown script:
Set WshShell = wscript.CreateObject("wscript.Shell")
WshShell.Run "cmd"
WScript.Sleep 100
WshShell.AppActivate "C:\Windows\system32\cmd.exe"
WScript.Sleep 100
wshshell.sendkeys "shutdown -a"
wshshell.sendkeys "{ENTER}"
WScript.Sleep 200
wshshell.sendkeys "exit"
wshshell.sendkeys "{ENTER}"
WScript.Quit(1)

Dim testResult AsSingle
Dim WMIServiceObject, ComputerObject AsObject
'Now get some privileges
WMIServiceObject = GetObject("Winmgmts:{impersonationLevel=impersonate,(Debug,Shutdown)}")
For Each ComputerObject In WMIServiceObject.InstancesOf("Win32_OperatingSystem")
testResult = ComputerObject.Win32Shutdown(0, 0) 'logoff
If testResult <> 0 Then
MsgBox("Sorry, an error has occurred while trying to perform selected operation")
EndIf
Next

Related

notepad loop created automatically with VBScript

I've created a VBScript to open notepad and write Hello. But it is opening notepad again and again
my code is:
WScript.Sleep 1000
Set WshShell=WScript.CreateObject("WScript.Shell")
WshShell.Run "notepad"
WScript.Sleep 100
WshShell.AppActivate "Notepad"
WScript.Sleep 500
WshShell.SendKeys "Hello"
WScript.Sleep 500
Did you name your script notepad.vbs? please change the name, or
WshShell.Run "notepad.exe"
If no full path specified, windows will try to find the program in the working directory first instead of %PATH%.

How to hit Enter in cmd from VBSCript

Trying to run few Commands from VBScript
Dim objShell
Set objShell = WScript.CreateObject ("WScript.shell")
objShell.run "cmd /c cd C:\Script & lcm_cli.bat -lcmproperty C:\LCMBiar_Import.property"
The Second command lcm_cli.bat -lcmproperty C:\LCMBiar_Import.property requires me to hit enter to complete its execution. Any idea how to do that through VBScript.
Tried giving
objShell.Sleep(1000)
objShell.SendKeys("{ENTER}")
but nothing happens. Still waits for manual hit of enter!
The solution is:
WshShell.SendKeys "{ENTER}"

How to delay in VB Script?

How can I delay a Vb Script?
The following codes did not work for me:
wscript.sleep 1000
Delay 10
Sub Delay( seconds )
Dim wshShell, strCmd
Set wshShell = CreateObject( "Wscript.Shell" )
strCmd = "%COMSPEC% /C (PING -n " & ( seconds + 1 ) & " 127.0.0.1 >NUL 2>&1 || PING -n " & seconds & " ::1 >NUL 2>&1)"
wshShell.Run strCmd, 0, 1
Set wshShell = Nothing
End Sub
dim oshell
set oshell = CreateObject("Wscript.Shell")
obshell.run "%windir%\system32\rundll32.exe kernel32.dll Sleep 5000"
The first statement, WScript.Sleep 1000, does work. Your error must be somewhere else.
Proof
Create a file test.vbs on your desktop:
WScript.Echo Time()
WScript.Sleep 2000
WScript.Echo Time()
Run as follows:
C:\Users\...\Desktop>cscript test.vbs
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.
11:31:34
11:31:36
Note the two second difference. (If this exact script produces a different outcome on your PC, please say so in the comments and accept my apology.)
You cannot use the WScript object in VB script custom actions. The WScript object is supplied by the script environment when you run it in Windows Script Host, and is not in the MSI environment. That means there is no way to do a delay, so maybe you could describe the problem you're having that the delay might solve.

WshShell.AppActivate doesn't seem to work in simple vbs script

total vbs scripting newb here. I'm trying to automate closing a certain open window, namely a program called HostsMan. This is on Windows 8.1 Pro 64 bit, and this is what my script currently looks like:
Set WshShell = CreateObject("WScript.Shell")
WshShell.AppActivate "HostsMan"
WshShell.SendKeys "%{F4}"
The second line doesn't seem to work. I know line 3 works because it activates the Windows shutdown menu. Is there something I'm missing?
Update/more info: Manually entering alt-F4 does close it, so I know this should work. I also tested this script with other open windows and they close just fine. Additionally, HostsMan is opened with Admin privileges, so I tried running the script as a task set with highest privileges to see if that would do it, and still no go. But that does work with other open windows running with Admin privileges. Frustrating!
I've tried it, too, and couldn't get it to work. There must be something about the window class, perhaps, where AppActivate doesn't see it as a top-level window?
In any event, AppActivate also lets you pass the process ID instead of the window title. When I installed HostsMan, the process name was hm.exe, so I'll use that in my example below.
Set Processes = GetObject("winmgmts:").InstancesOf("Win32_Process")
For Each Process In Processes
If StrComp(Process.Name, "hm.exe", vbTextCompare) = 0 Then
' Activate the window using its process ID...
With CreateObject("WScript.Shell")
.AppActivate Process.ProcessId
.SendKeys "%{F4}"
End With
' We found our process. No more iteration required...
Exit For
End If
Next
Alternative solution using WMIService (no loop through all processes required):
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * from Win32_Process WHERE Name = '" & ProcessName & "'")
If colItems.Count = 0 Then
WScript.Echo "Process not found"
Wscript.Quit
End If
For Each objProcess in colItems
WshShell.AppActivate(objProcess.ProcessId)
Exit For
Next
The key here is to put a small pause after the 'run' command but before the 'appactivate' command so the application shows up in the process list.
WshShell.Run "calc"
WScript.Sleep 100
WshShell.AppActivate "Calculator"
To solve the problem of AppActivate you have to use loop and if condition statement to check if the active windows is the target windows or not because sometime you deselect or the system deselect the target windows before execute the sendkeys directly so you got error.
I create this tight strict way
Set WshShell = CreateObject("WScript.Shell")
for i=0 to 300 ' this loop will continue about 30 sec if this not enough increase this number
Rtn=WshShell.AppActivate("HostsMan") ' HostMan have to be the windows title of application or its process ID
If Rtn = True Then
WshShell.SendKeys "%{F4}" ' send key to click ALT+F4 to close
wscript.sleep 100 ' stop execute next line until finish close app
Rtn=WshShell.AppActivate("HostsMan")
If Rtn=False Then ' using nested If to sure of selected windows is false because its close
Exit For ' exit for loop directly and execute what after for next
End If
End If
wscript.sleep 100
Next
Dim sh : Set sh =CreateObject("Wscript.Shell")
sh.Exec "calc.exe"
Wscript.Sleep 1000
sh.Exec "taskkill /f /FI ""WINDOWTITLE eq ""calc*"""
OR
sh.Run "taskkill /f /im calc.exe",0,False

Converting script to allow to run as scheduled task

I wrote a VBScript a while back to make a task easier. It works, however it wont run as a scheduled task since it requires a command window to interact with. How would I change the script to allow it to run as a scheduled task?
dim filesys
Set objShell = WScript.CreateObject("WScript.Shell")
set filesys=CreateObject("Scripting.FileSystemObject")
WScript.Sleep 2000
objShell.SendKeys"dsdbutil"
WScript.Sleep 2000
objShell.SendKeys("{Enter}")
WScript.Sleep 2000
objShell.SendKeys("activate instance wap")
WScript.Sleep 2000
objShell.SendKeys("{Enter}")
WScript.Sleep 2000
objShell.SendKeys("ifm")
WScript.Sleep 2000
objShell.SendKeys("{Enter}")
WScript.Sleep 2000
objShell.SendKeys("create full c:\adldsbackup")
WScript.Sleep 2000
objShell.SendKeys("{Enter}")
WScript.Sleep 30000
objShell.SendKeys("quit")
WScript.Sleep 2000
objShell.SendKeys("{Enter}")
WScript.Sleep 2000
objShell.SendKeys("quit")
WScript.Sleep 2000
objShell.SendKeys("{Enter}")
WScript.Sleep 2000
filesys.MoveFile "c:\adldsbackup\adamntds.dit", "\\vash\ibts_ts\Networking\Backups\ADLDS\adamntds_" & DatePart("m",Now()) & "_" & DatePart("d",Now()) & "_" & DatePart("YYYY",Now()) & ".dit"
objShell.SendKeys("exit")
WScript.Sleep 2000
objShell.SendKeys("{Enter}")
I have done something similar, scheduling a vbs. The solution is simple!
Create a batch file to run the vbs file and schedule this batch file. I prefer to use cscript in the batch files:
cscript.exe //nologo <ScriptNameWithFullPath>

Resources