我正在使用PowerShell编写监控报告。该脚本提取服务器详细信息,磁盘状态和应用程序状态。
我能够在HTML表格中通过邮件发送服务器详细信息和磁盘状态。但是读取文件的内容(通过从Oracle DB中提取数据的批处理文件创建)并通过HTML格式的相同邮件发送它不起作用。
以下是文本文件中的数据:
**program-id sta pid start time stop time**
Clnt_dp T 0 04/03/2017 08:00:03 04/03/2017 08:00:04
Clnt_ds R 5140 04/03/2017 08:00:03
Clnt_rpc R 8572 04/03/2017 08:00:03
bksrvr R 4692 04/03/2017 07:59:03
chkw T 0 04/03/2017 07:59:03 04/03/2017 08:09:04
db T 0 04/03/2017 07:59:01 04/03/2017 07:59:02
dsaux R 8444 04/03/2017 07:59:03
dssched R 4792 04/03/2017 07:59:03
dsshares R 7672 04/03/2017 07:59:03
ixmonSvc R 5552 04/03/2017 07:59:03
jbd R 7536 04/03/2017 07:59:02
migration R 476 04/03/2017 07:59:03
notifSrvr R 1220 04/03/2017 07:59:03
timestamp R 8928 04/03/2017 07:59:02
任何人都可以让我知道如何实现这一目标?感谢。
答案 0 :(得分:0)
您需要以固定宽度间隔分割字符串。使用您的文本文件摘录,模仿Get-Content:
cls
$content = @'
**program-id sta pid start time stop time**
Clnt_dp T 0 04/03/2017 08:00:03 04/03/2017 08:00:04
Clnt_ds R 5140 04/03/2017 08:00:03
Clnt_rpc R 8572 04/03/2017 08:00:03
bksrvr R 4692 04/03/2017 07:59:03
chkw T 0 04/03/2017 07:59:03 04/03/2017 08:09:04
db T 0 04/03/2017 07:59:01 04/03/2017 07:59:02
dsaux R 8444 04/03/2017 07:59:03
dssched R 4792 04/03/2017 07:59:03
dsshares R 7672 04/03/2017 07:59:03
ixmonSvc R 5552 04/03/2017 07:59:03
jbd R 7536 04/03/2017 07:59:02
migration R 476 04/03/2017 07:59:03
notifSrvr R 1220 04/03/2017 07:59:03
timestamp R 8928 04/03/2017 07:59:02
'@
$content -split "`r*`n" |
select -Skip 2 -Property @{name='program-id';expression={$_.Substring(0 ,17).Trim()}}, #1st
@{name='sta'; expression={$_.Substring(18,4).Trim()}}, #2nd
@{name='pid'; expression={$_.Substring(22,11).Trim()}}, #3rd
@{name='start time';expression={$_.Substring(33,19).Trim()}}, #4th
@{name='stop time'; expression={$_.Substring(54).Trim()}} | #5th
ConvertTo-Html
...并使用Get-Content ...
Get-Content -Path 'c:\path\to\file.txt' |
select -Skip 2 -Property @{name='program-id';expression={$_.Substring(0 ,17).Trim()}}, #1st
@{name='sta'; expression={$_.Substring(18,4).Trim()}}, #2nd
@{name='pid'; expression={$_.Substring(22,11).Trim()}}, #3rd
@{name='start time';expression={$_.Substring(33,19).Trim()}}, #4th
@{name='stop time'; expression={$_.Substring(54).Trim()}} | #5th
ConvertTo-Html
答案 1 :(得分:0)
使用此ConvertFrom-SourceTable cmdlet:
$content -split '[\r\n]+' | Foreach-Object {$_.Trim('*')} |
ConvertFrom-SourceTablel | Format-Table # or ... | ConvertTo-Html
program-id sta pid start time stop time
---------- --- --- ---------- ---------
Clnt_dp T 0 04/03/2017 08:00:03 04/03/2017 08:00:04
Clnt_ds R 5140 04/03/2017 08:00:03
Clnt_rpc R 8572 04/03/2017 08:00:03
bksrvr R 4692 04/03/2017 07:59:03
chkw T 0 04/03/2017 07:59:03 04/03/2017 08:09:04
db T 0 04/03/2017 07:59:01 04/03/2017 07:59:02
dsaux R 8444 04/03/2017 07:59:03
dssched R 4792 04/03/2017 07:59:03
dsshares R 7672 04/03/2017 07:59:03
ixmonSvc R 5552 04/03/2017 07:59:03
jbd R 7536 04/03/2017 07:59:02
migration R 476 04/03/2017 07:59:03
notifSrvr R 1220 04/03/2017 07:59:03
timestamp R 8928 04/03/2017 07:59:02