【问题标题】:How to get this PowerShell output as a Table view?如何将此 PowerShell 输出作为表格视图获取?
【发布时间】:2021-08-03 11:10:21
【问题描述】:

我需要在表格视图中获取此 PowerShell 输出。另外,需要在引号内。

当前输出格式:

Testing\Dump\DumpText-1.txt      Dump\DumpText-1.txt
Testing\Dump\DumpText-2.txt      Dump\DumpText-2.txt
Testing\Dump\SubDump1\DumpText-1.txt     SubDump1\DumpText-1.txt
Testing\Dump\SubDump1\DumpText-2.txt     SubDump1\DumpText-2.txt
Testing\Dump\SubDump2\Screenshot.png     SubDump2\Screenshot.png

所需的输出格式:

"Testing\Dump\DumpText-1.txt"            "Dump\DumpText-1.txt"
"Testing\Dump\DumpText-2.txt"            "Dump\DumpText-2.txt"
"Testing\Dump\SubDump1\DumpText-1.txt"   "SubDump1\DumpText-1.txt"
"Testing\Dump\SubDump1\DumpText-2.txt"   "SubDump1\DumpText-2.txt"
"Testing\Dump\SubDump2\Screenshot.png"   "SubDump2\Screenshot.png"

我的脚本是:

$directoryPath=$args[0]

Get-ChildItem $directoryPath -Recurse -Force | ForEach-Object -Process  { 
        
        if (!$_.PSIsContainer) {"$($_.FullName -creplace '^[^\\]*\\', '') `t` $($_.Directory.Name)\$($_.Name)"}
    
    }

【问题讨论】:

标签: powershell directory subdirectory


【解决方案1】:

试试PSCustomObject(并关注Quoting Rules):

Get-ChildItem $directoryPath -Recurse -Force -File |
    ForEach-Object -Process  { 
        [PSCustomObject]@{    
            fulln = """$($_.FullName -creplace '^[^\\]*\\', '')"""
            shrtn = """$(Join-Path -Path $_.Directory.Name -ChildPath $_.Name)"""
        }
    }

编辑

要从表格中隐藏列标题,请按以下方式应用Format-Table cmdlet(在Controlling column widths with Format-Table 阅读更多信息):

Get-ChildItem $directoryPath -Recurse -Force -File |
    ForEach-Object -Process  { 
        [PSCustomObject]@{    
            fulln = """$($_.FullName -creplace '^[^\\]*\\', '')"""
            shrtn = """$(Join-Path -Path $_.Directory.Name -ChildPath $_.Name)"""
        }
    } | Format-Table -HideTableHeaders -AutoSize

但是,Format- cmdlet 设计为仅用于控制台/屏幕输出。阅读更多Problem with Format- cmdlets

高级脚本:

Param(
    [Parameter(Position=0, Mandatory=$false, ValueFromPipeline)]
    [string]$directoryPath='\bat\filez',
    [Parameter()]
    [switch]$AsObject
)

$outObj = Get-ChildItem $directoryPath -Recurse -Force -File |
    ForEach-Object -Process  { 
        [PSCustomObject]@{    
            fulln = """$($_.FullName -creplace '^[^\\]*\\', '')"""
            shrtn = """$(Join-Path -Path $_.Directory.Name -ChildPath $_.Name)"""
        }
    }

if ( $AsObject.IsPresent ) {
    $outObj | Out-Default
} else {
    $outObj | Format-Table -HideTableHeaders -AutoSize
}

示例 1.\SO\67514630.ps1

"bat\filez\more_real.eml"      "filez\more_real.eml"
"bat\filez\PS_preferences.bat" "filez\PS_preferences.bat"
"bat\filez\Sample Input.eml"   "filez\Sample Input.eml"
"bat\filez\SampleInput.eml"    "filez\SampleInput.eml"
"bat\filez\folder\xxx.csv"     "folder\xxx.csv"

示例 2.\SO\67514630.ps1 \bat\foo.txt -AsObject

fulln                       shrtn
-----                       -----
"bat\files\676711\foo.txt"  "676711\foo.txt"
"bat\files\bubu\foo.txt"    "bubu\foo.txt"
"bat\Unusual Names\foo.txt" "Unusual Names\foo.txt"
"bat\foo.txt"               "bat\foo.txt"

【讨论】:

  • 如何更改此脚本以不打印对象名称(fulln 和 shrtn)?
  • @DhanushkaEkanayake 只需在此处输入您想要的名称..
  • @DhanushkaEkanayake 使用以下内容:| Format-Table -HideTableHeaders 在管道的最末端 - 或使用 PathToYourScript.ps1 | Format-Table -HideTableHeaders 并保持脚本不变。
  • 你能告诉我应该在脚本的哪里添加“| Format-Table -HideTableHeaders”吗? (我最后加了,但是不行)
猜你喜欢
  • 1970-01-01
  • 2017-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-10
  • 1970-01-01
相关资源
最近更新 更多