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

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

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


当前回答

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”显示在标题栏上。

其他回答

以下是对Shay Levi建议的补充(只需在脚本开头添加这些行):

if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))  
{  
  $arguments = "& '" +$myinvocation.mycommand.definition + "'"
  Start-Process powershell -Verb runAs -ArgumentList $arguments
  Break
}

这将导致当前脚本以管理员模式传递给一个新的powershell进程(如果当前User具有管理员模式,并且脚本不是以管理员身份启动)。

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”显示在标题栏上。

在Shay Levy的答案之上,遵循下面的设置(只有一次)

以管理员权限启动PowerShell。 在堆栈溢出问题PowerShell说“脚本的执行在这个系统上是禁用的。” 例如,将.ps1文件放在任何PATH文件夹中。Windows \ System32系统文件夹

设置之后:

按Win + R 调用powershell Start-Process powershell -Verb runAs <ps1_file>

现在,您可以在一个命令行中运行所有内容。上述操作适用于Windows 8 Basic 64位。

您可以创建一个批处理文件(*.bat),在双击时使用管理权限运行powershell脚本。通过这种方式,您不需要更改powershell脚本中的任何内容。要做到这一点,创建一个与powershell脚本名称和位置相同的批处理文件,然后将以下内容放入其中:

@echo off

set scriptFileName=%~n0
set scriptFolderPath=%~dp0
set powershellScriptFileName=%scriptFileName%.ps1

powershell -Command "Start-Process powershell \"-ExecutionPolicy Bypass -NoProfile -NoExit -Command `\"cd \`\"%scriptFolderPath%`\"; & \`\".\%powershellScriptFileName%\`\"`\"\" -Verb RunAs"

就是这样!

下面是解释:

假设powershell脚本位于路径C:\Temp\ScriptTest。ps1,您的批处理文件必须有路径C:\Temp\ScriptTest.bat。当有人执行这个批处理文件时,会发生以下步骤:

The cmd will execute the command powershell -Command "Start-Process powershell \"-ExecutionPolicy Bypass -NoProfile -NoExit -Command `\"cd \`\"C:\Temp\`\"; & \`\".\ScriptTest.ps1\`\"`\"\" -Verb RunAs" A new powershell session will open and the following command will be executed: Start-Process powershell "-ExecutionPolicy Bypass -NoProfile -NoExit -Command `"cd \`"C:\Temp\`"; & \`".\ScriptTest.ps1\`"`"" -Verb RunAs Another new powershell session with administrative privileges will open in the system32 folder and the following arguments will be passed to it: -ExecutionPolicy Bypass -NoProfile -NoExit -Command "cd \"C:\Temp\"; & \".\ScriptTest.ps1\"" The following command will be executed with administrative privileges: cd "C:\Temp"; & ".\ScriptTest.ps1" Once the script path and name arguments are double quoted, they can contain space or single quotation mark characters ('). The current folder will change from system32 to C:\Temp and the script ScriptTest.ps1 will be executed. Once the parameter -NoExit was passed, the window wont be closed, even if your powershell script throws some exception.

我正在使用下面的解决方案。它通过转录特性处理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