How do I specify “Run with highest privileges” in VBScript?

我是研究僧i 提交于 2019-12-24 02:59:28

问题


When I use the GUI Task Scheduler, I can easily check the "Run with highest privileges" checkbox.

I found no such option in the VBScript command line too, however.

Is there a way to do that from the VBScript?

How to add this script this two feature?

Example VBScript: http://msdn.microsoft.com/en-us/library/aa383665%28v%3DVS.85%29.aspx

Privileges: http://msdn.microsoft.com/en-us/library/windows/desktop/aa382076%28v=vs.85%29.aspx


回答1:


This VBScript code automates the SchTasks.exe program and should demonstrate what you want to do. To get a list of the switches for creating tasks with SchTasks, you can run this:

schtasks.exe /create /?

You still have to run the below script from an administrative command-prompt to be able to create a task with "Highest" privileges. Also, if you wish to use an account other than the system account, you should use the /RP switch. If you're fully automating it, you may wish to use the /F switch as well to force overwriting an existing task. Otherwise it may hang while waiting for user input.

Option Explicit

Dim WshShell, strWinDir, strCmdLine, lngExitCode
Const OpenAsCurrentWindowIsOpened = 10, WaitForExit = True

Set WshShell = CreateObject("WScript.Shell")
strWinDir = WshShell.ExpandEnvironmentStrings("%WINDIR%")

strCmdLine = strWinDir & "\System32\SCHTASKS.exe /create /SC DAILY /TN ""My VBScript Task"" /TR """ & strWinDir & "\System32\calc.exe"" /RL HIGHEST /RU ""NT AUTHORITY\SYSTEM"""

lngExitCode = WshShell.Run(strCmdLine, OpenAsCurrentWindowIsOpened, WaitForExit)

If lngExitCode = 0 Then
  WScript.Echo "Success"
Else
  WScript.Echo "Failed with error code " & CStr(lngExitCode)
End If



回答2:


If you're asking how to interactively run VBScripts in admin mode, you just have to run CSCRIPT from a command-line that is already running in "Admin mode". For example, click Start, type cmd, wait a few seconds..., right-click cmd.exe and choose "Run as Administrator". Then when you run CSCRIPT from that command-line it will run with highest privileges already. Note you have to be logged-in with an admin user account already.

Or are you asking how you automate creating new scheduled tasks in VBScript with those options set?



来源:https://stackoverflow.com/questions/20009708/how-do-i-specify-run-with-highest-privileges-in-vbscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!