我正在尝试使用PowerShell为计算机列表中的服务列表生成报告。我能够生成报告,但这不是我的预期。
这是我的代码:
param ([string[]] $ServerPath,
[string[]] $ServicePath,
[string] $ENVName,
[string] $DataPath,
$csv=@()
)
$ServiceReport= "D:\Test\ServiceReport.htm"
New-Item -ItemType file $ServiceReport -Force
# Function to write the HTML Header to the file
Function writeHtmlHeader
{
param($ServiceReport)
$date = ( get-date ).ToString('yyyy/MM/dd')
Add-Content $ServiceReport "<html>"
Add-Content $ServiceReport "<head>"
Add-Content $ServiceReport "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>"
Add-Content $ServiceReport '<title>Service Status Report </title>'
add-content $ServiceReport '<STYLE TYPE="text/css">'
add-content $ServiceReport "<!--"
add-content $ServiceReport "td {"
add-content $ServiceReport "font-family: Tahoma;"
add-content $ServiceReport "font-size: 11px;"
add-content $ServiceReport "border-top: 1px solid #999999;"
add-content $ServiceReport "border-right: 1px solid #999999;"
add-content $ServiceReport "border-bottom: 1px solid #999999;"
add-content $ServiceReport "border-left: 1px solid #999999;"
add-content $ServiceReport "padding-top: 0px;"
add-content $ServiceReport "padding-right: 0px;"
add-content $ServiceReport "padding-bottom: 0px;"
add-content $ServiceReport "padding-left: 0px;"
add-content $ServiceReport "}"
add-content $ServiceReport "body {"
add-content $ServiceReport "margin-left: 5px;"
add-content $ServiceReport "margin-top: 5px;"
add-content $ServiceReport "margin-right: 0px;"
add-content $ServiceReport "margin-bottom: 10px;"
add-content $ServiceReport ""
add-content $ServiceReport "table {"
add-content $ServiceReport "border: thin solid #000000;"
add-content $ServiceReport "}"
add-content $ServiceReport "-->"
add-content $ServiceReport "</style>"
Add-Content $ServiceReport "</head>"
Add-Content $ServiceReport "<body>"
add-content $ServiceReport "<table width='100%'>"
add-content $ServiceReport "<tr bgcolor='#0000FF'>"
add-content $ServiceReport "<td colspan='4' height='25' align='center'>"
add-content $ServiceReport "<font face='tahoma' color='#7CFC00' size='4'><strong>Service Stauts Report - $date</strong></font>"
add-content $ServiceReport "</td>"
add-content $ServiceReport "</tr>"
add-content $ServiceReport "</table>"
}
# Function to write the HTML Header to the file
Function writeTableHeader
{
param($ServiceReport ,$ServerName)
Add-Content $ServiceReport "<tr bgcolor='#20B2AA'>"
Add-Content $ServiceReport "<td width='10%' align='center'><font face='tahoma' color='#7CFC00' size='4'>Service Name</font></td>"
Add-Content $ServiceReport "<td width='30%' align='center'><font face='tahoma' color='#7CFC00' size='4'>$ServerName</font></td>"
#Add-Content $ServiceReport "<td width='10%' align='center'><font face='tahoma' color='#7CFC00' size='4'>Status</font></td>"
Add-Content $ServiceReport "</tr>"
}
Function writeHtmlFooter
{
param($ServiceReport)
Add-Content $ServiceReport "</body>"
Add-Content $ServiceReport "</html>"
}
Function WriteServiceInfo
{
param($ServiceReport ,$Servicename,$Status)
Add-Content $ServiceReport "<tr bgcolor='#008B8B'>"
Add-Content $ServiceReport "<td bgcolor='#F0FFF0' align=left ><b><font face='tahoma' color='#003399' size='3'>$Servicename</font></td>"
#Add-Content $ServiceReport "<td bgcolor='#F0FFF0' align=left ><b><font face='tahoma' color='#003399' size='3'>$Servername</font></td>"
Add-Content $ServiceReport "<td bgcolor='#F0FFF0' align=center ><b><font face='tahoma' color='#003399' size='4'>$Status</font></td>"
Add-Content $ServiceReport "</tr>"
}
writeHtmlHeader $ServiceReport
Add-Content $ServiceReport "<table width='100%'><tbody>"
Add-Content $ServiceReport "<tr bgcolor='#008B8B'>"
Add-Content $ServiceReport "<td width='100%' align='center' colSpan=3><font face='tahoma' color='#003399' size='6'><strong> Service Status Report</strong></font></td>"
Add-Content $ServiceReport "</tr>"
writeTableHeader $ServiceReport
# import the CSV to get server list
$Serverlist = Import-Csv -path $ServerPath #| Where-Object {$_.IsActive -eq '1'}
$servers=$ServerList.ServerName
$ENV=$ServerList.ENV
# fetch the serverlist where service name match in the CSV
$ENVServer= (Import-Csv -path $ServerPath | Where-Object {$_.ENV -eq $ENVName}).ServerName
$ServicesList=(Import-Csv -Path $ServicePath).ServiceName
foreach($Server in $ENVServer){
$ServicesList=(Import-Csv -Path $ServicePath).ServiceName
ForEach($Service in $Serviceslist){
$servicestatus=Get-Service -ComputerName $Server | Where-Object {$_.ServiceName -like "$Service"}
$lenth=$servicestatus.length
#$state=$servicestatus.Status
if ($lenth -eq 0)
{
#do nothing
}
else {
writeTableHeader $ServiceReport $servicestatus.MachineName
WriteServiceInfo $ServiceReport $servicestatus.ServiceName $servicestatus.Status
}
}
}
输出:
Service Name Server Name Status
==================================
Service1 Server1 running
service2 server1 stopped
service1 server2 stoppped
预期输出:
ServiceName Server1 server2 server3 .........
================================================
Service1 runing stopped running
ervice2 stopped running stopped
service3 running stopped stopped
...... ...... ..... .....
.......
如果有上述任何可能,请帮助我。我一直在努力,但我身边没有运气。
谢谢, 希瓦
答案 0 :(得分:0)
您正在寻找类似的东西。
取消注释&lt;# - ComputerName $ Computer#&gt;部分让它活跃起来;) 它是在我自己的机器上进行测试的。
$Servicelist = @("WinRM", "Winmgmt", "DHCP")
$Computerlist = @("LocalHost", "Server1","Server2","Server3")
$ServiceStatus = @{}
Foreach ($Computer in $Computerlist)
{
$ServicesFound = Get-Service <#-ComputerName $Computer#> | Where-Object {$Servicelist -contains $_.Name} | Select Name, Status
Foreach ($Service in $ServicesFound)
{
If(!($ServiceStatus["$($Service.Name)"])) {
$ServiceStatus["$($Service.Name)"] = $Service | Select @{N='Service'; E={$($Service.Name)}}, @{N="$Computer"; E={$Service.Status}}
} else {
$ServiceStatus["$($Service.Name)"] = $ServiceStatus["$($Service.Name)"] | Select *, @{N="$Computer"; E={$Service.Status}}
}
}
}
$ServiceStatus.Values | ConvertTo-Html -Fragment
HTML输出
<table>
<colgroup><col/><col/><col/><col/><col/></colgroup>
<tr><th>Service</th><th>LocalHost</th><th>Server1</th><th>Server2</th><th>Server3</th></tr>
<tr><td>Dhcp</td><td>Running</td><td>Running</td><td>Running</td><td>Running</td></tr>
<tr><td>WinRM</td><td>Stopped</td><td>Stopped</td><td>Stopped</td><td>Stopped</td></tr>
<tr><td>Winmgmt</td><td>Running</td><td>Running</td><td>Running</td><td>Running</td></tr>
</table>
看起来像这样
Service LocalHost Server1 Server2 Server3
=========================================
Dhcp Running Running Running Running
WinRM Stopped Stopped Stopped Stopped
Winmgmt Running Running Running Running
答案 1 :(得分:-1)
此时提供的唯一建议是探索Format-Table,大多数返回PowerShell结果的命令都可以通过管道传输到格式表,如下所示
get-process | Format-Table
这是一个链接,在以特定方式格式化表格以查看结果时提供了大量信息
https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.utility/format-table