在 PowerShell 中获得管理员权限

Bri*_*ian 40 windows powershell permissions

我的 Windows Vista 计算机上只有一个帐户,并且该用户具有管理权限。但是,当我尝试在 PowerShell 中执行命令以终止某个进程时,我收到一条“访问被拒绝”消息。我如何成为管理员?

Nat*_*ley 35

根据微软的说法,Powershell v2 的方式是右键单击快捷方式并选择以管理员身份运行

并在 Powershell 窗口内提升:

start-process powershell –verb runAs
Run Code Online (Sandbox Code Playgroud)

来自 cmd.exe 批处理文件、快捷方式或运行行的内容(重复)如下所示:

powershell "start-process powershell -verb runas"
Run Code Online (Sandbox Code Playgroud)

  • 有趣的是,它可以在旧的 Powershell (5.1) 中工作,但在新的 Powershell 6 (6.2.3) 中不再有效。 (2认同)

Kno*_*nox 19

最简单的方法是使用管理令牌启动 Powershell。为此,您可以右键单击 Powershell(或它的快捷方式),然后单击“以管理员身份运行”。或者,您可以使用elevate.cmd


Has*_*own 14

您可以使用它在运行时自我提升脚本:

#at top of script
if (!
    #current role
    (New-Object Security.Principal.WindowsPrincipal(
        [Security.Principal.WindowsIdentity]::GetCurrent()
    #is admin?
    )).IsInRole(
        [Security.Principal.WindowsBuiltInRole]::Administrator
    )
) {
    #elevate script and exit current non-elevated runtime
    Start-Process `
        -FilePath 'powershell' `
        -ArgumentList (
            #flatten to single array
            '-File', $MyInvocation.MyCommand.Source, $args `
            | %{ $_ }
        ) `
        -Verb RunAs
    exit
}

#example program, this will be ran as admin
$args
Pause
Run Code Online (Sandbox Code Playgroud)

注意,这仍然遵守安全规则(如执行策略),并且会很好地提示 UAC。这可以解决,但你不应该这样做。


use*_*278 11

这将打开一个新的 powershell 实例:

function Run-Elevated ($scriptblock)
{
  # TODO: make -NoExit a parameter
  # TODO: just open PS (no -Command parameter) if $scriptblock -eq ''
  $sh = new-object -com 'Shell.Application'
  $sh.ShellExecute('powershell', "-NoExit -Command $scriptblock", '', 'runas')
}
Run Code Online (Sandbox Code Playgroud)

我希望这有问题 - 特别是,您不会在调用脚本中返回脚本块的输出。另一方面,它会出现在新的 PS 实例中,因此您可以在那里使用它进行破解。


Rya*_*her 5

如果要始终以管理员权限运行 PowerShell,可以右键单击 PowerShell 快捷方式,然后单击“快捷方式”选项卡上的“高级...”按钮,然后选择“以管理员身份运行”。