powershell - 如何将 powershell out-string 中的字符串作为参数传递给 get-content/cat (直接)?

标签 powershell

我基本上想这样做:

Get-Childitem -Recurse -filter "* *" -Force | foreach-object {
  get-childitem $_ | select-object fullname |
    Format-Table -HideTableHeaders -wrap |
    out-string | cat $input
}

我认为这里的 Cat 是 powershell 的 get-content 的别名,但我在编写 bash 代码后经常使用它。我不认为这是问题所在。它给出了错误:

cat : The input object cannot be bound to any parameters for the command either because the command does not take
+ ... me | Format-Table -HideTableHeaders -wrap | out-string | cat $input }

尝试获取内容:

Get-Childitem -Recurse -filter "* *" -Force | foreach-object {
  get-childitem $_ | select-object fullname |
    Format-Table -HideTableHeaders -wrap |
    out-string | Get-Content $input
}

类似错误:

Get-Content : The input object cannot be bound to any parameters for the command either because the command does
not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.

我还尝试删除out-string

寻找这个问题的答案是我如何到达out-string的,但它不起作用。我很想知道如果可行的话如何在不分配变量的情况下做到这一点。

最佳答案

事实上,catGet-Content 的别名。在 Windows 上。

管道输入Get-Content接受的唯一形式是表示文件路径的对象或字符串,然后输出其内容。

这不是Out-String输出(它通常输出一个单多行字符串,在您的情况下是 Format-Table 生成的 for-display 格式输出的字符串表示形式)。

根据您的评论,您正在寻找如下内容:

Get-Childitem -Recurse -Directory -Filter "* *" -Force | 
  Get-ChildItem -File |
    Get-Content

顺便说一句:automatic $input variable仅在特定上下文中定义,并且不能按照您尝试的方式用作命令参数。具体来说,$input 表示当前整个范围外部管道/标准输入输入,而不是流经给定管道的内容。

关于powershell - 如何将 powershell out-string 中的字符串作为参数传递给 get-content/cat (直接)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68944034/

相关文章:

powershell - 替换文字失败

regex - Powershell 使用正则表达式创建新的哈希表对象

powershell - 在 Azure PowerShell 中向 ARM 和 ASM 进行一次身份验证

powershell - 在 PowerShell 中,如何在不更改顺序的情况下从拆分路径中删除重复项?

REGEX - 从专有名称中提取 OU

powershell - 将 Cmdlet 输出与字符串连接起来

powershell - 较短版本的 powershell cmdlet 参数

json - 使用 Powershell 从 JSON 选择特定关键字值

visual-studio-2010 - Powershell 运行时

powershell - 范围标题必须使用适当的属性或方法