powershell - 创建批处理文件以启用 PS 脚本执行并使用行命令启动提升的 PS shell

标签 powershell batch-file uac

为了简化查找​​每个安装程序并手动逐一启动它们的工作,我想制作一个 .bat 文件,为大约 10 个应用程序安装程序运行巧克力安装,同时无需执行任何操作。

我尝试使用 -command 选项输入命令,但它从未在 PowerShell 的提升窗口中执行该命令。

  • 启动bat文件-->启动elev PS shell。
  • 以管理员身份在桌面上运行 PS 脚本 --> 安装巧克力程序。

我想在提升的 PS shell 中运行的代码:

Set-ExecutionPolicy Bypass -scope Process -Force
Write-Host -ForegroundColor Black -BackgroundColor White "Chocolatey wird heruntergeladen und installiert"
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
choco feature enable -n=allowGlobalConfirmation
Write-Host -ForegroundColor Black -BackgroundColor White "Programme werden heruntergeladen und installiert"
choco install googlechrome
choco install firefox
choco install irfanview
choco install mpc-hc-clsid2
choco install 7zip
choco install notepadplusplus.install
choco install vlc
choco install adobereader
choco install libreoffice-still

理想情况下,我只需单击 .bat 文件并接受 PS 管理窗口的 UAC,所有程序都会在后台下载并安装。

powershell -Command "&{ Start-Process powershell -ArgumentList '-File C:\users\gabri\desktop\start.ps1' -Verb RunAs}"

.bat 文件仅显示 UAC,然后什么也没有发生。

最佳答案

Set-ExecutionPolicy Bypass -scope Process -Force 放入脚本文件 (*.ps1) 在某种程度上达不到目的,因为如果持久配置的execution policy从一开始就阻止执行脚本,您对进程级覆盖的尝试永远不会执行。

相反,请使用 PowerShell CLI-ExecutionPolicy 参数,用于执行策略的进程级覆盖。

此外,您可能需要使用 -NoExit CLI 参数来保持提升的 session 打开,以便您检查结果。

:: From a batch file.
powershell -Command "Start-Process -Verb RunAs powershell -ArgumentList '-ExecutionPolicy Bypass -NoExit -File C:\users\gabri\desktop\start.ps1'"

关于powershell - 创建批处理文件以启用 PS 脚本执行并使用行命令启动提升的 PS shell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76831778/

相关文章:

delphi - 尝试在标准用户上写入 HKLM 时如何不触发异常?

powershell - PowerShell 中的数组展开

sql-server - 如何从 PowerShell 运行 sp_configure 'Ad Hoc Distributed Queries'?

windows - 在 Windows 批处理脚本中重命名文件

batch-file - 为什么在 IN 之后 FOR 会因延迟子字符串扩展而失败?

windows-vista - vbscript检测UAC是否提升

visual-studio - 如何将 UAC 集成到我的 VB6 程序中?

windows - 将文件夹结构和子目录/文件导出到文本

powershell - 如何自动更新在powershell中运行的后台作业的数量?

linux - shell cmd transfair windows cmd