-1

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    
Berti
  • 11
  • 5

1 Answers1

0

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.

LesFerch
  • 1,540
  • 2
  • 5
  • 21
  • Thank you LesFerch for your answer. Is your psexec.exe file in your system32 directory? I have mine in the the same folder as the vbs script. I need to use it from there. Still I copied the psexec.exe to my system32 file just to test, tried your code but it is not working for me. Maybe I need to register psexec.exe before using it? – Berti Mar 30 '22 at 03:08
  • Sorry, my fault. I forgot to run it elevated. It is working like a charm. Thank you. I really appreciate it. – Berti Mar 30 '22 at 03:17
  • Now the only problem is to hide the psexec.exe console window, if possible – Berti Mar 30 '22 at 03:18
  • I'm not aware of any way to prevent psexec from flashing on the screen, but do you need to run psexec? Usually psexec is only needed to run code on a remote computer. The sample script you provided is just running on the local computer. In which case, you can just run `schtasks` directly and then it will be hidden. – LesFerch Mar 30 '22 at 03:45
  • "Scheduled Start" task can be enabled/disabled by just using schtasks, but "sihpostreboot" won't. It needs special permissions. It is a problem for many users. We need psexec as it is the only way I have found to disable that task from a script. – Berti Mar 30 '22 at 04:40
  • The psexec console doesn't really bother me, but i don't want the others to see it flashing. – Berti Mar 30 '22 at 04:59
  • You are using Psexec to run schtasks via the System account. You can do the same thing by using schtasks to create a task that runs schtasks under the System account. See my revised answer. – LesFerch Mar 30 '22 at 06:36
  • In fact I use Psexec as part of "Action" in a task that does a lot of things. It checks if values are changed and reverts them back. That's why I don't want others to see that is happening. The flashing console betrays it when launched. I will try your revised solution and let you know. – Berti Mar 30 '22 at 08:27
  • You can use the script above as a template to run anything (that doesn't interact with the user) via the System account. Just change the line that starts with `TR =`. – LesFerch Mar 30 '22 at 14:29
  • I used the task created from your revised script for sihpostreboot and added other similar that need System permissions. Also added multiple triggers, the ones I had in the main task I mentioned above. Changing the owner to "System" of the main task didn't work for other "Actions". It works only with "Administrators" as owner. I have two "main" tasks now and is working perfectly, thanks to your revised script. Thumbs UP! – Berti Mar 31 '22 at 08:38
  • For the "main" task I used before, mentioned above, that did not work changing owner to "system", the problem comes from the way I have created that task. LesFerch solution works for any kind of command or "Action" needed. – Berti Mar 31 '22 at 11:01