Windows Core运行命令(具有提升的特权) [英] windows core run command with elevated privileges

查看:128
本文介绍了Windows Core运行命令(具有提升的特权)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于标准用户,有一些选项可以以管理员身份(或任何其他用户)运行,但是,即使以管理员身份登录,某些功能也需要提升"运行.

There are a few options for standard user to run as Administrator (or any another user), however, even when logged as Administrator, some functions requires to run 'elevated'.

在Windows gui上,只需右键单击.exe并选择run as Administrator或什至提升'cmd'或'powershell'.

On a windows gui, just right click a .exe and select run as Administrator or even elevate 'cmd' or 'powershell'.

如何在Windows核心上获得提升的特权?

How can you get elevated privileges on Windows core?

推荐答案

通常,要在Windows上以编程方式调用具有高程的可执行文件(以管理员身份运行),请使用

Generally, to programmatically invoke an executable with elevation (Run as Administrator) on Windows, use the Start-Process cmdlet with -Verb RunAs.

这同样适用于pwsh.exe,PowerShell Core 可执行文件,因此在最简单的情况下,您可以编写:

This applies equally to pwsh.exe, the PowerShell Core executable, so that in the simplest case you can write:

# Open a new console window with PowerShell Core running with admin privileges.
Start-Process -Verb RunAs pwsh


如果您希望将其包装在便捷功能中,该功能也更健壮,并且在Windows上是跨版本 (在Windows中也可以使用) PowerShell):


If you wanted to wrap that in a convenience function that is also more robust and cross-edition on Windows (also works in Windows PowerShell):

function Enter-AdminPSSession {
  Start-Process -Verb RunAs (Get-Process -Id $PID).Path
}

# Optionally also define a short alias name:
# Note: 'psadmin' is a nonstandard alias name; a more conformant name would be
#       the somewhat clunky 'etasn' 
#       ('et' for 'Enter', 'a' for admin, and 'sn'` for session)
Set-Alias psadmin Enter-AdminPSSession

如果您希望该功能也可以跨平台(也可以在类似Unix的平台上使用):

If you want the function to also be cross-platform (to also work on Unix-like platforms):

function Enter-AdminPSSession {
  if ($env:OS -eq 'Windows_NT') {
    Start-Process -Verb RunAs (Get-Process -Id $PID).Path
  } else {
    sudo (Get-Process -Id $PID).Path
  }
}

重要:由于涉及的cmdlet/实用程序,

Important: Due to the cmdlets / utilities involved,

  • Windows 上,新会话总是在 new 控制台窗口中打开.

  • on Windows, the new session invariably opens in a new console window.

  • 新会话是管理员会话的事实反映在其窗口的标题(前缀Administrator:)

Unix (Linux,macOS)上,新会话总是在相同控制台(终端)窗口中打开.

on Unix (Linux, macOS), the new session invariably opens in the same console (terminal) window.

  • 在Unix上,没有明显的迹象表明已进入管理员会话.运行whoami是一种快速的测试方法(在管理会话中返回root);更好的解决方案是修改prompt函数以在提示字符串中反映管理会话.
  • On Unix there is no obvious indicator that an admin session has been entered; running whoami is a quick way to test for that (returns root in an admin session); a better solution would be to modify the prompt function to reflect an admin session in the prompt string.

如果您还希望能够在新会话中运行命令并有选择地自动关闭命令,那么还需要做更多的工作.

If you additionally want the ability to run commands in the new session and optionally auto-close it, much more work is needed.

如果您下载脚本Enter-AdminPSSession.ps1 (MIT许可的Gist),则可以运行以下命令:

If you download script Enter-AdminPSSession.ps1 (an MIT-licensed Gist), you can run commands such as the following:

# Example: Synchronously run an MSI installer with elevation
#          and exit on completion.
Enter-AdminPSSession -Exit { Start-Process msiexec -Args '/qn /i package.msi' }

# Check for success via $LASTEXITCODE
if ($LASTEXITCODE -ne 0) { Throw "Installation failed." }

此外,脚本:

  • 在交互式提升会话中使用[admin] 

确保呼叫会话的当前位置(工作目录)也是提升的会话的当前位置.

ensures that the calling session's current location (working directory) is also the elevated session's current location.

这篇关于Windows Core运行命令(具有提升的特权)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆