【问题标题】:Pass CMD output to Powershell via pipeline [duplicate]通过管道将 CMD 输出传递给 Powershell [重复]
【发布时间】:2021-11-27 18:56:17
【问题描述】:

想知道如何将控制台应用程序输出从 cmd.exe 传递到 Powershell,以便在没有临时文件的情况下进行进一步操作。

例子:

cmd /c SET | powershell -command Write-Host -f Green $_

期望:绿色 SET 的输出。

【问题讨论】:

  • 等等,你是在 PowerShell 还是 cmd 中运行这个?
  • -Command "$input | Write-Host -f Green"。请注意,这个特定示例当然是人为设计的,因为 PowerShell 可以简单地读取环境本身并按照SET 的方式对其进行格式化,如果您愿意的话 (dir env:\ |% { $_.name + "=" + $_.value })。
  • 不存在关于着色本身或环境变量的问题。这是关于将数据传递到 powershell 以进行进一步处理的方法。无论如何,这个问题得到了回答。谢谢大家。

标签: windows powershell batch-file cmd pipeline


【解决方案1】:

如果你想从一个可执行文件从内部 powershell 输出,只需直接通过管道输出到一个函数:

cmd /c SET |Write-Host -ForegroundColor Green

如果你想从 cmd.exe 通过管道输出到 powershell.exe:

SET |powershell -Command "$input |Write-Host -ForegroundColor Green"

或者,直接从 PowerShell 的 env: 驱动器获取环境变量,无需调用 cmd

Get-ChildItem env: |Out-String |Write-Host -ForegroundColor Green

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-24
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 2016-07-21
    相关资源
    最近更新 更多