如何将.bashrc中的Docker相关函数转换为PowerShell等效实现?
PowerShell版VidHop Docker管理函数
以下是与你原有bash功能完全匹配的PowerShell函数实现:
function vidhop_stop { $containerIds = docker ps -a -q if ($containerIds) { docker stop $containerIds docker rm $containerIds } } function vidhop_start { # 执行stop并抑制输出 vidhop_stop *> $null docker run --name vidhop-docker ` -v "${PWD}/media:/vidhop" ` -v "${PWD}/vidhop/.bash_history:/root/.bash_history" ` -it vidhop-docker /bin/bash } function vidhop_build { if (-not (Test-Path -Path "Dockerfile" -PathType Leaf)) { Write-Host "当前目录没有Dockerfile" return } docker build -t vidhop-docker . } function vidhop_build_no_cache { if (-not (Test-Path -Path "Dockerfile" -PathType Leaf)) { Write-Host "当前目录没有Dockerfile" return } docker build --no-cache -t vidhop-docker . } function vidhop { param( [Parameter(Position=0)] [string]$Command ) if ([string]::IsNullOrEmpty($Command)) { Write-Host "用法: vidhop start | stop | build" return } switch ($Command.ToLower()) { "start" { vidhop_start; return } "stop" { vidhop_stop; return } "build" { vidhop_build; return } default { Write-Host "'$Command' 不是有效参数,请使用 'vidhop [ start | stop | build ]'" } } }
新手安装指南
临时生效(仅当前PowerShell会话可用)
- 打开PowerShell窗口
- 复制上述全部代码,粘贴到窗口中按回车执行
- 直接输入
vidhop start/vidhop stop等命令即可使用
永久生效(所有PowerShell会话都可用)
- 查看PowerShell配置文件路径,执行命令:
$PROFILE - 若文件不存在,创建配置文件:
New-Item -Path $PROFILE -ItemType File -Force - 打开配置文件进行编辑:
notepad $PROFILE - 将上述函数代码粘贴到文件中,保存后关闭
- 允许本地脚本运行(否则配置文件无法生效):
按提示输入Set-ExecutionPolicy RemoteSigned -Scope CurrentUserY确认 - 重启PowerShell,之后每次打开都能直接使用这些命令
内容的提问来源于stack exchange,提问作者progonkpa
相关产品推荐
相关产品推荐

