将默认对象格式设置为表格

时间:2018-01-16 07:18:19

标签: windows powershell formatting powershell-v3.0

我一直在研究PowerShell函数来查询WSUS并返回计算机名,更新组,LastSyncTime和所需/失败的更新。

该函数输出对象

typename: Microsoft.UpdateServices.Internal.BaseApi.ComputerTarget

我为这个现有对象添加了自定义属性,只想设置输出中显示的对象。为了设置它,我使用了这段代码:

#Creating my object:
[void][reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration")
$wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::getUpdateServer("mywsusserver.domain.local",$False)
$results = $wsus.SearchComputerTargets("search_string")

#add Custom Members
$results | Add-Member -MemberType AliasProperty -Name ComputerName -Value FullDomainName
$results | Add-Member -NotePropertyName "Groups" -NotePropertyValue ($results.GetComputerTargetGroups() | where {$_.name -ne 'All Computers'}).name
$results | Add-Member -NotePropertyName "NeededUpdates" -NotePropertyValue ($objupdateinfo.downloadedcount + $objupdateinfo.notinstalledcount)
$results | Add-Member -NotePropertyName "FailedUpdates" -NotePropertyValue $objupdateinfo.failedcount         

#set the default display properties for my object
$defaultDisplayset = 'ComputerName','Groups','IPAddress','LastSyncTime','NeededUpdates','FailedUpdates'

$defaultDisplayPropertySet = New-Object System.Management.Automation.PSPropertySet('DefaultDisplayPropertySet',[string[]]$defaultDisplaySet)

$PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]@($defaultDisplayPropertySet)

$results | Add-Member MemberSet PSStandardMembers $PSStandardMembers

显示属性信息:this article

所以当我显示这个代码块的输出时,我得到这样的东西:

PS C:\> $results


ComputerName  : Servername1
Groups        : Third Week
IPAddress     : 192.168.1.100
LastSyncTime  : 9/13/2017 2:19:51 AM
NeededUpdates : 15
FailedUpdates : 0

ComputerName  : Servername2
Groups        : Customer - Third Week
IPAddress     : 192.168.100.73
LastSyncTime  : 9/13/2017 2:44:34 AM
NeededUpdates : 7
FailedUpdates : 0

ComputerName  : Servername3
Groups        : Third Week
IPAddress     : 192.168.200.233
LastSyncTime  : 9/13/2017 2:09:23 AM
NeededUpdates : 6
FailedUpdates : 0

我想默认显示的是:

ComputerName Groups     IPAddress       LastSyncTime         NeededUpdates FailedUpdates
------------ ------     ---------       ------------         ------------- -------------
Servername1  Third Week 192.168.1.100   9/13/2017 2:19:51 AM            15             0
Servername2  Third Week 192.168.100.73  9/13/2017 2:44:34 AM             7             0
Servername3  Third Week 192.168.200.233 9/13/2017 2:09:23 AM             6             0

但我发现实现它的唯一方法是使用:

$results | Format-Table -AutoSize

我已经探索过this article,我只想让这个对象的输出成为一个表,而不必使用Format-Table来实现它。

0 个答案:

没有答案