简体   繁体   English

在 bat 文件中以管理员身份运行 powershell 命令

[英]Run a powershell command as administrator in a bat file

I have this powershell command that must run as administartor in powershell.我有这个 powershell 命令必须在 powershell 中以管理员身份运行。 I want to run as a batch file as administator.我想以管理员身份作为批处理文件运行。 Would you please help me?你能帮帮我吗?

Get-AppxPackage -allusers ContentDeliveryManager |获取-AppxPackage -allusers ContentDeliveryManager | foreach {Add-AppxPackage "$($_.InstallLocation)\appxmanifest.xml" -DisableDevelopmentMode -register } foreach {Add-AppxPackage "$($_.InstallLocation)\appxmanifest.xml" -DisableDevelopmentMode -register }

You can run it like this:你可以像这样运行它:

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -Command ""& {Get-AppxPackage -allusers ContentDeliveryManager | foreach {Add-AppxPackage ""$($_.InstallLocation)\appxmanifest.xml"" -DisableDevelopmentMode -register }"" -Verb RunAs}"

UPDATE更新

Fixed double quotes固定双引号

To avoid the problem of double/double quote, i suggest you to put your command in a script.ps1 then do this command:为避免双引号问题,我建议您将命令放在script.ps1中,然后执行以下命令:

PowerShell -windowstyle hidden -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -windowstyle hidden -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""YourScript.ps1""' -Verb RunAs}"

Try this batch试试这批

<# : batch script
@echo off
cd /D "%~dp0"
if not "%1"=="am_admin" (powershell start -verb runas '%0' am_admin & exit /b)
setlocal
cd %~dp0
powershell -executionpolicy remotesigned -Command "Invoke-Expression $([System.IO.File]::ReadAllText('%~f0'))"
endlocal
goto:eof
#>
# here write your powershell commands...
Get-AppxPackage -allusers ContentDeliveryManager | foreach {Add-AppxPackage "$($_.InstallLocation)\appxmanifest.xml" -DisableDevelopmentMode -register }

save it as.bat or.cmd and run it.将其保存为 .bat 或 .cmd 并运行它。

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

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