我正在尝试将列表导入HTML格式的表格。它似乎正在按预期工作,除了我有很多单元格填充空白。我试图使用trim和其他方法删除所有额外的空格,但额外的空格仍然在表中。
这是代码。
uemcli -d 192.168.1.100 -u Local/admin -p admin /event/alert/hist show -acknowledged no |
select -Skip 4 > $PSSCriptRoot\output.txt
$nl = [System.Environment]::NewLine
$unity = Get-Content $PSScriptRoot\output.txt
$unity = $unity -replace "[0-9]:",""
$unity = $unity -replace "=",":"
$unity = $unity -replace "$n1$n1",""
$unity = $unity.Trim()
$UnitySummary = ($unity) -split '$n1$n1' | foreach {
$Stringdata = $_.Replace(':','=') -replace '$n1',''
New-Object PSObject -Property $(ConvertFrom-StringData $Stringdata)
}
$style = @"
<style>
TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse; }
TH{border-width: 2px;padding: 3px;border-style: solid; border-color: black;background-color: #08088A; color: white; font-size: 67%;}
TD{border-width: 2px;padding: 3px;border-style: solid; border-color: black; font-size: 100%;}
</style>
"@
$unitySummary |
select 'Id','Time','Message' |
ConvertTo-Html -Head $style |
Out-File $PSScriptRoot\weboutput.html
这是我在第一行(output.txt)中处理的原始文件:
1: ID = alert_457 Time = 2017-12-16 02:46:04.937 Message = A recommended system software (version 4.2.1.9535982) is now available for download. Description = "A recommended system software is now available for download. To ensure optimal system performance, EMC recommends upgrading to this version. Run a health check about a week before installing the upgrade to identify and resolve any underlying issues that may prevent a successful update. (https://172.16.243.100/help/webhelp/en_US/index.html?#unity_t_update_software.html)" Severity = info Acknowledged = no 2: ID = alert_456 Time = 2017-12-15 01:04:33.480 Message = Host 172.16.243.13 is operating normally. Description = "The component is operating normally. No action is required." Severity = info Acknowledged = no 3: ID = alert_455 Time = 2017-12-15 01:04:33.367 Message = Host 172.16.243.12 is operating normally. Description = "The component is operating normally. No action is required." Severity = info Acknowledged = no 4: ID = alert_454 Time = 2017-12-15 01:04:33.245 Message = Host 172.16.243.11 is operating normally. Description = "The component is operating normally. No action is required." Severity = info Acknowledged = no 5: ID = alert_453 Time = 2017-12-15 00:04:38.035 Message = Unable to refresh managed server 172.16.243.13 because of a connection issue. Description = "Failed to connect to host. Please check your network connection. (https://172.16.243.100/help/webhelp/en_US/index.html?#unity_t_unable_to_refresh_managed_server_because_of_a_connection_issue.html)" Severity = critical Acknowledged = no 6: ID = alert_452 Time = 2017-12-15 00:04:37.935 Message = Unable to refresh managed server 172.16.243.12 because of a connection issue. Description = "Failed to connect to host. Please check your network connection. (https://172.16.243.100/help/webhelp/en_US/index.html?#unity_t_unable_to_refresh_managed_server_because_of_a_connection_issue.html)" Severity = critical Acknowledged = no 7: ID = alert_451 Time = 2017-12-15 00:04:37.753 Message = Unable to refresh managed server 172.16.243.11 because of a connection issue. Description = "Failed to connect to host. Please check your network connection. (https://172.16.243.100/help/webhelp/en_US/index.html?#unity_t_unable_to_refresh_managed_server_because_of_a_connection_issue.html)" Severity = critical Acknowledged = no
这是我得到的输出。您可以看到项目放在正确的列中,但是插入了额外的空白单元格,导致数据显示在错误的行中。
答案 0 :(得分:2)
Get-Content
不会将输入文件读作单个字符串,而是读取行数组。因为New-Object
只创建具有单个属性的对象。通过select 'Id','Time','Message'
管理结果然后添加其他属性,仅使用空值。
将Get-Content
的输出通过Out-String
管道以解决此问题:
$unity = Get-Content $PSScriptRoot\output.txt | Out-String
在PowerShell v3及更新版本上,您也可以使用参数Get-Content
调用-Raw
以获得相同的结果:
$unity = Get-Content $PSScriptRoot\output.txt -Raw
答案 1 :(得分:2)
请接受Ansgar的回答,因为它解决了问题的根源。我发布此信息作为额外的帮助,帮助您了解自己所追求的目标。
不是将整个事物作为一个字符串读取,将其拆分,进行多次替换(包括将所有等号转换为冒号,然后将所有冒号转换为等号),取大字符串,将其拆分为条目(由一个数字后跟一行开头的冒号表示,并通过ConvertFrom-StringData
管道每个条目并从中创建对象。它将为您提供我非常确定您希望能够传递到您的表中的内容(选择您想要的粗略属性)。
您甚至可以通过将原始命令传送到Out-String
来跳过整个保存到磁盘并将其读回。
$Unity = uemcli -d 192.168.1.100 -u Local/admin -p admin /event/alert/hist show -acknowledged no |
select -Skip 4 |
Out-String
$Unity -split '(?m)^\d:' |
Where{$_.Trim()} |
ForEach{ [PSCustomObject](ConvertFrom-StringData -StringData $_) }
然后,您只需将其输出到ConvertTo-Html
,然后就可以了。
$Unity = uemcli -d 192.168.1.100 -u Local/admin -p admin /event/alert/hist show -acknowledged no |
select -Skip 4 |
Out-String
$style = @"
<style>
TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse; }
TH{border-width: 2px;padding: 3px;border-style: solid; border-color: black;background-color: #08088A; color: white; font-size: 67%;}
TD{border-width: 2px;padding: 3px;border-style: solid; border-color: black; font-size: 100%;}
</style>
"@
$Unity -split '(?m)^\d:' |
Where{$_.Trim()} |
ForEach{ [PSCustomObject](ConvertFrom-StringData -StringData $_) } |
ConvertTo-Html -Head $style -as Table -Property 'Id','Time','Message' |
Set-Content $PSScriptRoot\weboutput.html