我的函数为一台或多台计算机查询CimInstance
,并将正常运行时间信息存储在对象($obj
)中。如果$obj
包含少于5个属性,则所有内容都可以作为单个表显示。当我有5个属性时,它默认为一个列表,如果我将其传递给Format-Table
,它会为每台计算机输出一个单独的表。是否有一个原因?我可以做些什么来显示所有五个属性并且有一个连续的表吗?
function Get-LKUptime {
param(
[parameter(Mandatory = $true,
ValueFromPipeline = $True,
ValueFromPipelineByPropertyName = $True)]
[string[]]$computername=$env:COMPUTERNAME
)
foreach ($computer in $computername) {
if (Test-Connection $computer -Count 1 -ErrorAction SilentlyContinue) {
try {
$os = Get-CimInstance -ErrorAction 'Stop' -ClassName Win32_OperatingSystem -ComputerName $computer
$status = 'OK'
$continue = $true
} catch {
Write-Warning "Unable to query WMI info for $computer"
$status = 'ERROR'
}
if ($continue) {
$uptime = ((New-Timespan -Start $os.LastBootUpTime -End $os.LocalDateTime).TotalDays -as [int])
if ($uptime -gt 30) {
$patch = $true
} else {
$patch = $false
}
}
} else {
Write-Warning "Connection to $computer failed. $computer may be offline"
$status = 'OFFLINE'
}
$properties = [Ordered]@{
'ComputerName' = $computer;
'StartTime' = $os.LastBootUpTime;
'Uptime (Days)' = $uptime;
'Status' = $status;
'MightNeedPatched' = $patch
}
$obj = New-Object -TypeName PSObject -Property $properties
$obj | Format-Table
}
}
4个属性:
5个属性:
答案 0 :(得分:1)
请勿在函数中使用Format-*
cmdlet,因为它会将原始对象替换为无法进行进一步处理的格式化对象。相反,您应该为您键入自定义格式。
<强> LiamKemp.Format.ps1xml:强>
<Configuration>
<ViewDefinitions>
<View>
<Name>LiamKemp.UptimeInfo</Name>
<ViewSelectedBy>
<TypeName>LiamKemp.UptimeInfo</TypeName>
</ViewSelectedBy>
<TableControl>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>ComputerName</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>StartTime</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Uptime (Days)</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Status</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>MightNeedPatched</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
然后你可以加载你的格式文件:
Update-FormatData .\LiamKemp.Format.ps1xml
或者如果您将功能打包在模块中,则应使用FormatsToProcess
键。
您修改函数以将自定义类型名称添加到输出对象:
function Get-LKUptime {
param(
[parameter(Mandatory = $true,
ValueFromPipeline = $True,
ValueFromPipelineByPropertyName = $True)]
[string[]]$computername=$env:COMPUTERNAME
)
process {
foreach ($computer in $computername) {
if (Test-Connection $computer -Count 1 -ErrorAction SilentlyContinue) {
try {
$os = Get-CimInstance -ErrorAction 'Stop' -ClassName Win32_OperatingSystem -ComputerName $computer
$status = 'OK'
$continue = $true
} catch {
Write-Warning "Unable to query WMI info for $computer"
$status = 'ERROR'
}
if ($continue) {
$uptime = ((New-Timespan -Start $os.LastBootUpTime -End $os.LocalDateTime).TotalDays -as [int])
if ($uptime -gt 30) {
$patch = $true
} else {
$patch = $false
}
}
} else {
Write-Warning "Connection to $computer failed. $computer may be offline"
$status = 'OFFLINE'
}
[PSCustomObject]@{
'ComputerName' = $computer;
'StartTime' = $os.LastBootUpTime;
'Uptime (Days)' = $uptime;
'Status' = $status;
'MightNeedPatched' = $patch
} | Add-Member -TypeName LiamKemp.UptimeInfo -PassThru
}
}
}
现在输出的函数默认格式化为表格。