2009-05-27 133 views
11

是否有可能提升PowerShell脚本的权限,以便没有管理员权限的用户可以运行该脚本?我们的网络管理员正试图找到更多省时的方法来完成目前他们必须使用远程桌面的特定任务......使用PS脚本自动执行这些任务将有所帮助,但用户没有管理员权限。Elevate PowerShell脚本

+3

所属的服务器故障。 – Richard 2009-05-27 13:03:23

+0

查看SF @ http://serverfault.com/questions/12985/elevated-powershell-script – Richard 2009-05-27 15:13:12

+2

是的,如果你看看发布该问题的用户......你会看到它是我。我张贴在那里因为你试图让我的问题在这里启动... – 2009-05-28 20:18:32

回答

8

的任务更像是setuid的比须藤......谢天谢地,setuid的可能:你可以简单地创建计划任务(没有设定的时间表),并将其设置为运行升高。然后,给你的用户执行该任务的权利。我前面概述了in a blog post以及PowerShell脚本,以帮助创建运行它们的任务和快捷方式。

问题(如JaredPar建议的)是,您必须确保您计划运行的应用程序提升或“以管理员身份”受到保护,尤其是如果您将运行脚本。确保只有管理员可以编辑或替换该脚本,否则你将王国的公钥放弃。

6

如果您正在使用V2,你可以使用高达上the PowerShell Team Blog

Start-Process "$psHome\powershell.exe" -Verb Runas -ArgumentList '-command "Get-Process"' 

以下这将运行“获取进程”以管理员身份。

如果你没有V2,你可以创建一个StartInfo对象并将这个动词设置为Runas。

function Start-Proc { 
    param ([string]$exe = $(Throw "An executable must be specified"),[string]$arguments)  

    # Build Startinfo and set options according to parameters 
    $startinfo = new-object System.Diagnostics.ProcessStartInfo 
    $startinfo.FileName = $exe 
    $startinfo.Arguments = $arguments 
    $startinfo.verb = "RunAs" 
    $process = [System.Diagnostics.Process]::Start($startinfo) 

} 

have a blog post,讨论更多的关于使用System.Diagnostics.ProcessStartInfo对象。

+1

如果用户不是管理员,则不起作用;) – Jaykul 2009-05-27 14:10:42

1

第一choco install pscx通过http://chocolatey.org/(你可能需要重新启动你的shell环境)

然后启用psxc

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser #allows scripts to run from the interwebs, such as pcsx 

然后使用Invoke-Elevated

Invoke-Elevated {Add-PathVariable $args[0] -Target Machine} -ArgumentList $MY_NEW_DIR