windows下vscode系列编辑器批量命令操作

批量配置文件

editors.json
{
  "editors": [
    {
      "name": "VSCode",
      "path": "D:\\_SOFT_INSTALL\\Microsoft VS Code\\bin\\code"
    },
    {
      "name": "VSCodium",
      "path": "D:\\_SOFT_INSTALL\\VSCodium\\bin\\codium"
    },
    {
      "name": "Windsurf",
      "path": "D:\\_SOFT_INSTALL\\Windsurf\\bin\\windsurf"
    },
    {
      "name": "Trae CN",
      "path": "D:\\_SOFT_INSTALL\\TraeCN\\bin\\trae"
    },
    {
      "name": "CodeBuddy CN",
      "path": "D:\\_SOFT_INSTALL\\CodeBuddyCN\\bin\\buddycn"
    },
    {
      "name": "Qoder",
      "path": "D:\\_SOFT_INSTALL\\Qoder\\bin\\qoder"
    },
    {
      "name": "Lingma",
      "path": "D:\\_SOFT_INSTALL\\Lingma\\bin\\lingma"
    },
    {
      "name": "Kiro",
      "path": "D:\\_SOFT_INSTALL\\Kiro\\bin\\kiro"
    },
    {
      "name": "CodeFlicker",
      "path": "D:\\_SOFT_INSTALL\\KuaishouCodeFlicker\\bin\\codeflicker"
    },
    {
      "name": "CatPawAI",
      "path": "D:\\_SOFT_INSTALL\\CatPawAI\\bin\\catpawai"
    }
  ]
}

批量执行ps1脚本

vsrun.ps1
# 设置编码,防止中文乱码
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8

# 1. 获取脚本所在目录和配置文件路径
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$ConfigFile = Join-Path $ScriptDir "editors.json"

# 检查配置文件是否存在
if (-not (Test-Path $ConfigFile)) {
    Write-Host "[错误] 找不到配置文件: $ConfigFile" -ForegroundColor Red
    exit 1
}

# 2. 解析参数
if (-not $args) {
    Write-Host "用法: .\vsrun.ps1 [参数...]"
    Write-Host "示例: .\vsrun.ps1 --install-extension .\my-plugin.vsix"
    exit 1
}

# 3. 读取 JSON 配置
try {
    $jsonContent = Get-Content $ConfigFile -Raw -Encoding UTF8
    $config = $jsonContent | ConvertFrom-Json
} catch {
    Write-Host "[错误] 解析 JSON 失败: $_" -ForegroundColor Red
    exit 1
}

# 4. 遍历编辑器并执行
foreach ($editor in $config.editors) {
    $name = $editor.name
    $path = $editor.path

    Write-Host "--- [$name] 开始执行 ---" -ForegroundColor Cyan

    if (Test-Path $path) {
        try {
            $filePath = ""
            $cmdArgs = @()

            # 核心修复逻辑:处理空格路径
            $ext = [System.IO.Path]::GetExtension($path).ToLower()

            # 如果是无后缀文件(如 bin/code)或 .bat/.cmd,必须通过 cmd /c 调用
            if ([string]::IsNullOrEmpty($ext) -or $ext -eq ".bat" -or $ext -eq ".cmd") {
                $filePath = "cmd.exe"

                # 重点:给路径加上双引号 "...",防止空格截断
                # 使用 /d 参数禁用当前目录自动切换,保持环境纯净
                $cmdArgs = @("/d", "/c", "`"$path`"") + $args
            } 
            # 如果是 .exe,直接调用
            else {
                $filePath = $path
                $cmdArgs = $args
            }

            # 执行进程
            # -RedirectStandardOutput 用于捕获输出(原样输出)
            # -RedirectStandardError 用于捕获错误
            $outLog = "$Temp\vscode_out_$RANDOM.log"
            $errLog = "$Temp\vscode_err_$RANDOM.log"

            $proc = Start-Process -FilePath $filePath `
                                  -ArgumentList $cmdArgs `
                                  -NoNewWindow `
                                  -Wait `
                                  -PassThru `
                                  -RedirectStandardOutput $outLog `
                                  -RedirectStandardError $errLog

            # 原样输出标准输出内容
            if (Test-Path $outLog) {
                $content = Get-Content $outLog -Raw
                if ($content) { Write-Host $content -NoNewline }
                Remove-Item $outLog -Force
            }

            # 原样输出错误内容
            if (Test-Path $errLog) {
                $content = Get-Content $errLog -Raw
                if ($content) { Write-Host $content -NoNewline -ForegroundColor Red }
                Remove-Item $errLog -Force
            }

            # 检查退出代码
            if ($proc.ExitCode -ne 0) {
                Write-Host "[错误] 进程退出代码: $($proc.ExitCode)" -ForegroundColor Red
            }

        } catch {
            Write-Host "[异常] $_" -ForegroundColor Red
        }
    } else {
        Write-Host "[跳过] 路径不存在: $path" -ForegroundColor Yellow
    }

    Write-Host ""
}

Write-Host "[完成] 所有任务已处理。"
目录
冀ICP备2021025979号-1