PowerShell对很多日常任务都非常有用,但是如果您需要在考虑安全性的情况下调整某些函数,那么如何定义函数以使其需要提升呢?今天的超级用户问答帖子回答了一位好奇的读者的问题。
今天的问答环节是由SuperUser提供的,SuperUser是Stack Exchange的一个分支,是一个由社区驱动的问答网站分组。
超级用户读者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:
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函数?
超级用户贡献者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:
A function to return $True or $False if the current window is running with elevated permissi***:
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:
Source: Run with Elevated Permissi*** [SS64.com]
有什么要补充的解释吗?在评论中发出声音。想从其他精通技术的Stack Exchange用户那里了解更多答案吗?在这里查看完整的讨论主题。
学习编程的关键第一步是使用循环。谢天谢地,PowerShell将随着您的技能不断增长。 ...
文件加密和解密可以是一个工作位。但是,使用PowerShell扩展,可以将进程精简为一行命令。为此,我们需要安装Gpg4win和Powershell模块。使用脚本,我们可以自动化文件加密和解密过程。 ...
...“在这里打开命令提示符”的选项被替换为“在这里打开PowerShell窗口” ...
大多数用户从未听说过PowerShell,更不用说触摸它并试用了。这可能是因为这个名字,听起来像是只有铁杆IT极客才会使用的东西。或者是因为PowerShell的好处还不清楚。可能是因为最初的学习曲线。 ...
...示文件资源管理器的所有技巧,以及如何在命令提示符和PowerShell中使用它们。您甚至可以将“复制到”和“移动到”添加到文件资源管理器的上下文菜单中。 在Windows 10中复制文件或文件夹时,所选项目将被复制并保存在所选的...
PowerShell是一种比命令提示符更强大的命令行shell和脚本语言。自从Windows10发布以来,它就成了默认的选择,而且有很多方法可以打开它。 PowerShell的使用更为复杂,但它比命令提示符更加健壮和强大。这就是为什么它成为高级用...
使用Windows10的PowerShellcmdlet,您可以轻松地添加应用程序,以便在启动时自动触发VPN连接。自动化这个任务意味着当你使用某些应用程序时,你甚至不必考虑连接到VPN。 要利用PowerShell中的此功能,如果您还没有VPN服务,则必须...
...么是MD5、SHA-1和SHA-256哈希,如何检查它们? 在Windows上,PowerShell的Get FileHash命令计算文件的校验和。要使用它,首先打开PowerShell。在Windows 10上,右键单击“开始”按钮并选择“Windows PowerShell”。您也可以通过在“开始”菜单中...
...括项目符号列表、编号列表或字母列表。我们来看看。 如何在microsoft word中创建多级列表 在Word中创建多级列表非常简单。从输入列表中的一行或多行开始,列表中的每一项都在不同的行上。然后,选择这些行。 在功能区的“...