Link to home
Start Free TrialLog in
Avatar of gladmins
gladmins

asked on

Setting a scheduled tasks to run a vbs script

Hi there,

So I'm trying to set a scheduled task with the below in a batch file, when our end users log onto the system..  However its shutting the computer down with out pop up message to click on Yes to shut it down or No to cancel

schtasks /create /sc daily /st 15:05:00 /tn shutdown /tr "cscript c:\shutdown.vbs"

Could someone help me out please.

Regards


Option Explicit

Dim objNetwork, strComputer, objWMIService, colOperatingSystems
Dim objOperatingSystem, objShell, intReturn

Const SHUTDOWN = 1
Const POWEROFF = 8

Set objShell = CreateObject("Wscript.Shell")
intReturn = objShell.Popup("Click ""Yes"" to shutdown, or ""No"" to prevent shutdown", _
  20, "Shutdown Computer", vbYesNo + vbInformation)

If (intReturn = vbNo) Then
  ' User clicked "No", abort.
  Wscript.Quit
End If

Set objNetwork = CreateObject("Wscript.Network")
strComputer = objNetwork.ComputerName

' Shutdown the local computer.
Set objWMIService = GetObject("winmgmts:" _
  & "{impersonationLevel=impersonate,(Shutdown)}!\\" & _
    strComputer & "\root\cimv2")

Set colOperatingSystems = objWMIService.ExecQuery _
  ("Select * from Win32_OperatingSystem")

For Each objOperatingSystem in colOperatingSystems
  'objOperatingSystem.Reboot()
  objOperatingSystem.Win32Shutdown(SHUTDOWN)
Next

Open in new window

Avatar of gladmins
gladmins

ASKER

I should also add if I manually set the scheduled tasks I do get the prompts..
ASKER CERTIFIED SOLUTION
Avatar of Don
Don
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi gladmins,

What user is the scheduled task running as?  If it is SYSTEM then dstewartjr is on the right track.   Scheduled tasks created using schtasks that run as SYSTEM will always run non-interactively, so any prompts will not be displayed.  One solution then is to use AT as dstewartjr advises.

When you run schtasks yourself, using the commandline you posted, they run as the user that set up the task, i.e. you, so you then *would* see all the dialogs.

Regards,
Daz