本文介绍了如何存储格式表的输出以备后用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本可以创建多个作业并在作业中存储两个简单的值.

I have a script that creates several jobs and stores two simple values in the jobs.

Start-Job -ScriptBlock {param ([string]$compip) tnc $compip | select RemoteAddress,PingSucceeded -WarningAction SilentlyContinue} -ArgumentList $compip

这很好用.我想知道的是如何将以下代码存储到变量中?

This works fine. What I would like to know is how can I store the following code into a variable?

Get-Job | Receive-Job | sort RemoteAddress | FT

我已经试过了,但它并没有像我想象的那样工作:

I have tried this, but it does not work as I thought it would:

$pcs = Get-Job | Receive-Job | sort RemoteAddress | FT
$pcs.RemoteAddress

我这样做是不是走错了路?我想存储来自上面 get-job 命令的数据,以便稍后在脚本中使用这些值.我认为它会起作用,因为输出看起来正确:命令:

Am I going at this the wrong way? I would like to store the data from the get-job command above to use the values later in the script. I assumed it would work because the output looks correct:Command:

Get-Job | Receive-Job | sort RemoteAddress | FT

输出:

RemoteAddress PingSucceeded
------------- -------------
192.168.0.163          True
192.168.0.101          False
192.168.0.2            False
192.168.0.251          True

推荐答案

Format-cmdlets 的问题

这里的问题是您使用了 FT,它是 Format-Table 的别名.这些Format- cmdlet 仅用于控制台/屏幕输出.你可以用它们做很多事情来定制输出,但在每种情况下,PowerShell 都需要对数据进行处理才能做到这一点.这包括将传递的对象分解为不同对象的组...

Problem with Format-cmdlets

The issue here is your use of FT which is an alias for Format-Table. These Format- cmdlets are designed for console/screen output only. There are many things you can do with them to tailor that output but in every case PowerShell needs to massage the data in order to be able to do so. This includes breaking down to the passed objects into groups of different objects...

Microsoft.PowerShell.Commands.Internal.Format.FormatEndData
Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
Microsoft.PowerShell.Commands.Internal.Format.FormatStartData
Microsoft.PowerShell.Commands.Internal.Format.GroupEndData
Microsoft.PowerShell.Commands.Internal.Format.GroupStartData

以上数据类型是从运行此代码中提取的.

The above data types were extract from running this code.

Get-ChildItem c:\temp | Format-Table | Get-Member

因此您不再拥有通常从 Get-ChildItemSystem.IO.FileInfo 和 System.IO.DirectoryInfo 对象>

So you no longer have the System.IO.FileInfo and System.IO.DirectoryInfo objects that you would normally get from Get-ChildItem

另一个大问题来自 Format-cmdlet 的性质,即截断数据,例如具有大量元素或长字符串的数组,以尽可能地适合屏幕.对于数组,这是由于首选项变量 $FormatEnumerationLimit 通常默认为 4.

Another large issue comes from Format-cmdlets nature to truncated data, like arrays with a large numbers of elements or long strings, to make as much fit on screen. In the case of arrays, this is due to the preference variable $FormatEnumerationLimit which is commonly defaulted to 4.

Ten Numbers
-----------
{1, 2, 3, 4...}

这些和其他限制都可以通过 cmdlet 开关(如 -AutoSize-HideTableHeadersout-string -width、等等.不过没关系,因为...

These, and other, limitations can all be mitigated with cmdlet switches like -AutoSize and -HideTableHeaders, out-string -width, etc. That does not matter however because...

好消息是解决方案非常简单.停止将它们用于控制台输出以外的任何其他用途.使用我之前的示例:

Good news is the solution is very simple. Stop using them for anything other than console output. Using my earlier example:

  • 将结果保存在变量中?:$result = Get-ChildItem C:\temp

导出数据:Get-ChildItem C:\temp |导出-CSV $path -NoTypeInformation.其他 Export- cmdlet 在这里可能是首选,例如 Export-CLIXml 用于复杂对象,当您想将它们存储以供其他地方使用时.如果您只是想在输出中包含一些漂亮的东西,那么请考虑使用 ConvertTo-HTML.

Exporting Data: Get-ChildItem C:\temp | Export-CSV $path -NoTypeInformation. Other Export-cmdlets could be preferred here like Export-CLIXml for complex objects when you want to store them for use elsewhere. If you are just looking for something pretty to include in your output then consider ConvertTo-HTML instead.

提取单个属性?:只需使用 Select-Object.$result |选择prop1,prop2.您还可以扩展您的属性选择以使用 -ExpandProperty 获取字符串或字符串数​​组: $result |选择 -ExpandProperty prop1

Extracting individual properties?: Just use Select-Object. $result | Select prop1, prop2. You can also expand your property selection to just get the strings or string array with -ExpandProperty: $result | Select -ExpandProperty prop1

使用上述属性执行内联计算?:使用计算表达式 就像使用 Format-Cmdlets 一样.$result |选择prop1,@{Name="prop2";Expression={$_.prop2 * 3}

Performing inline calculations with said properties?: Use calculated expression just as you would with the Format-Cmdlets. $result | Select prop1, @{Name="prop2";Expression={$_.prop2 * 3}

潜在的可接受用途

有些人更喜欢将输出用于电子邮件和记录统计数据.虽然以更易于使用的格式保存数据以备后用是不可或缺的.但是,如果您真的需要这些数据,请记住您不再使用最初拥有的对象.

Some prefer the output for use in emails and for recording statistics. While it is integral to keep data in its more easily used format for later use. However if you really need that data keep in mind that you are not working with the object your originally had anymore.

因此,如果您需要表格格式的数据但存储为字符串,请考虑Out-String

So if you needed your data in a table format but stored as a string then consider Out-String

$body = Get-ChildItem c:\temp | Format-Table | Out-String

但请记住,Format-Table 将处理对象输出,以使其显示在屏幕上(截断的数组属性和长字符串).真的..如果你想要它漂亮和格式化,那么你应该使用 ConvertTo-HTML.

but remember that Format-Table will play with object output in order to get it to display on screen (truncated array properties and long strings). Really.. if you wanted it nice and formatted then you should just use ConvertTo-HTML.

重点是您几乎不需要保留格式表中的数据.几乎总有更好的方法.

Point is you almost never need to keep the data from Format-Table. There is almost always a better way.

这篇关于如何存储格式表的输出以备后用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 05:28