简体   繁体   English

从命令提示符运行提升的 powershell 命令

[英]Run elevated powershell command from command prompt

powershell.exe Start-Process powershell.exe "set-executionpolicy" -Verb runAs

The above command will run a powershell window as admin and execute the command before it, however, if I want to make it say set-executionpolicy bypass , it will error out.上面的命令将以管理员身份运行一个 powershell 窗口并执行它之前的命令,但是,如果我想让它说set-executionpolicy bypass ,它会出错。 How can I pass this parameter through the top command?如何通过 top 命令传递此参数?

Below is the error I get when I pass said parameter:以下是我在传递所述参数时得到的错误:

    Start-Process : A positional parameter cannot be found that accepts argument
'bypass'.
At line:1 char:1
+ Start-Process powershell.exe set-executionpolicy bypass -Verb runAs
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Start-Process], ParameterB
   indingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell
   .Commands.StartProcessCommand

您可以直接在PowerShell.exe上使用–ExecutionPolicy开关来绕过执行策略。

powershell.exe –ExecutionPolicy Bypass

Start-Process accepts an array of arguments in order to pass into created process. Start-Process接受参数数组以传递给创建的进程。 So, in order to launch an elevated Powershell that would execute a Set-ExecutionPolicy cmdlet you need to wrap the arguments into an array.因此,为了启动一个提升的 Powershell 来执行Set-ExecutionPolicy cmdlet,您需要将参数包装到一个数组中。

start-process -filepath powershell.exe -argumentlist @('-command','Set-ExecutionPolicy Bypass') -verb runas

As a side note, you probably want to set execution policy with -scope argument for it to actually be effective once that process ends.作为旁注,您可能希望使用-scope参数设置执行策略,以便在该过程结束后真正生效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用提升的命令运行Powershell脚本 - Run powershell script with elevated command 以管理员身份在宏中运行 Powershell 命令(高级) - Run Powershell command in macro as Admin (Elevated) 如何从 Windows 命令提示符调用提升的 PowerShell 以执行命令和 PowerShell 脚本文件? - How do I call an elevated PowerShell from Windows command prompt to execute commands and a PowerShell script file? 从命令提示符使用 PowerShell 命令运行 schtasks.exe - Run schtasks.exe with PowerShell command from command prompt 从命令提示符运行 PowerShell 命令(无 ps1 脚本) - Run PowerShell command from command prompt (no ps1 script) 在Powershell中运行命令提示符命令 - Run command prompt commands in powershell PowerShell:如何从PowerShell以管理员身份运行远程命令提示符命令? - PowerShell : How to run remote command prompt commands as admin from PowerShell? 无法通过命令提示符运行 powershell 命令 - Cannot run powershell command via command prompt 如何从Windows命令提示符(CMD)运行Long Powershell脚本 - How to Run Long Powershell script from Windows Command Prompt (CMD) PowerShell脚本从任务调度程序运行时未显示命令提示符 - Command prompt not showing when PowerShell script run from task schduler
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM