为了简化查找每个安装程序并手动逐一启动它们的工作,我想制作一个 .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/