本文主要是介绍如何定义需要海拔的PowerShell功能?,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
PowerShell can be extremely useful for a lot of everyday tasks as is, but if you need to tweak some functions with a bit of safety in mind, then how do you define a function so that it requires elevation? Today’s SuperUser Q&A post has the answer to a curious reader’s question.
PowerShell可以对许多日常任务非常有用,但是,如果您需要在调整某些功能时考虑到一点安全性,那么如何定义一个功能以使其需要提升? 今天的“超级用户问答”帖子回答了一个好奇的读者的问题。
Today’s Question & Answer session comes to us courtesy of SuperUser—a subdivision of Stack Exchange, a community-driven grouping of Q&A web sites.
今天的“问答”环节由SuperUser提供,它是Stack Exchange的一个分支,该社区是由社区驱动的Q&A网站分组。
问题 (The Question)
SuperUser reader Vlastimil wants to know how to define a PowerShell function that requires elevation:
超级用户阅读器Vlastimil想知道如何定义需要提升的PowerShell函数:
Since I cannot find any alternatives to Linux’s sudo elevation command, I have the following question. How do I define a PowerShell function that requires elevation, as in activating a UAC prompt on my Windows 8.1 Pro, 64-bit system? For example, say I run the following function:
由于找不到Linux的sudo海拔命令的替代方案,因此我有以下问题。 如何在Windows 8.1 Pro 64位系统上激活UAC提示时定义需要提升的PowerShell功能? 例如,假设我运行以下功能:
With the following results:
结果如下:
To be completely clear, if I run PowerShell as “user”, then run the aforementioned function system-check, I want the function to elevate in order to be able to execute the command (I want the UAC prompt to appear).
确切地说,如果我以“用户”身份运行PowerShell,然后运行上述功能system-check ,则我希望该功能升高以能够执行命令(我希望出现UAC提示)。
How do you define a PowerShell function that requires elevation?
如何定义需要提升的PowerShell函数?
答案 (The Answer)
SuperUser contributor Ashton has the answer for us:
超级用户贡献者Ashton为我们提供了答案:
To run a specific command from an elevated window:
要从提升的窗口中运行特定命令:
For example:
例如:
To run a specific script from an elevated window:
要从提升的窗口中运行特定的脚本,请执行以下操作:
To run an entire PowerShell session that prompts the UAC:
要运行提示UAC的整个PowerShell会话,请执行以下操作:
A function to return $True or $False if the current window is running with elevated permissions:
如果当前窗口正在使用提升的权限运行,则返回$ True或$ False的函数:
To ensure a script is only run As Admin, add this to the beginning:
为确保仅以管理员身份运行脚本,请将其添加到开头:
In PowerShell v4.0, the above can be simplified by using a #Requires statement:
在PowerShell v4.0中,可以使用#Requires语句简化上述操作:
Source: Run with Elevated Permissions [SS64.com]
来源: 以高权限运行 [SS64.com]
Have something to add to the explanation? Sound off in the comments. Want to read more answers from other tech-savvy Stack Exchange users? Check out the full discussion thread here.
有什么补充说明吗? 在评论中听起来不错。 是否想从其他精通Stack Exchange的用户那里获得更多答案? 在此处查看完整的讨论线程 。
翻译自: https://www.howtogeek.com/324064/how-do-you-define-a-powershell-function-that-requires-elevation/
这篇关于如何定义需要海拔的PowerShell功能?的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!