英文:
Create Batch File to enable PS script exec and launch elevated PS shell with line commands
问题
为了简化我查找每个安装程序并逐个手动启动它们的工作,我想创建一个批处理文件 (.bat) 来运行大约 10 个应用程序安装程序的 Chocolatey 安装,而无需在此期间进行其他操作。
我尝试使用 -command
选项输入我的命令,但它从未在 PowerShell 的提升窗口中执行。
- 启动 .bat 文件 --> 启动提升的 PowerShell Shell。
- 以管理员身份在桌面上运行 PowerShell 脚本 --> 安装 Chocolatey 程序。
我希望在提升的 PowerShell Shell 中运行的代码如下:
Set-ExecutionPolicy Bypass -scope Process -Force
Write-Host -ForegroundColor Black -BackgroundColor White "正在下载和安装 Chocolatey"
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 "正在下载和安装程序"
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 文件并接受 UAC(用户帐户控制)以获取 PowerShell 的管理员窗口,然后所有程序将在后台下载并安装。
powershell -Command "&{ Start-Process powershell -ArgumentList '-File C:\users\gabri\desktop\start.ps1' -Verb RunAs}"
批处理文件只显示一个 UAC 提示,然后什么都不发生。
英文:
To simplify my job of finding every installer and manually launching them one by one, I would like to make a .bat file that runs a chocolatey install for ~10 app-installers without doing something in the meantime.
I tried to use the -command
option for entering my command, but it never executed it in a elevated window of PowerShell.
- start bat file --> starts elev PS shell.
- run PS script on Desktop as Admin --> install chocolatey programs.
My code that I would like to run in a elevated 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
Ideally I just have to click on the .bat file and accept UAC for the admin window of PS and all programs are downloaded and installed in the background.
powershell -Command "&{ Start-Process powershell -ArgumentList '-File C:\users\gabri\desktop\start.ps1' -Verb RunAs}"
The .bat file only shows a UAC, and then nothing happens.
答案1
得分: 2
将Set-ExecutionPolicy Bypass -scope Process -Force
放在脚本文件 (*.ps1
) 中,有点违背了初衷,因为如果永久配置的执行策略在一开始就阻止执行你的脚本,你尝试的进程级覆盖就不会执行。
相反,使用PowerShell的CLI的-ExecutionPolicy
参数来进行进程级执行策略的覆盖。
另外,你可能想要使用-NoExit
CLI参数,以保持提升的会话开启,这允许你检查结果。
:: 从批处理文件中执行。
powershell -Command "Start-Process -Verb RunAs powershell -ArgumentList '-ExecutionPolicy Bypass -NoExit -File C:\users\gabri\desktop\start.ps1'"
英文:
<!-- language-all: sh -->
Putting Set-ExecutionPolicy Bypass -scope Process -Force
inside a script file (*.ps1
) somewhat defeats the purpose, because if the persistently configured execution policy prevents execution of your script to begin with, your attempt at a process-level override never gets to execute.
Instead, use the PowerShell CLI's -ExecutionPolicy
parameter for a process-level override of the execution policy.
Additionally, you may want to use the -NoExit
CLI parameter so as to keep the elevated session open, which allows you to inspect the results.
:: From a batch file.
powershell -Command "Start-Process -Verb RunAs powershell -ArgumentList '-ExecutionPolicy Bypass -NoExit -File C:\users\gabri\desktop\start.ps1'"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论