我想创建一个包含一系列字符串标题的表格,这些字符串已从输出中提取。我已经用过了......
$Scopearray = @("$server","$ip","$ScopeName","$Comment")
转过来......
$ip = $Trimmed[0]
$server = $Trimmed[1]
$ScopeName = $Trimmed[2]
$Comment = $Trimmed[3]
进入这个:
PS C:\WINDOWS\system32> $Scopearray MyServer.domain 10.1.1.1 NameofScope ScopeDetails
但我需要把它变成一张桌子,就像这样:
我已尝试过下面的内容,以及其他多维数据的例子,但我明显遗漏了一些基本的东西。
$table = @()
foreach ($instance in $Scopearray) {
$row = "" | Select ServerName,IP,ScopeName,Comment
$row.Heading1 = "Server Name"
$row.Heading2 = "IP Address"
$row.Heading3 = "Scope Name"
$row.Heading4 = "Comment"
$table += $row
}
答案 0 :(得分:1)
根据输入数据创建对象:
... | ForEach-Object {
New-Object -Type PSObject -Property @{
'Server Name' = $Trimmed[1]
'IP Address' = $Trimmed[0]
'Scope Name' = $Trimmed[2]
'Comment' = $Trimmed[3]
}
}
在PowerShell v3及更高版本中,您可以使用[PSCustomObject]
类型加速器来简化它:
... | ForEach-Object {
[PSCustomObject]@{
'Server Name' = $Trimmed[1]
'IP Address' = $Trimmed[0]
'Scope Name' = $Trimmed[2]
'Comment' = $Trimmed[3]
}
}
默认情况下,PowerShell以表格形式显示最多包含4个属性的对象(除非对象具有特定的格式说明),但如果需要,您可以通过Format-Table
cmdlet强制表格输出:
... | Format-Table
请注意,除了Out-String
之外,如果您想将该表格表示写入文件,则需要Format-Table
:
... | Format-Table | Out-String | Set-Content 'C:\output.txt'