简体   繁体   English

使用快捷方式的管理员权限在 UNC 路径上运行 PowerShell 脚本

[英]Running A PowerShell Script At A UNC Path With Admin Privilege From Shortcut

I have created a PowerShell script to install some Features On Demand for our system administrators throughout our company.我创建了一个 PowerShell 脚本来为我们公司的系统管理员安装一些按需功能。 I have created a shortcut for the script and selected "Run As Administrator" in the advanced options.我为脚本创建了一个快捷方式,并在高级选项中选择了“以管理员身份运行”。 When I run the script directly it runs but errors state:当我直接运行脚本时,它会运行但错误状态:

The requested operation requires elevation

If I run the script from the shortcut, UAC pops up to allow to run as administrator of which I hit Yes and then a Black CMD box pops up and then immediately closes.如果我从快捷方式运行脚本,UAC 会弹出以允许以管理员身份运行,我点击是,然后弹出一个黑色 CMD 框,然后立即关闭。 It doesn't display any part of my script and doesn't pause at the end like I have in the script.它不会显示我的脚本的任何部分,也不会像我在脚本中那样在结尾处暂停。 In the Target of the shortcut I have put:在我放置的快捷方式的目标中:

powershell.exe -ExecutionPolicy Bypass

Still has the same problem.仍然有同样的问题。

If I put the powershell.exe -ExecutionPolicy Bypass into the script it opens an Admin Powershell window on top of the original window but the script runs in the lower window.如果我将powershell.exe -ExecutionPolicy Bypass 放入脚本中,它会在原始窗口的顶部打开一个 Admin Powershell 窗口,但脚本在较低的窗口中运行。 I can only assume that it launches the admin window back at C:\\Windows\\System32我只能假设它会在C:\\Windows\\System32处启动管理窗口

Is there a way to run a command to start PowerShell as admin and go directly to my script on a UNC path?有没有办法运行命令以管理员身份启动 PowerShell 并直接转到 UNC 路径上的脚本?

Something link this:东西链接这个:

Start-Process Powershell.exe -Verb runAs -File "\\SERVER\\Scripts\\Script.ps1" Start-Process Powershell.exe -Verb runAs -File "\\SERVER\\Scripts\\Script.ps1"

Or is there a better way to get my shortcut to launch my scripts as an administrator to the script on the UNC path?或者有没有更好的方法让我的快捷方式以管理员身份启动我的脚本到 UNC 路径上的脚本?

After a bunch of research I found the issue.经过一番研究,我发现了这个问题。 Turns out I was correct in thinking the Administrative box was starting at C:\\Windows\\System32 and there was no parameter passing to the box to tell it what to write.事实证明,我认为管理框从 C:\\Windows\\System32 开始是正确的,并且没有传递给该框的参数来告诉它要写什么。 After some trial and error this is what I got to work and has done exactly what I wanted:经过一些试验和错误后,这就是我开始工作并完全完成了我想要的:

In the shortcut I removed the "Run As Admnistrator" option under Advanced.在快捷方式中,我删除了高级下的“以管理员身份运行”选项。 I added some values to the Target of the shortcut:我向快捷方式的目标添加了一些值:

powershell -ExecutionPolicy Bypass "\\UNC\To\Script.ps1"

This started a launch script which ran in user mode.这启动了一个在用户模式下运行的启动脚本。 To get the administrative functions done by the script I moved those command to a different script "AdminScript.ps1" and called to it from Script.pst1 like so:为了让脚本完成管理功能,我将这些命令移动到不同的脚本“AdminScript.ps1”并从 Script.pst1 调用它,如下所示:

Start-Process powershell -Argument "-ExecutionPolicy Bypass -noexit \\UNC\Path\to\AdminScript.ps1" -Verb runAs

This then launched the Administrative Powershell window and set the ExecutionPolicy to Bypass since it starts default as Restricted.然后启动管理 Powershell 窗口并将 ExecutionPolicy 设置为 Bypass,因为它默认启动为 Restricted。

From there I was able to complete the the installation of the FoD with administrative privileges.从那里我能够以管理权限完成 FoD 的安装。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM