<# .SYNOPSIS 启动 Windows 上的 Podman 容器 .DESCRIPTION 此脚本检查 Podman machine 状态,必要时启动它,然后启动指定的容器列表。 #> # 配置参数 $PodmanWaitTime = 3 $PodmanContainerWaitTime = 3 $ContainerList = @("filecodebox") function Write-ColorOutput { param( [string]$Message, [string]$Color = "White" ) Write-Host $Message -ForegroundColor $Color } try { # 检查 Podman 是否可用 if (-not (Get-Command podman -ErrorAction SilentlyContinue)) { throw "Podman 未安装或未在 PATH 中找到。请确保已安装 Podman。" } Write-ColorOutput "等待 $PodmanWaitTime 秒以确保系统准备就绪..." "Yellow" Start-Sleep -Seconds $PodmanWaitTime # 检查 Podman machine 状态 Write-ColorOutput "检查 Podman machine 状态..." "Cyan" $machineInfo = podman machine info 2>&1 if ($LASTEXITCODE -ne 0) { throw "获取 Podman machine 信息时出错: $machineInfo" } $machineState = ($machineInfo | Select-String "machinestate").Line if (-not $machineState) { throw "无法从 podman machine info 中解析机器状态" } $machineState = ($machineState -split ":")[1].Trim().ToLower() if ($machineState -ne "running") { Write-ColorOutput "启动 Podman machine..." "Yellow" $startResult = podman machine start 2>&1 if ($LASTEXITCODE -ne 0) { throw "启动 Podman machine 失败: $startResult" } Write-ColorOutput "等待 Podman machine 启动完成..." "Cyan" Start-Sleep -Seconds 10 } else { Write-ColorOutput "Podman machine 已在运行。" "Green" } # 启动容器 Write-ColorOutput "启动 Podman 容器..." "Cyan" Start-Sleep -Seconds $PodmanContainerWaitTime foreach ($container in $ContainerList) { Write-ColorOutput "启动容器: $container" "Yellow" # 检查容器是否存在 $containerExists = podman container inspect $container 2>&1 if ($LASTEXITCODE -ne 0) { Write-ColorOutput "警告: 容器 '$container' 不存在或无法访问" "Red" continue } # 启动容器 $startResult = podman container start $container 2>&1 if ($LASTEXITCODE -ne 0) { Write-ColorOutput "启动容器 '$container' 失败: $startResult" "Red" } else { Write-ColorOutput "容器 '$container' 启动成功" "Green" } Start-Sleep -Seconds 2 } Write-ColorOutput "所有操作已完成。" "Green" # 显示容器状态摘要 Write-ColorOutput "`n容器状态摘要:" "Cyan" podman ps -a --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" Pause } catch { Write-ColorOutput "错误: $($_.Exception.Message)" "Red" exit 1 }