You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Invoke-Command执行结果无法正确导出至CSV的技术问询

PowerShell脚本CSV导出异常问题解决

问题描述

脚本可正常运行,但导出的CSV包含PSComputerName、RunspaceId等无关字段,实际导出内容:

"PSComputerName","RunspaceId","PSShowComputerName","Length"
"Server02","cecee03a-e45f-4b77-85e4-dfade4c30e78","True","0"
"Server02","cecee03a-e45f-4b77-85e4-dfade4c30e78","True","229"
"Server02","cecee03a-e45f-4b77-85e4-dfade4c30e78","True","0"
"Server02","cecee03a-e45f-4b77-85e4-dfade4c30e78","True","0"

期望导出的CSV格式:

Computer Name,Last Exit Code
Server02,0
Server122,99
Port closed or computer unreachable removing  server33

当前使用的脚本代码:

$installs = Import-Csv "C:\TEMP\agent.csv"
$results = Foreach ($Singleitem in $installs) {
    try {
       Invoke-Command -ComputerName $Singleitem.name -ScriptBlock { cmd /c C:\tmp\agent.bat ;[pscustomobject]@{Lastexitcode = $lastexitcode} } -ErrorAction 'Stop'
    }
    catch {
        [pscustomobject]@{
             "ComputerName" = "Port closed or computer unreachable removing $($Singleitem.name)"
        }
    }
}

$results | Export-Csv -Path "C:\Temp\agentinstall.csv" -Encoding 'UTF8' -NoTypeInformation

问题原因

  1. Invoke-Command远程执行返回的对象会自动附加PSComputerName、RunspaceId等远程执行元数据属性
  2. 成功、失败分支返回的对象结构不一致,导致CSV导出字段混乱

修复方案

方案1:保持CSV列结构统一(推荐)

确保成功和失败分支返回结构一致的对象,剥离远程元数据:

$installs = Import-Csv "C:\TEMP\agent.csv"
$results = Foreach ($Singleitem in $installs) {
    try {
        # 仅获取远程执行的退出码,避免带回元数据对象
        $exitCode = Invoke-Command -ComputerName $Singleitem.name -ScriptBlock { 
            cmd /c C:\tmp\agent.bat 
            $LASTEXITCODE 
        } -ErrorAction 'Stop'
        # 构造与期望格式一致的对象
        [pscustomobject]@{
            "Computer Name" = $Singleitem.name
            "Last Exit Code" = $exitCode
        }
    }
    catch {
        # 失败分支同样构造统一结构,退出码可设为自定义值或空
        [pscustomobject]@{
            "Computer Name" = "Port closed or computer unreachable removing $($Singleitem.name)"
            "Last Exit Code" = 99 # 或$null
        }
    }
}

$results | Export-Csv -Path "C:\Temp\agentinstall.csv" -Encoding 'UTF8' -NoTypeInformation

方案2:实现混合格式输出(破坏CSV列结构,不推荐)

若必须保留错误提示单行格式,可直接写入文本而非使用Export-Csv:

$installs = Import-Csv "C:\TEMP\agent.csv"
# 先写入CSV表头
"Computer Name,Last Exit Code" | Out-File "C:\Temp\agentinstall.csv" -Encoding UTF8

Foreach ($Singleitem in $installs) {
    try {
        $exitCode = Invoke-Command -ComputerName $Singleitem.name -ScriptBlock { 
            cmd /c C:\tmp\agent.bat 
            $LASTEXITCODE 
        } -ErrorAction 'Stop'
        # 写入正常执行结果行
        "$($Singleitem.name),$exitCode" | Out-File "C:\Temp\agentinstall.csv" -Encoding UTF8 -Append
    }
    catch {
        # 写入错误提示行
        "Port closed or computer unreachable removing $($Singleitem.name)," | Out-File "C:\Temp\agentinstall.csv" -Encoding UTF8 -Append
    }
}

内容的提问来源于stack exchange,提问作者Arbelac

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 18:14:53