String 将PowerShell对象输出为字符串

String 将PowerShell对象输出为字符串,string,powershell,format,String,Powershell,Format,此代码启动ping: $ProcessInfo = New-Object System.Diagnostics.ProcessStartInfo $ProcessInfo.FileName = "ping.exe" $ProcessInfo.RedirectStandardError = $true $ProcessInfo.RedirectStandardOutput = $true $ProcessInfo.UseShellExecute = $false $ProcessInfo.Argu

此代码启动ping:

$ProcessInfo = New-Object System.Diagnostics.ProcessStartInfo
$ProcessInfo.FileName = "ping.exe"
$ProcessInfo.RedirectStandardError = $true
$ProcessInfo.RedirectStandardOutput = $true
$ProcessInfo.UseShellExecute = $false
$ProcessInfo.Arguments = "localhost"
$Proc = New-Object System.Diagnostics.Process
$Proc.StartInfo = $ProcessInfo
$Proc.Start() | Out-Null
当我在命令中键入$Proc时

$Proc
我明白了:

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
     27       3     1936       1852    10     0.02   6832 PING
我想把数据行转换成字符串

所以我试了一下:

$Proc | Format-table -HideTableHeaders
$Foo = $Proc | Format-table -HideTableHeaders
$Foo
我得到了这个:

 27       3     1936       1852    10     0.02   6832 PING
Microsoft.PowerShell.Commands.Internal.Format.FormatStartData Microsoft.PowerShell.Commands.Internal.Format.GroupStartData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.GroupEndData Microsoft.PowerShell.Commands.Internal.Format.FormatEndData
我试过这个:

$Proc | Format-table -HideTableHeaders
$Foo = $Proc | Format-table -HideTableHeaders
$Foo
得到这个:

 27       3     1936       1852    10     0.02   6832 PING
Microsoft.PowerShell.Commands.Internal.Format.FormatStartData Microsoft.PowerShell.Commands.Internal.Format.GroupStartData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.GroupEndData Microsoft.PowerShell.Commands.Internal.Format.FormatEndData
所以问题是。。。通常如何将powershell对象的nice字符串输出转换为字符串

最终,当我在powershell脚本的一行中单独键入$Proc时,我要做的就是将单词START放在正常输出的前面

START     27       3     1936       1852    10     0.02   6832 PING
但是,“Start”+$Proc会产生以下结果:

Start System.Diagnostics.Process (PING)
这就是为什么我想尝试将$Proc转换成字符串。

类似于

$string=$proc |格式表-HideTableHeaders |输出字符串
“开始”+$string

为什么不在
格式表
之后通过管道将其传输到
输出字符串