简体   繁体   English

PowerShell ForEach-Object参数

[英]PowerShell ForEach-Object parameters

I am currently trying to use the Get-RemoteProgram script to list of installed programs on remote computers. 我目前正在尝试使用Get-RemoteProgram脚本列出远程计算机上已安装的程序。

Not only do I want to capture the remote program list but also the version which is straight forward for a single system. 我不仅要捕获远程程序列表,而且要捕获单个系统的直接版本。

RemoteProgram -ComputerName remotecomputername -Property DisplayVersion,VersionMajor
ProgramName                                                 ComputerName        DisplayVersion VersionMajor
-----------                                                 ------------        -------------- ------------
System Center Endpoint Protection                           remotecomputername  4.7.214.0                 4
Microsoft Visual Studio 2010 Tools for Office Runtime (x64) remotecomputername  10.0.50903                4
Synaptics Pointing Device Driver                            remotecomputername  18.0.7.34                18

However, when I loop this across multiple systems I lose the DisplayVersion and MajorVersion fields completely. 但是,当我在多个系统之间循环时,我会完全丢失DisplayVersionMajorVersion字段。

Get-Content -Path C:\Temp\computerlist.txt | ForEach-Object -Begin {RemoteProgram} -Process {RemoteProgram -ComputerName $_ -Property DisplayVersion,VersionMajor}
ProgramName                                                ComputerName
-----------                                                ------------
System Center Endpoint Protection                          remotecomputer
Microsoft Visual J# 2.0 Redistributable Package - SE (x64) remotecomputer
Mozilla Firefox 57.0.2 (x64 en-US)                         remotecomputer
Mozilla Maintenance Service                                remotecomputer

This is a case of PowerShell's output formatting. 这是PowerShell输出格式的一种情况。

When an object is returned, you get the full object and all of its properties. 返回对象后,您将获得完整的对象及其所有属性。

When you try to display an object without instructions, PowerShell does its best to display it appropriately. 当您尝试显示没有说明的对象时,PowerShell会尽力适当地显示它。 That means it will decide which properties to show, whether to display them as a table or list, etc. 这意味着它将决定要显示的属性,是将它们显示为表格还是列表等。

You can override how its displayed with Format-* commands, but for the purposes of using the objects, all the information is there. 您可以使用Format-*命令覆盖其显示方式 ,但是出于使用对象的目的,所有信息都已存在。

That means if you pipe it to something like Export-Csv , it will use all the properties even if you aren't seeing them in a normal display. 这意味着,如果您将其通过管道Export-CsvExport-Csv类的东西,即使您没有在常规显示中看到它们,它也会使用所有属性。

If you want to see them specifically, use a Format-* command, but again those are for display only; 如果要特别查看它们 ,请使用Format-*命令,但是同样,这些仅用于显示; don't send the out of those to other commands. 不要将这些发送给其他命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM