functionGet-TestData { # if a function is to return more than one information kind, # wrap it in a custom object
[PSCustomObject]@{ # wrap anything you'd like to return ID = 1 Random = Get-Random Date = Get-Date Text = 'Hello' BIOS = Get-WmiObject-Class Win32_BIOS User = $env:username } }
结果是以表格形式呈现:
1 2 3 4 5 6 7 8 9 10
PS> Get-TestData
ID : 1 Random : 147704985 Date : 25.05.201813:09:26 Text : Hello BIOS : \\DESKTOP-7AAMJLF\root\cimv2:Win32_BIOS.Name="1.6.1",SoftwareElementID="1.6.1",SoftwareElementState=3,TargetOperatingSys tem=0,Version="DELL - 1072009" User : tobwe
当移除掉一些属性,限制属性个数为 4 个或更少时,PowerShell 输出一个表格:
1 2 3 4 5
PS> Get-TestData
ID Random Text User ---------------- 1567248729 Hello tobwe
[PSCustomObject]@{ # wrap anything you'd like to return ID = 1 Random = Get-Random Date = Get-Date Text = 'Hello' BIOS = Get-WmiObject-Class Win32_BIOS User = $env:username } | # add the first-class citizen info to your object Add-Member-MemberType MemberSet -Name PSStandardMembers -Value$info-PassThru
ID : 1 Random : 1298877814 Date : 25.05.201813:15:22 Text : Hello BIOS : \\DESKTOP-7AAMJLF\root\cimv2:Win32_BIOS.Name="1.6.1",SoftwareElementID="1.6.1",SoftwareElementState=3,TargetOperatingSys tem=0,Version="DELL - 1072009" User : tobwe