且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何运行计划任务以使用提升的权限停止和启动 s-s-rS 服务?

更新时间:2023-10-10 10:32:22

您收到该错误是因为函数 Invoke-Admin() 旨在为您要运行的程序传递参数具有较高的特权.如果您希望您的 powershell 脚本 s-s-rSScript.ps1 使用此 Invoke-Admin(),您可以将其转换为独立脚本.

You are getting that error because the function Invoke-Admin() was designed to have parameters passed for the program you wanted to run with elevated privledges. If you want your powershell script s-s-rSScript.ps1 to use this Invoke-Admin() you could convert it to a standalone script.

取没有函数声明和外括号的代码.将此文件保存为 Invoke-Admin.ps1

Take the code without the function declartion and outer brackets. Save this a file called Invoke-Admin.ps1

param ( [string]$program = $(throw "Please specify a program" ),
    [string]$argumentString = "",
    [switch]$waitForExit )

$psi = new-object "Diagnostics.ProcessStartInfo"
$psi.FileName = $program 
$psi.Arguments = $argumentString
$psi.Verb = "runas"
$proc = [Diagnostics.Process]::Start($psi)
if ( $waitForExit ) {
    $proc.WaitForExit();
}

创建后,您可以尝试使用以下内容提升您的脚本:

With that created then you could try to elevate your script with the following:

C:\*pathtoscript*\Invoke-Admin.ps1 -program "Powershell.exe" -argumentString "-file C:\s-s-rS_Script\s-s-rSScript.ps1"

您应该在那时获得提升提示,然后,一旦接受,将使用管理员权限使用您的脚本运行另一个窗口.

You should get the elevation prompt at that point and then, once accepted, will run another window with your script using admin rights.

这绝不是实现这一目标的唯一途径.

This is by no means the only way to accomplish this goal.

调度程序

你在标题中有这个,但在问题中并没有真正涵盖它.将此作为计划任务运行将不起作用,因为它需要用户输入.但是,您可以假设脚本在无人看管的情况下工作,只需使用脚本创建一个任务即可.

You have this in the title but dont really cover it in the question. Running this as a scheduled task will not work since it requires user input. You could however just make a task with your script as is assuming it works unattended.

常规标签

Run whether user is logged on or not
Run with highest privileges

操作 > 新建...

Action: Start a program Program/script: %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe 
Add arguments: -ExecutionPolicy Unrestricted -NoProfile -File C:\s-s-rS_Script\s-s-rSScript.ps1
Start in (optional): %SystemRoot%\system32\WindowsPowerShell\v1.0