powershell html报表,在Powershell中使用HTML创建表格

这样的事情应该工作。

单桌

Get-Content "C:\Dv\Server_List.txt" -ErrorAction SilentlyContinue | ForEach-Object {

Write-Host "Automatic Services Stopped :" $_

Get-WmiObject Win32_Service -ComputerName $_ -Filter "startmode = 'auto' AND state != 'running'"

} |

Select-Object DisplayName, Name, State, StartMode |

ConvertTo-Html |

Out-File C:\Dv\Report.html

多个表

这个版本创建使用ConvertTo-Html -Fragment表的顺序。每个表格前面都有一个带有计算机名称的HTML标头(在本例中为h2)。单个表格在最后连接并合并成单个HTML文档,使用ConvertTo-Html的裸(不输入)调用。

# Generate the content which should be inserted as a set of HTML tables

$preContent = Get-Content "C:\Dv\Server_List.txt" -ErrorAction SilentlyContinue | ForEach-Object {

$ComputerName = $_

Write-Host "Automatic Services Stopped :" $ComputerName

# A table (and heading) will only be generated for $ComputerName if there are services matching the filter.

Get-WmiObject Win32_Service -ComputerName $ComputerName -Filter "startmode = 'auto' AND state != 'running'" |

Select-Object DisplayName, Name, State, StartMode |

ConvertTo-Html -PreContent "

$ComputerName

" -Fragment

}

# Generate the document which holds all of the individual tables.

$htmlDocument = ConvertTo-Html -Head $htmlHead -PreContent $preContent | Out-String

# Because the document has no input object it will have an empty table (

), this should be removed.

$htmlDocument -replace '

' | Out-File C:\Dv\Report.html

造型

你会发现这些生成的是相当原始的HTML。其中一个更好的方法来解决是使用CSS,这里是使HTML表格看起来更漂亮我的一个片段:

$HtmlHead = '

body {

background-color: white;

font-family: "Calibri";

}

table {

border-width: 1px;

border-style: solid;

border-color: black;

border-collapse: collapse;

width: 100%;

}

th {

border-width: 1px;

padding: 5px;

border-style: solid;

border-color: black;

background-color: #98C6F3;

}

td {

border-width: 1px;

padding: 5px;

border-style: solid;

border-color: black;

background-color: White;

}

tr {

text-align: left;

}

'

# Use the Head parameter when calling ConvertTo-Html

... | ConvertTo-Html -Head $HtmlHead | ...

注:当片段参数是提供与ConvertTo- 不的头仅适用HTML。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值