您知道如果您是系统的管理员用户,您可以右键单击一个批处理脚本,然后以管理员身份运行它,而无需输入管理员密码?

我想知道如何用PowerShell脚本做到这一点。我不想输入我的密码;我只是想模仿右键单击Run As Administrator方法。

到目前为止,我读到的所有内容都要求您提供管理员密码。


当前回答

这里有一些答案是接近的,但比所需的工作多了一些。

为脚本创建一个快捷方式,并将其配置为“以管理员身份运行”:

创建快捷方式。 右键单击快捷方式并打开属性… 编辑目标从<script-path>到powershell <script-path> 单击高级…并启用以管理员身份运行

其他回答

Benjamin Armstrong发表了一篇关于自提升PowerShell脚本的优秀文章。他的代码有一些小问题;下面是基于评论中建议的修复的修改版本。

基本上,它获取与当前进程相关联的身份,检查它是否是管理员,如果不是,就创建一个具有管理员权限的新PowerShell进程,并终止旧进程。

# Get the ID and security principal of the current user account
$myWindowsID = [System.Security.Principal.WindowsIdentity]::GetCurrent();
$myWindowsPrincipal = New-Object System.Security.Principal.WindowsPrincipal($myWindowsID);

# Get the security principal for the administrator role
$adminRole = [System.Security.Principal.WindowsBuiltInRole]::Administrator;

# Check to see if we are currently running as an administrator
if ($myWindowsPrincipal.IsInRole($adminRole))
{
    # We are running as an administrator, so change the title and background colour to indicate this
    $Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)";
    $Host.UI.RawUI.BackgroundColor = "DarkBlue";
    Clear-Host;
}
else {
    # We are not running as an administrator, so relaunch as administrator

    # Create a new process object that starts PowerShell
    $newProcess = New-Object System.Diagnostics.ProcessStartInfo "PowerShell";

    # Specify the current script path and name as a parameter with added scope and support for scripts with spaces in it's path
    $newProcess.Arguments = "& '" + $script:MyInvocation.MyCommand.Path + "'"

    # Indicate that the process should be elevated
    $newProcess.Verb = "runas";

    # Start the new process
    [System.Diagnostics.Process]::Start($newProcess);

    # Exit from the current, unelevated, process
    Exit;
}

# Run your code that needs to be elevated here...

Write-Host -NoNewLine "Press any key to continue...";
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown");

我正在使用下面的解决方案。它通过转录特性处理stdout/stderr,并将退出码正确地传递给父进程。您需要调整文本路径/文件名。

If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{ 
  echo "* Respawning PowerShell child process with elevated privileges"
  $pinfo = New-Object System.Diagnostics.ProcessStartInfo
  $pinfo.FileName = "powershell"
  $pinfo.Arguments = "& '" + $myinvocation.mycommand.definition + "'"
  $pinfo.Verb = "RunAs"
  $pinfo.RedirectStandardError = $false
  $pinfo.RedirectStandardOutput = $false
  $pinfo.UseShellExecute = $true
  $p = New-Object System.Diagnostics.Process
  $p.StartInfo = $pinfo
  $p.Start() | Out-Null
  $p.WaitForExit()
  echo "* Child process finished"
  type "C:/jenkins/transcript.txt"
  Remove-Item "C:/jenkins/transcript.txt"
  Exit $p.ExitCode
} Else {
  echo "Child process starting with admin privileges"
  Start-Transcript -Path "C:/jenkins/transcript.txt"
}

# Rest of your script goes here, it will be executed with elevated privileges

乔纳森和谢伊·列维发布的代码对我不起作用。

请在下面找到工作代码:

If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{   
#"No Administrative rights, it will display a popup window asking user for Admin rights"

$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process "$psHome\powershell.exe" -Verb runAs -ArgumentList $arguments

break
}
#"After user clicked Yes on the popup, your file will be reopened with Admin rights"
#"Put your code here"

当然,如果您有管理员帐户,还可以强制以管理员身份打开应用程序。

找到文件,右键单击>属性>快捷方式>高级,并选择以管理员身份运行

单击“确定”。

C:\Users\“username”\AppData\漫游\Microsoft\Windows\开始菜单\程序\Windows PowerShell是PowerShell快捷方式所在的位置。它仍然会转到不同的位置来调用实际的“exe”(%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe)。

由于PowerShell在涉及权限时是用户配置文件驱动的;如果你的用户名/配置文件有权限做一些事情,那么在PowerShell中,你通常也可以这样做。也就是说,您可以更改位于用户配置文件下的快捷方式,例如C:\Users\"username"\AppData\Roaming\Microsoft\Windows\开始菜单\Programs\Windows PowerShell。

右键单击属性。点击“快捷方式”标签下的“高级”按钮,该标签位于“评论”文本框的右下方,与“打开文件位置”和“更改图标”两个按钮的右侧相邻。

选中“以管理员身份运行”的复选框。单击确定,然后应用和确定。再次右键单击位于C:\Users\“username”\AppData\漫游\Microsoft\Windows\开始菜单\程序\Windows PowerShell的图标,并选择“固定到开始菜单/任务栏”。

现在,无论何时单击该图标,它都会调用UAC进行升级。选择“YES”后,您将注意到PowerShell控制台打开,屏幕顶部将标记为“Administrator”。

更进一步说……你可以在Windows PowerShell的配置文件中右键单击相同的图标快捷方式,并分配一个键盘快捷方式,该快捷方式将执行与单击最近添加的图标完全相同的操作。所以在说“快捷键”的地方,输入键盘键/按钮组合,如:Ctrl + Alt + PP(适用于PowerShell)。单击“应用”和“确定”。

现在你所要做的就是按下你分配的按钮组合,你会看到UAC被调用,在你选择'YES'后,你会看到一个PowerShell控制台出现,“Administrator”显示在标题栏上。