Powershell 在批处理脚本期间重新启动环境和脚本
我已经为我和其他一些人构建了一些FFmpeg powershell脚本,我正在尝试使设置和更新过程尽可能简单。最终目标是能够运行1个批处理文件,安装Chocolate、FFmpeg、git,克隆github repo(用于更新),并编辑Windows注册表以将实际的FFmpeg powershell脚本/控制台程序添加到Windows资源管理器上下文菜单。通过这种方式,我只需将包含所有内容的文件夹传递给他们一次,每当我更改或向项目中添加内容时,我可以告诉他们再次运行批处理文件,并且presto所有内容都是最新的 然而,我正在努力找到一种方法来安装chocolate,然后用chocolate安装git,然后用一个.bat文件运行git命令。从安装Chocolate后我可以看出,在安装git之前,我需要完全重新启动shell,然后在使用git命令之前,我必须再次重新启动shell。到目前为止,大多数实际处理都是通过从.bat文件启动的Powershell脚本进行的,在执行每一步时,我都会更新一个txt文件,尝试重新启动批处理脚本,并读取txt文件以从我停止的地方开始:Powershell 在批处理脚本期间重新启动环境和脚本,powershell,batch-file,github,ffmpeg,chocolatey,Powershell,Batch File,Github,Ffmpeg,Chocolatey,我已经为我和其他一些人构建了一些FFmpeg powershell脚本,我正在尝试使设置和更新过程尽可能简单。最终目标是能够运行1个批处理文件,安装Chocolate、FFmpeg、git,克隆github repo(用于更新),并编辑Windows注册表以将实际的FFmpeg powershell脚本/控制台程序添加到Windows资源管理器上下文菜单。通过这种方式,我只需将包含所有内容的文件夹传递给他们一次,每当我更改或向项目中添加内容时,我可以告诉他们再次运行批处理文件,并且presto所
@echo off
echo Administrative permissions required. Detecting permissions...
echo.
net session >nul 2>&1
if %errorLevel% == 0 (
echo Success: Administrative permissions confirmed.
echo.
) else (
echo Failure: Current permissions inadequate.
PAUSE
exit
)
set relativePath=%~dp0
set relativePath=%relativePath:~0,-1%
PowerShell -NoProfile -ExecutionPolicy Bypass -File "%relativePath%\Setup\CheckRequiredPackages.ps1" -relativePath "%relativePath%"
set /p step=<"%relativePath%\Setup\Step.txt"
if %step% == 1 (
(echo 2) > "%relativePath%\Setup\Step.txt"
PowerShell -NoProfile -ExecutionPolicy Bypass -File "%relativePath%\Setup\GetChocolatey.ps1"
start "" "%relativePath%\RunMe.bat"
exit
)
if %step% == 2 (
(echo 3) > "%relativePath%\Setup\Step.txt"
PowerShell -NoProfile -ExecutionPolicy Bypass -File "%relativePath%\Setup\GetRequiredPackages.ps1"
start "" "%relativePath%\RunMe.bat"
exit
)
if %step% == 3 (
(echo 0) > "%relativePath%\Setup\Step.txt"
PowerShell -NoProfile -ExecutionPolicy Bypass -File "%relativePath%\Setup\Update.ps1" -relativePath "%relativePath%"
)
PAUSE
Exit
@echo关闭
需要回显管理权限。正在检测权限。。。
回声。
net会话>nul 2>&1
如果%errorLevel%==0(
回显成功:已确认管理权限。
回声。
)否则(
回显失败:当前权限不足。
暂停
出口
)
设置相对路径=%~dp0
设置relativePath=%relativePath:~0,-1%
PowerShell-NoProfile-ExecutionPolicy Bypass-文件“%relativePath%”\Setup\CheckRequiredPackages.ps1”-relativePath“%relativePath%”
set/p step=“%relativePath%\Setup\step.txt”
PowerShell-NoProfile-ExecutionPolicy Bypass-文件“%relativePath%\Setup\getchocolate.ps1”
启动“”%relativePath%\RunMe.bat”
出口
)
如果%step%==2(
(echo 3)>%relativePath%\Setup\Step.txt
PowerShell-NoProfile-ExecutionPolicy Bypass-文件“%relativePath%\Setup\GetRequiredPackages.ps1”
启动“”%relativePath%\RunMe.bat”
出口
)
如果%step%==3(
(echo 0)>%relativePath%\Setup\Step.txt
PowerShell-NoProfile-ExecutionPolicy Bypass-文件“%relativePath%\Setup\Update.ps1”-relativePath“%relativePath%”
)
暂停
出口
问题是在批处理脚本中使用
start
命令似乎不起作用,我猜,因为新进程是从处理巧克力安装的同一个进程派生出来的,它不算是实际重新启动shell。是否有任何方法可以实际重新启动shell并以某种方式让批处理文件在没有用户干预的情况下启动?事实上,启动的进程继承调用进程的环境,而不是从注册表读取可能更新的环境变量定义
Chocolate附带批处理文件RefreshEnv.cmd
(C:\ProgramData\Chocolate\bin\RefreshEnv.cmd
,但C:\ProgramData\Chocolate\bin
应位于%PATH%
中)特别是为了避免必须启动新的独立会话才能使环境更新生效
因此,类似以下内容可能会起作用:
:: Assumes that Chocolatey was just installed to the default location.
call "%ProgramData%\chocolatey\bin\RefreshEnv.cmd"
:: If Chocolatey was *previously* installed and its installation directory
:: has already been added to %Path%, the following will do:
:: call RefreshEnv.cmd
call "%relativePath%\RunMe.bat"
由于Chocolate仅在脚本执行期间安装,因此其binaries文件夹尚未在%Path%
中,因此您必须按其完整路径调用RefreshEnv.cmd
,如上图所示,该路径假定为默认安装目录
现在演示如何从PowerShell直接使用.NET方法刷新$env:Path
(%Path%
)环境变量,这是一种实用的解决方案
但是,请注意,RefreshEnv.cmd
更全面,因为它重新加载所有环境变量定义,因此可能会重新添加和修改这些定义
请注意,从PowerShell调用RefreshEnv.cmd
不起作用,因为它会耗尽进程(这意味着它无法更新调用进程的环境)
但是,chocolate提供了一个updatesessionenvironment
PowerShell命令(别名为refreshenv
),您可以在安装chocolate后立即使用该命令,如下所示:
#导入定义Update SessionEnvironment又名refreshenv的模块
导入模块“$env:ProgramData\Chocolate\helpers\chocolateyProfile.psm1”
#刷新所有环境变量。
更新SessionEnvironment或:refreshenv
请参阅,了解一种更健壮的方法,该方法不依赖于假定默认位置已安装到
事实上,启动的进程继承调用进程的环境,而不是从注册表读取可能更新的环境变量定义
Chocolate附带批处理文件RefreshEnv.cmd
(C:\ProgramData\Chocolate\bin\RefreshEnv.cmd
,但C:\ProgramData\Chocolate\bin
应位于%PATH%
中)特别是为了避免必须启动新的独立会话才能使环境更新生效
因此,类似以下内容可能会起作用:
:: Assumes that Chocolatey was just installed to the default location.
call "%ProgramData%\chocolatey\bin\RefreshEnv.cmd"
:: If Chocolatey was *previously* installed and its installation directory
:: has already been added to %Path%, the following will do:
:: call RefreshEnv.cmd
call "%relativePath%\RunMe.bat"
由于Chocolate仅在脚本执行期间安装,因此其binaries文件夹尚未在%Path%
中,因此您必须按其完整路径调用RefreshEnv.cmd
,如上图所示,该路径假定为默认安装目录
现在演示如何从PowerShell直接使用.NET方法刷新$env:Path
(%Path%
)环境变量,这是一种实用的解决方案
但是,请注意,RefreshEnv.cmd
更全面,因为它重新加载所有环境变量定义,因此可能是新添加和修改的
Write-Host "Installing / updating required packages..."
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol =
[System.Net.ServicePointManager]::SecurityProtocol -bor 3072; Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
choco install ffmpeg -y
choco install git -y
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
Write-Host "Deleting old files..."
Remove-Item -LiteralPath $relativePath -Force -Recurse
Start-Sleep 2
Write-Host "`nUpdating Files..."
git clone https://github.com/TheNimble1/FFmpegContextCommands.git $relativePath