简体   繁体   English

如何以管理员身份使用 powershell 运行 CMD 命令?

[英]How I can run CMD commands as administrator with powershell?

I want to run CMD commands as admin with Powershell. There is full command list:我想以管理员身份使用 Powershell 运行 CMD 命令。有完整的命令列表:

Dism /OnLine /CleanUp-Image /CheckHealth&Dism /OnLine /CleanUp-Image /RestoreHealth

Can be any way to do this?有什么办法可以做到这一点? I want to do with Run command.我想用运行命令来做。

Step 1: Open the Command Prompt, and type the PowerShell as a command, then press Enter key.第 1 步:打开命令提示符,输入 PowerShell 作为命令,然后按 Enter 键。 Step 2: Now, the command prompt will turn to Windows PowerShell.第 2 步:现在,命令提示符将变为 Windows PowerShell。 Step 3: Type the command start-process PowerShell -verb runas and press "enter" key.第 3 步:输入命令 start-process PowerShell -verb runas 并按“enter”键。 Step 4: It will bring up an elevated Windows PowerShell as an administrator.第4步:它将以管理员身份启动提升的Windows PowerShell。

Is that what you needed?那是你需要的吗?

You can run most cmd commands within PowerShell without having to open another process, but if you want to elevate you will need to invoke Start-Process -RunAs to elevate the child process.您可以在 PowerShell 内运行大多数cmd命令,而无需打开另一个进程,但如果您想要提升,则需要调用Start-Process -RunAs来提升子进程。 For example:例如:

$spArgs = @{
  Verb = 'RunAs';
  Path = "${env:SystemRoot}\system32\dism.exe";
  ArgumentList = "/OnLine /CleanUp-Image /CheckHealth&Dism /OnLine /CleanUp-Image /RestoreHealth";
}
Start-Process @spArgs

If you want to run a cmd built-in command however, there's no external command you can run from PowerShell (for example, mklink ).但是,如果您想运行cmd内置命令,则无法从 PowerShell 运行外部命令(例如mklink )。 In this case you will need to run cmd.exe elevated powershell. To use the mklink example:在这种情况下,您需要运行cmd.exe elevated powershell。要使用mklink示例:

$spArgs = @{
  Verb = 'RunAs';
  Path = "${env:SystemRoot}\system32\cmd.exe";
  ArgumentList = "/c mklink /D LINK_PATH TARGET_PATH;
}
Start-Process @spArgs

In either scenario, if you want to run something elevated from something other than PowerShell, the commands above can be used though you will need to first proxy the call through powershell.exe -Command "Start-Process...."在任何一种情况下,如果您想运行从 PowerShell 以外的其他东西提升的东西,可以使用上面的命令,但您需要首先通过powershell.exe -Command "Start-Process...."代理调用。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM