Search code examples
vbscriptpsexec

Run Psexec from vbscript to disable scheduled task on Windows 10 and hide console window


I need a vbs script that will run through a scheduled task. The vbs script will be used to disable windows update scheduled tasks, which require special privileges, using Psexec. Psexec.exe is located in the same directory as the vbs script. Trying to do it through vbscript hoping Psexec.exe console would not be visible. I Can do it by using cmd script, but can not hide Psexec.exe console window, which I don't want to be visible as I don't want users/anyone to see something is launched. The console shows briefly and quits, but it is still visible.

%~dp0psexec.exe -i -d -s -accepteula schtasks /change /tn "Microsoft\Windows\WindowsUpdate\sihpostreboot" /disable

I tried but could not make Psexec disable the task through vbs script at all, with the console window visible or not.

Option Explicit
Dim fso
Dim GetTheParent
Dim vbhide
Set fso = CreateObject("Scripting.FileSystemObject")
GetTheParent = fso.GetParentFolderName(Wscript.ScriptFullName)
Dim objShell
Set objShell = Wscript.CreateObject("WScript.Shell")
objShell.Run GetTheParent & "\psexec.exe -i -d -s -accepteula schtasks /change /tn \Microsoft\Windows\WindowsUpdate\sihpostreboot /disable", vbhide    

Solution

  • Eliminate psexec altogether. Use schtasks to create a task that runs schtasks via the System account, like this:

    If WScript.Arguments.length = 0 Then
      Set objShell = CreateObject("Shell.Application")
      'Pass a bogus argument, say [ uac]
      objShell.ShellExecute "wscript.exe", Chr(34) & WScript.ScriptFullName & Chr(34) & " uac", "", "runas", 1
    Else
      Set oWSH = WScript.CreateObject("WScript.Shell")
      TR = "schtasks /change /tn '\Microsoft\Windows\WindowsUpdate\sihpostreboot' /disable"
      oWSH.Run "schtasks /create /ru system /tn CustomTask /tr """ & TR & """ /sc onevent /ec system /mo *[System/EventID=999] /f",0,True
      oWSH.Run "schtasks /run /tn CustomTask",0,False
    End If
    

    Note that the OnEvent entry is just a dummy event to allow us to create the scheduled task.

    Also note the single quotes in the TR = string. That's an Schtasks feature, not a VBScript feature. Schtasks converts those single quotes to double quotes when it creates the task, allowing for a command that has arguments with spaces.