如何在powershell的提升模式下执行一组命令 [英] how to execute set of commands in elevated mode of powershell

查看:55
本文介绍了如何在powershell的提升模式下执行一组命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了以下方法在管理员模式下执行命令.

i have tried the below way to execute the commands in administrator mode.

PS>start-process powershell -verb runas $app = Get-AppxPackage -all|Where-Object{$_.Name-like "*$ReleaseName*"}

PS>start-process powershell -verb runas Remove-AppxPackage $app.PackageFullName

  • 对于第一次调用,它打开并成功执行命令并关闭管理 powershell 实例.对于第二次调用,它需要 $app 信息,该信息不可用,因为它再次打开一个新的 PS 管理窗口
  • 我无法在正常模式下执行 Get-AppxPackage -all -all 仅需要管理员模式
  • for the first call, it opens and executes the command successfully and closes the admin powershell instance. for the second call it requires $app information which is not available because it again opens a new PS admin window
  • i can't execute Get-AppxPackage -all in normal mode -all requires admin mode only

尝试了以下但没有运气.

tried the below but no luck.

PS>start-process powershell -verb runas 
{

$app = Get-AppxPackage | Where-Object{$_.Name-like "*$ReleaseName*"};

Remove-AppxPackage $app.PackageFullName

}

有人可以建议我如何在 powershell 提升模式下执行上述指令集吗?

can someone suggest me how to execute set of instructions like above in powershell elevated mode?

提前致谢

推荐答案

显而易见的方法:

以提升模式"打开Powershell控制台->右键单击快捷方式/exe,然后单击以管理员身份运行.或者在开始菜单中,输入 Powershell 并按 CTRL + SHIFT + ENTER

Open Powershell console in "elevated mode" -> Right click shortcut / exe and click Run as Administrator. Or in start menu, type Powershell and hit CTRL + SHIFT + ENTER

然后从这里运行命令.

或者您可以在脚本(.ps1 文件)中包含命令并调用该脚本:

Or you can have the commands in a script (.ps1 file) and invoke that script:

start-process powershell -verb runas -argument script.ps1

我还想提一下,在你的命令中,你不必将它存储在 $app 中,你可以使用类似的东西:

I would also like to mention that in yout commands, you don't have to store it in $app, you can use something like:

Get-AppxPackage -all| Where-Object{$_.Name-like "$ReleaseName"} | Remove-AppxPackage

这篇关于如何在powershell的提升模式下执行一组命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆