# https://github.com/iainbrighton/PScribo # help about_document
# create a folder to store generated documents $OutPath = "c:\temp\out" $exists = Test-Path-Path$OutPath if (!$exists) { $null = New-Item-Path$OutPath-ItemType Directory -Force }
# generate document Document 'BIOS' { # get an object with rich information $info = Get-WmiObject-Class Win32_BIOS
# find out the property names that have actual information $properties = $info | Get-Member-MemberType *property | Select-Object-ExpandProperty Name | Where-Object {
$info.$_-ne$null-and$info.$_-ne''
} | Sort-Object
# turn each property into a separate object $infos = $properties | ForEach-Object { [PSCustomObject]@{ Name = $_ Value = $info.$_ }
}
Paragraph -Style Heading1 "BIOS Information"
# generate a table with one line per property $infos | # select the properties to display, and the header texts to use Table -Columns Name,Value -Headers'Item','Content'-Width0
# get the service data to use: # generate the service information to use # (requires PowerShell 5 because prior to PowerShell 5, Get-Service does not supply # StartType - alternative: use Get-WmiObject -Class Win32_Service, and adjust # property names)
# IMPORTANT: run this information through Select-Object to get a cloned copy # of the original objects so that style information can be appended $services = Get-Service | Select-Object-Property DisplayName, Status, StartType | Sort-Object-Property DisplayName
# define style to use for highlighting Style -Name HighlightedService -Color Red -BackgroundColor Yellow -Bold
# apply a different format to cell "Status" for all objects where # status is "Stopped" and StartType is "Automatic" $services | Where { $_.Status -eq'Stopped'-and$_.StartType -eq'Automatic'} | Set-Style-Property Status -Style HighlightedService
# https://github.com/iainbrighton/PScribo # help about_document
# create a folder to store generated documents $OutPath = "c:\temp\out" $exists = Test-Path-Path$OutPath if (!$exists) { $null = New-Item-Path$OutPath-ItemType Directory -Force }
# generate document Document 'ServiceReport' { # generate the service information to use # (requires PowerShell 5 because prior to PowerShell 5, Get-Service does not supply # StartType - alternative: use Get-WmiObject -Class Win32_Service, and adjust # property names) $services = Get-Service | Select-Object-Property DisplayName, Status, StartType
# generate a table with one line per service $services | # select the properties to display, and the header texts to use Table -Columns DisplayName, Status, StartType -Headers'Service Name','Current State','Startup Type'-Width0
# https://github.com/iainbrighton/PScribo # help about_document
# create a folder to store generated documents $OutPath = "c:\temp\out" $exists = Test-Path-Path$OutPath if (!$exists) { $null = New-Item-Path$OutPath-ItemType Directory -Force }
# generate document Document 'ADUser' { # get 40 AD user to illustrate $user = Get-ADUser-Filter * -ResultSetSize40-Properties mail | Select-Object-Property Name, mail, SID
Paragraph -Style Heading1 "AD User Liste"
# generate a table with one line per user $user | # select the properties to display, and the header texts to use Table -Columns Name, mail, SID -Headers'Employee','Email','SID'-Width0
# https://github.com/iainbrighton/PScribo # help about_document
# create a folder to store generated documents $OutPath = "c:\temp\out" $exists = Test-Path-Path$OutPath if (!$exists) { $null = New-Item-Path$OutPath-ItemType Directory -Force }
# generate document Document 'ServiceReport' { # generate the service information to use # (requires PowerShell 5 because prior to PowerShell 5, Get-Service does not supply # StartType - alternative: use Get-WmiObject -Class Win32_Service, and adjust # property names) $services = Get-Service | Select-Object-Property DisplayName, Status, StartType
# generate a table with one line per service $services | # select the properties to display, and the header texts to use Table -Columns DisplayName, Status, StartType -Headers'Service Name','Current State','Startup Type'-Width0
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
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 = 'Hallo' BIOS = Get-WmiObject-Class Win32_BIOS User = $env:username } }
ID : 1 Random : 1794057589 Date : 25.05.201813:06:57 Text : Hallo 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