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

PowerShell Get-ChildItem返回结果不一致问题求助

多服务器搜索文件时输出格式不一致问题

我在多台服务器的驱动器中搜索特定文件,使用的PowerShell代码如下:

$servers = Get-ADComputer -filter {Name -like "SERVER-*"}
foreach ($server in $Servers){
    $Server.Name
    Invoke-Command -ComputerName $server.Name -ScriptBlock {
        $filename="db2cmd.exe"
        $listOfDrives=(get-psdrive | ? { $_.provider -match 'FileSystem'}).root
        foreach ($Drive in $listOfDrives){
            If ($Drive -ne "C:\" -and $Drive -ne "A:\") {
                Get-ChildItem -Path $Drive -Filter $filename -Recurse -ErrorAction SilentlyContinue -Force
            } 
        }
    }
}

执行后,多数服务器找到文件时会返回包含目录和列标题的完整输出:

Directory: E:\IBM\DB2\SQLLIB\BIN


Mode                LastWriteTime         Length Name                                                                                       PSComputerName                                                                           
----                -------------         ------ ----                                                                                       --------------                                                                           
-a----         1/5/2024  11:32 AM          55888 db2cmd.exe                                                                                 ServerA 

但部分服务器仅返回文件属性行,缺失目录和列标题:

-a----         1/5/2024  11:32 AM          55888 db2cmd.exe                                                                                 ServerB

我用相同命令却出现输出差异,调整搜索参数也没解决这个显示问题,求帮忙。


问题原因

这种差异通常是因为远程会话中Get-ChildItem的输出在序列化/反序列化过程中丢失了格式元数据,或者部分服务器的PowerShell版本、配置不同导致输出行为不一致。当Invoke-Command返回的对象在本地被格式化时,若对象的类型信息不完整,就会丢失目录标题和列头。

解决办法

1. 在远程脚本块中直接格式化输出为字符串

强制在远程端就把输出格式化为固定字符串,避免本地格式化差异:

$servers = Get-ADComputer -filter {Name -like "SERVER-*"}
foreach ($server in $Servers){
    Write-Host "`n=== 正在检查服务器: $($Server.Name) ==="
    Invoke-Command -ComputerName $server.Name -ScriptBlock {
        $filename="db2cmd.exe"
        $listOfDrives=(Get-PSDrive | Where-Object { $_.provider -match 'FileSystem'}).root
        foreach ($Drive in $listOfDrives){
            If ($Drive -ne "C:\" -and $Drive -ne "A:\") {
                $results = Get-ChildItem -Path $Drive -Filter $filename -Recurse -ErrorAction SilentlyContinue -Force
                if ($results) {
                    # 手动添加目录信息和格式化输出
                    Write-Host "`n   Directory: $($results.DirectoryName)"
                    $results | Format-Table -AutoSize | Out-String | Write-Host
                }
            } 
        }
    }
}

2. 本地统一格式化返回的对象

把远程返回的对象收集到本地后,统一用Format-Table处理,确保格式一致:

$servers = Get-ADComputer -filter {Name -like "SERVER-*"}
foreach ($server in $Servers){
    Write-Host "`n=== 服务器: $($Server.Name) ==="
    $searchResults = Invoke-Command -ComputerName $server.Name -ScriptBlock {
        $filename="db2cmd.exe"
        $listOfDrives=(Get-PSDrive | Where-Object { $_.provider -match 'FileSystem'}).root
        $output = @()
        foreach ($Drive in $listOfDrives){
            If ($Drive -ne "C:\" -and $Drive -ne "A:\") {
                $output += Get-ChildItem -Path $Drive -Filter $filename -Recurse -ErrorAction SilentlyContinue -Force
            } 
        }
        return $output
    }
    if ($searchResults) {
        $searchResults | Format-Table -AutoSize -Property Mode, LastWriteTime, Length, Name, PSComputerName
    } else {
        Write-Host "未找到目标文件"
    }
}

3. 检查远程服务器的PowerShell版本

部分老版本PowerShell(如v2.0)在序列化对象时会丢失更多元数据,可在远程脚本块中添加Write-Output $PSVersionTable.PSVersion查看版本,统一升级到较新的版本(如v5.1)也能减少这类差异。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 19:10:55