我有以下Powershell脚本来查询几个远程VM上的正常运行时间/磁盘空间/内存使用情况,但生成的结果被捆绑在一起,因为我将剩余的服务器添加到此脚本中(大约30/40多个待添加)数据开始变得不可读。
[CmdLetBinding()]
Param ( [Parameter(Mandatory=$false,Position=1)][string]$path = "C:\LON3 Web Server Report - $(get-date -f dd_MM_yyyy).html",
[Parameter(Mandatory=$false,Position=2)][array]$servers = @("Svr1","Svr2","Svr3")
)
Function Get-UpTime
{ Param ([string[]]$servers)
Foreach ($s in $servers)
{
$os = Get-WmiObject -class win32_OperatingSystem -cn $s
New-Object psobject -Property @{
Uptime = ((get-date) - $os.converttodatetime($os.lastbootuptime)).Days
Hostname=$s
}
}
}
Function Get-DiskSpace
{
Param ([string[]]$servers)
Foreach ($s in $servers)
{
Get-WmiObject -Class win32_volume -cn $s |
Select-Object @{LABEL='Server';EXPRESSION={$s}},
driveletter, label,
@{LABEL=" Total (GB)"; Expression = {" {0:N2} " -f ($_.capacity / 1GB)}},
@{LABEL=" Free Space (GB) ";EXPRESSION={" {0:N2} " -f ($_.freespace/1GB)}},
@{LABEL=" Percent Free (%) ";Expression = { "{0:N2} " -f (($_.FreeSpace / $_.Capacity)*100) }},
@{LABEL=" Percent Usage (%) ";Expression = {" {0:N2} " -f ((($_.Capacity - $_.freespace)/$_.Capacity)*100) }}
}
}
Function Get-MemoryUsage
{
Param ([string[]]$servers)
Foreach ($s in $servers)
{
Get-WmiObject -Class win32_OperatingSystem -cn $s |
Select-Object @{LABEL='Server';EXPRESSION={$s}},
@{LABEL=" Total Physical Memory (MB) "; Expression = {" {0:N0} " -f (($_.totalvisiblememorysize / 1024))}},
@{LABEL=" Free Physical Memory (MB) "; Expression = {" {0:N0} " -f (($_.freephysicalmemory / 1024))}},
@{LABEL=" Total Virtual Memory (MB) "; Expression = {" {0:N0} " -f (($_.totalvirtualmemorysize / 1024))}},
@{LABEL=" Free Virtual Memory (MB) "; Expression = {" {0:N0} " -f (($_.freevirtualmemory / 1024))}}
}
}
$upTime = Get-UpTime -servers $servers |
ConvertTo-Html -As Table -Fragment -PreContent "
Created on: $(get-date)
<h2>Server Uptime</h2> " | Out-String
$disk = Get-DiskSpace -servers $servers |
ConvertTo-Html -As Table -Fragment -PreContent "
<h2>Disk</h2> "| Out-String
$memory = Get-MemoryUsage -servers $servers |
ConvertTo-Html -As Table -Fragment -PreContent "
<h2>Memory Usage</h2> "| Out-String
$head = @'
<style media='screen'>
body {
font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
}
table{
border-collapse: collapse;
border: none;
font: 10pt Verdana, Geneva, Arial, Helvetica, sans-serif;
color: black;
margin-bottom: 10px;
}
table td{
font-size: 12px;
padding-left: 0px;
padding-right: 20px;
text-align: left;
}
table th {
font-size: 12px;
font-weight: bold;
padding-left: 0px;
padding-right: 20px;
text-align: left;
}
h2{ clear: both; font-size: 130%;color:#354B5E; }
h3{
clear: both;
font-size: 75%;
margin-left: 20px;
margin-top: 30px;
color:#475F77;
}
p{ margin-left: 20px; font-size: 12px; }
table.list{ float: left; }
table.list td:nth-child(1){
font-weight: bold;
border-right: 1px grey solid;
text-align: right;
}
table.list td:nth-child(2){ padding-left: 7px; }
table tr:nth-child(even) td:nth-child(even){ background: #BBBBBB; }
table tr:nth-child(odd) td:nth-child(odd){ background: #F2F2F2; }
table tr:nth-child(even) td:nth-child(odd){ background: #DDDDDD; }
table tr:nth-child(odd) td:nth-child(even){ background: #E5E5E5; }
div.column { width: 320px; float: left; }
div.first{ padding-right: 20px; border-right: 1px grey solid; }
div.second{ margin-left: 30px; }
table{ margin-left: 20px; }
–>
</style>
<style media='print'>
</style>
'@
ConvertTo-Html -Head $head -PreContent "<h1>Daily Status Report - SQL Servers</h1>" -PostContent $upTime, $disk, $memory >> $path
Invoke-Item $path
下面这张图片让您更容易理解。我基本上需要在每个服务器之间显示一个“新行”或“中断”(标记为“红色”行),但我无法弄清楚Powershell / HTML编码以实现此目的。
答案 0 :(得分:1)
完成分离的最简单方法可能只是在每个集合的底部添加一个空对象,Will至少会在每个服务器之间提供一个换行符。所以你可以在Get-DiskSpace
函数
Function Get-DiskSpace
{
Param ([string[]]$servers)
Foreach ($s in $servers)
{
Get-WmiObject -Class win32_volume -cn $s |
Select-Object @{LABEL='Server';EXPRESSION={$s}},
driveletter, label,
@{LABEL=" Total (GB)"; Expression = {" {0:N2} " -f ($_.capacity / 1GB)}},
@{LABEL=" Free Space (GB) ";EXPRESSION={" {0:N2} " -f ($_.freespace/1GB)}},
@{LABEL=" Percent Free (%) ";Expression = { "{0:N2} " -f (($_.FreeSpace / $_.Capacity)*100) }},
@{LABEL=" Percent Usage (%) ";Expression = {" {0:N2} " -f ((($_.Capacity - $_.freespace)/$_.Capacity)*100) }}
}
[PsCustomObject]@{Server = "";driveletter = "";label = ""; "Total (GB)" = ""; "Free Space (GB)" = ""; "Percent Free (%)" = ""; "Percent Usage (%)" = ""}
}
请注意,这可能不是最佳方式,因为很难将该方法扩展到其他脚本,但它可以实现您所需的目标。