-2

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)
  • Start by eliminating the use of SendKeys. That's not a reliable way to manage shutdowns or restarts. – LesFerch Oct 24 '22 at 04:08
  • 1
    Why don't you have a simple batch file that executes `shutdown -a` and eliminate all the extra code? There's no need at all for VBScript to execute a single line of code in batch. – Ken White Oct 24 '22 at 04:51
  • 1
    There is neither a VBScript nor a batch file needed to shutdown Windows at a specific time by the Windows Task Scheduler. The Windows Task Scheduler can do that itself with an appropriate configured scheduled task. – Mofi Oct 24 '22 at 06:46

1 Answers1

0
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
Lundt
  • 142
  • 1
  • 1
  • 3