Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将分隔符插入PowerShell输出_Powershell - Fatal编程技术网

将分隔符插入PowerShell输出

将分隔符插入PowerShell输出,powershell,Powershell,Get Process-Name explorer | select Handles、WS、CPU、Id、ProcessName | ft-HideTableHeaders的结果返回以下输出: 2623 255336448 178.125 10080 explorer 为了将此数据接收到第三方系统中,我需要对结果进行管道分隔: 2623|255336448|178.125|10080|explorer 实现这一目标的最佳方法是什么?如何: (Get-Process explorer |

Get Process-Name explorer | select Handles、WS、CPU、Id、ProcessName | ft-HideTableHeaders
的结果返回以下输出:

2623 255336448 178.125 10080 explorer 
为了将此数据接收到第三方系统中,我需要对结果进行管道分隔:

2623|255336448|178.125|10080|explorer 
实现这一目标的最佳方法是什么?

如何:

(Get-Process explorer |
  Select-Object Handles,Ws,CPU,ID,ProcessName |
  ConvertTo-Csv -Delimiter '|' -NoTypeInformation |
  Select-Object -Skip 1) -replace '"',''
仅使用
ft
Format Table
)以便于在PowerShell控制台中查看(这不利于将数据发送到其他应用程序,因为这样您就必须撤消格式化,所以首先不要格式化)。

以提供一种更简洁(且速度稍快)的替代方法:

  • foreach('Handles'、'WS'、'CPU'、'Id'、'ProcessName'){$.$prop}
    输出每个进程对象的所有感兴趣的属性(
    $\uCmdlet提供的手头输入对象)

  • $(…)
    将它们收集为一个
    [object[]
    数组,该数组

  • 。。。启用该数组用作
    -join
    运算符的LHS,以便使用
    |
    作为分隔符连接数组元素以形成单个字符串

总的来说,您可以根据需要为每个输入对象获得一个字符串

Get-Process -Name explorer | ForEach-Object { 
  $(foreach ($prop in 'Handles', 'WS', 'CPU', 'Id', 'ProcessName') { $_.$prop }) -join '|'
}