将一个PowerShell对象输出到一个字符串中 [英] Output a powershell object into a string
问题描述
此代码启动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
我得到这个: (K)PM(K)WS(K)VM(M)CPU ProcessName $ b $($) b ------- ------ ----- ----- ----- -------- -----------
27 3 1936 1852 10 0.02 6832 PING
我想要的是将数据线一个字符串。
所以我试过这个:
$ b $ pre $ code $ proc | Format-table -HideTableHeaders
我得到这个:
27 3 1936 1852 10 0.02 6832 PING
我试过这个:
$ Foo = $ Proc | Format-table -HideTableHeaders
$ foo
得到这个:
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对象的良好的字符串输出到一个字符串?
最终,我所要做的是在我刚刚在我的powershell脚本中的一行中输入$ Proc时,在正常输出前面粘贴START字样。
START 27 3 1936 1852 10 0.02 6832 PING
但是,Start+ $ Proc产生这个:
$ p $ 启动System.Diagnostics.Process(PING)
所以这就是为什么我想要把$ Proc放到一个字符串中。
>类似的东西。$ b
$ string = $ proc | Format-Table -HideTableHeaders | Out-String
start+ $ string
?
This code launches 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
When I type $Proc at the command:
$Proc
I get this:
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
27 3 1936 1852 10 0.02 6832 PING
What I want to do is get the data line into a string.
So I tried this:
$Proc | Format-table -HideTableHeaders
And I got this:
27 3 1936 1852 10 0.02 6832 PING
I Tried this:
$Foo = $Proc | Format-table -HideTableHeaders
$Foo
And got this:
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
So the question is... How do you typically get the nice string output from a powershell object into a string?
Ultimately all I'm trying to do is stick the word START in front of the normal output I get when I just type $Proc by itself on a line in my powershell script.
START 27 3 1936 1852 10 0.02 6832 PING
But, "Start " + $Proc produces this:
Start System.Diagnostics.Process (PING)
So that's why I thought I'd try to get $Proc into a string.
Something like..
$string = $proc | Format-Table -HideTableHeaders | Out-String
"start" + $string
That work for you?
这篇关于将一个PowerShell对象输出到一个字符串中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!