如何在任务计划程序上使用脚本选择“以最高权限运行”

如何在任务计划程序上使用脚本选择“以最高权限运行”

你好,我正在编写一些脚本来在任务计划程序中添加任务。但是我需要一个脚本来选择“以最高权限运行”。

示例代码:

Dim settings
Set settings = taskDefinition.Settings
settings.Enabled = True
settings.StartWhenAvailable = True
settings.Hidden = False

答案1

您可以使用PowerShell 中的-RunLevel Highest标志来完成此操作。New-ScheduledTaskPrincipal

例子:

# Set the scheduled task time and repitition
$TaskTime = New-ScheduledTaskTrigger -Daily -At 12:00

# Set  the task to run as a local administrator with highest level privileges
$TaskUser = New-ScheduledTaskPrincipal -GroupId "BUILTIN\Administrators" -RunLevel Highest

# Set actions the schedule task should perform
$Action1 = New-ScheduledTaskAction -Execute "chrome.exe"
$Action2 = New-ScheduledTaskAction -Execute "notepad.exe"

# Registers the task with Task Scheduler
Register-ScheduledTask "Test Scheduled Task" -Action $Action1,$Action2 -Principal $TaskUser

PowerShell ScheduledTask 文档

相关内容