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

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

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


当前回答

我找到了一个方法…

创建一个批处理文件打开你的脚本:

@echo off
START "" "C:\Scripts\ScriptName.ps1"

然后创建一个快捷方式,在桌面上说(右键单击新建->快捷方式)。

然后粘贴到位置:

C:\Windows\System32\runas.exe /savecred /user:*DOMAIN*\*ADMIN USERNAME* C:\Scripts\BatchFileName.bat

第一次打开时,您必须输入一次密码。然后将其保存在Windows凭据管理器中。

在此之后,您应该能够以管理员身份运行,而无需输入管理员用户名或密码。

其他回答

这是一个澄清…

powershell的RUNAS / SAVECRED凭据“不安全”,尝试了一下,它将管理员身份和密码添加到凭据缓存中,可以在其他地方使用。如果您已经这样做了,我建议您检查并删除该条目。

请检查您的程序或代码,因为微软的策略是,如果没有UAC(入口点)以管理身份执行程序,则不能将用户代码和管理代码混合在同一个代码blob中。这将是Linux上的sudo(同样的事情)。

UAC有3种类型,在程序清单中生成的提示符或入口点。它不会提升程序,所以如果没有UAC,它需要管理,它将失败。虽然UAC作为管理员需求是好的,但它可以防止在没有身份验证的情况下执行代码,并防止混合代码场景在用户级别执行。

以下是对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:\Windows\System32\cmd.exe”,并选择“以管理员身份运行”,然后你就可以以管理员身份运行任何应用程序,而无需提供任何密码。

您可以创建一个批处理文件(*.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.

提升PowerShell from Start>运行

在2012R2或2016年,你不能在没有炮击两次的情况下使用“run”命令运行elevated powershell:

C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell -命令"saps powershell -Verb RunAs "