Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 包含多个特定服务的win32_服务的格式化HTML报告_Powershell - Fatal编程技术网

Powershell 包含多个特定服务的win32_服务的格式化HTML报告

Powershell 包含多个特定服务的win32_服务的格式化HTML报告,powershell,Powershell,我陷入了筛选/格式化问题 目标: 提供导出为HTML的客户可读输出,以便于查看 所需: 筛选多个服务及其要在报告中显示的数据 问题: 使用win32_服务以所需形式收集/解析数据,以查询特定服务 #CODE # First lets create a text file, where we will later save the Service Health info $ServiceHealthFileName = "ServiceHealth.htm" $serverlist

我陷入了筛选/格式化问题

目标: 提供导出为HTML的客户可读输出,以便于查看

所需: 筛选多个服务及其要在报告中显示的数据

问题: 使用win32_服务以所需形式收集/解析数据,以查询特定服务

#CODE
  # First lets create a text file, where we will later save the Service Health  info
  $ServiceHealthFileName = "ServiceHealth.htm"
  $serverlist = "mylistofservers.txt"
  $warning = "Stopped"
  New-Item -ItemType file $ServiceHealthFileName -Force

# Function to write the HTML Header to the file
Function writeHtmlHeader
{
param($fileName)
$date = ( get-date ).ToString('yyyy/MM/dd')
Add-Content $fileName "<html>"
Add-Content $fileName "<head>"
Add-Content $fileName "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>"
Add-Content $fileName '<title> Server Health</title>'
add-content $fileName '<STYLE TYPE="text/css">'
add-content $fileName  "<!--"
add-content $fileName  "td {"
add-content $fileName  "font-family: Tahoma;"
add-content $fileName  "font-size: 11px;"
add-content $fileName  "border-top: 1px solid #999999;"
add-content $fileName  "border-right: 1px solid #999999;"
add-content $fileName  "border-bottom: 1px solid #999999;"
add-content $fileName  "border-left: 1px solid #999999;"
add-content $fileName  "padding-top: 0px;"
add-content $fileName  "padding-right: 0px;"
add-content $fileName  "padding-bottom: 0px;"
add-content $fileName  "padding-left: 0px;"
add-content $fileName  "}"
add-content $fileName  "body {"
add-content $fileName  "margin-left: 5px;"
add-content $fileName  "margin-top: 5px;"
add-content $fileName  "margin-right: 0px;"
add-content $fileName  "margin-bottom: 10px;"
add-content $fileName  ""
add-content $fileName  "table {"
add-content $fileName  "border: thin solid #000000;"
add-content $fileName  "}"
add-content $fileName  "-->"
add-content $fileName  "</style>"
Add-Content $fileName "</head>"
Add-Content $fileName "<body>"

add-content $fileName  "<table width='100%'>"
add-content $fileName  "<tr bgcolor='#CCCCCC'>"
add-content $fileName  "<td colspan='7' height='25' align='center'>"
add-content $fileName  "<font face='tahoma' color='#003399' size='4'><strong> Server Health - $date</strong></font>"
add-content $fileName  "</td>"
add-content $fileName  "</tr>"
add-content $fileName  "</table>"

}

# Function to write the HTML Header to the file
Function writeTableHeader
{
param($fileName)

Add-Content $fileName "<tr bgcolor=#CCCCCC>"
Add-Content $fileName "<td width='10%' align='center'>Name</td>"
Add-Content $fileName "<td width='50%' align='center'>ProcessId</td>"
Add-Content $fileName "<td width='10%' align='center'>State</td>"
Add-Content $fileName "<td width='10%' align='center'>StartMode</td>"
Add-Content $fileName "<td width='10%' align='center'>ExitCode</td>"
Add-Content $fileName "<td width='10%' align='center'>Status</td>"
Add-Content $fileName "</tr>"
}

Function writeHtmlFooter
{
param($fileName)

Add-Content $fileName "</body>"
Add-Content $fileName "</html>"
}

Function writeServiceInfo
{
param($fileName,$SvcName,$SvcPID,$SvcSM,$SvcExCd,$ServiceState,$SvcStatus)
$SvcName= $Item.Name
$SvcPID= $Item.ProcessId
$SvcSM= $Item.StartMode
$SvcExCd= $Item.ExitCode
$ServiceState= $Item.State
$SvcStatus= $Item.Status
 if ($ServiceState -eq $warning)
 {
 Add-Content $fileName "<tr>"
 Add-Content $fileName "<td>$SvcName</td>"
 Add-Content $fileName "<td>$SvcPID</td>"
 Add-Content $fileName "<td bgcolor='#FBB917' align=center>$ServiceState</td>"
 # #FBB917
 Add-Content $fileName "<td>$SvcSM</td>"
 Add-Content $fileName "<td>$SvcExCd</td>"
 Add-Content $fileName "<td>$SvcStatus</td>"
 Add-Content $fileName "</tr>"
 }
 else
 {
 Add-Content $fileName "<tr>"
 Add-Content $fileName "<td>$SvcName</td>"
 Add-Content $fileName "<td>$SvcPID</td>"
 Add-Content $fileName "<td>$ServiceState</td>"
 Add-Content $fileName "<td>$SvcSM</td>"
 Add-Content $fileName "<td>$SvcExCd</td>"
 Add-Content $fileName "<td>$SvcStatus</td>"
 Add-Content $fileName "</tr>"
 }
}

#Function sendEmail 
#{ param($from,$to,$subject,$smtphost,$htmlFileName) 
#$body = Get-Content $htmlFileName 
#$smtp= New-Object System.Net.Mail.SmtpClient $smtphost 
#$msg = New-Object System.Net.Mail.MailMessage $from, $to, $subject, $body 
#$msg.isBodyhtml = $true 
#$smtp.send($msg) 



writeHtmlHeader $ServiceHealthFileName
foreach ($server in Get-Content $serverlist)
{
 Add-Content $ServiceHealthFileName "<table width='100%'><tbody>"
 Add-Content $ServiceHealthFileName "<tr bgcolor='#CCCCCC'>"
 Add-Content $ServiceHealthFileName "<td width='100%' align='center' colSpan=6><font face='tahoma' color='#003399' size='2'><strong> $server </strong></font></td>"
 Add-Content $ServiceHealthFileName "</tr>"

 writeTableHeader $ServiceHealthFileName

 $store = Get-WmiObject -Class Win32_Service -ComputerName $name -Filter "Name LIKE 'ss0'" ;Get-WmiObject -Class Win32_Service -ComputerName $name -Filter "Name LIKE 'ss1'";Get-WmiObject -Class Win32_Service -ComputerName $name -Filter "Name LIKE 'ss2'";Get-WmiObject -Class Win32_Service -ComputerName $name -Filter "Name LIKE 'ss3'";Get-WmiObject -Class Win32_Service -ComputerName $name -Filter "Name LIKE 'ss4'";Get-WmiObject -Class Win32_Service -ComputerName $name -Filter "Name LIKE 'ss5'";Get-WmiObject -Class Win32_Service -ComputerName $name -Filter "Name LIKE 'ss6'";Get-WmiObject -Class Win32_Service -ComputerName $name -Filter "Name LIKE 'ss7'" 
#The below would not let me create objects in the store it had an error.
 #store = Get-WmiObject -Class Win32_Service -ComputerName $name -Filter "Name LIKE 'ss0'"
 #$store = $store + Get-WmiObject -Class Win32_Service -ComputerName $name -Filter "Name LIKE 'ss1'" 
 #$store = $store + Get-WmiObject -Class Win32_Service -ComputerName $name -Filter "Name LIKE 'ss2'" 
 #$store = $store + Get-WmiObject -Class Win32_Service -ComputerName $name -Filter "Name LIKE 'ss3'" 
 #$store = $store + Get-WmiObject -Class Win32_Service -ComputerName $name -Filter "Name LIKE 'ss4'" 
 #$store = $store + Get-WmiObject -Class Win32_Service -ComputerName $name -Filter "Name LIKE 'ss5'" 
 #$store = $store + Get-WmiObject -Class Win32_Service -ComputerName $name -Filter "Name LIKE 'ss6'" 
 #$store = $store + Get-WmiObject -Class Win32_Service -ComputerName $name -Filter "Name LIKE 'ss7'"
 foreach ($item in $store)
 {
 Write-Host  $item.Name $item.Name $item.FreeSpace $item.Size
 writeServiceInfo $ServiceHealthFileName $item.Name $item.ProcessId $item.State $item.StartMode $Item.ExitCode $Item.Status

 }
}
writeHtmlFooter $ServiceHealthFileName
$date = ( get-date ).ToString('yyyy/MM/dd')
# First lets create a text file, where we will later save the Service Health info
$ServiceHealthFileName = "ServiceHealth.htm"
$serverlist = "yourserverlist.txt"
$warning = "Stopped"
New-Item -ItemType file $ServiceHealthFileName -Force

# Function to write the HTML Header to the file
Function writeHtmlHeader
{
param($fileName)
$date = ( get-date ).ToString('yyyy/MM/dd')
Add-Content $fileName "<html>"
Add-Content $fileName "<head>"
Add-Content $fileName "<meta http-equiv='Content-Type' content='text/html;    charset=iso-8859-1'>"
Add-Content $fileName '<title> Server Health</title>'
add-content $fileName '<STYLE TYPE="text/css">'
add-content $fileName  "<!--"
add-content $fileName  "td {"
add-content $fileName  "font-family: Tahoma;"
add-content $fileName  "font-size: 11px;"
add-content $fileName  "border-top: 1px solid #999999;"
add-content $fileName  "border-right: 1px solid #999999;"
add-content $fileName  "border-bottom: 1px solid #999999;"
add-content $fileName  "border-left: 1px solid #999999;"
add-content $fileName  "padding-top: 0px;"
add-content $fileName  "padding-right: 0px;"
add-content $fileName  "padding-bottom: 0px;"
add-content $fileName  "padding-left: 0px;"
add-content $fileName  "}"
add-content $fileName  "body {"
add-content $fileName  "margin-left: 5px;"
add-content $fileName  "margin-top: 5px;"
add-content $fileName  "margin-right: 0px;"
add-content $fileName  "margin-bottom: 10px;"
add-content $fileName  ""
add-content $fileName  "table {"
add-content $fileName  "border: thin solid #000000;"
add-content $fileName  "}"
add-content $fileName  "-->"
add-content $fileName  "</style>"
Add-Content $fileName "</head>"
Add-Content $fileName "<body>"
add-content $fileName  "<table width='100%'>"
add-content $fileName  "<tr bgcolor='#CCCCCC'>"
add-content $fileName  "<td colspan='7' height='25' align='center'>"
add-content $fileName  "<font face='tahoma' color='#003399' size='4'><strong> Server Health - $date</strong></font>"
add-content $fileName  "</td>"
add-content $fileName  "</tr>"
add-content $fileName  "</table>"

}

# Function to write the HTML Header to the file
Function writeTableHeader
{
param($fileName)

Add-Content $fileName "<tr bgcolor=#CCCCCC>"
Add-Content $fileName "<td width='10%' align='center'>Name</td>"
Add-Content $fileName "<td width='50%' align='center'>ProcessId</td>"
Add-Content $fileName "<td width='10%' align='center'>State</td>"
Add-Content $fileName "<td width='10%' align='center'>StartMode</td>"
Add-Content $fileName "<td width='10%' align='center'>ExitCode</td>"
Add-Content $fileName "<td width='10%' align='center'>Status</td>"
Add-Content $fileName "</tr>"
}

Function writeHtmlFooter
{
param($fileName)

Add-Content $fileName "</body>"
Add-Content $fileName "</html>"
}

Function writeServiceInfo
{
param($fileName,$SvcName,$SvcPID,$SvcSM,$SvcExCd,$ServiceState,$SvcStatus)
$SvcName= $Item.Name
$SvcPID= $Item.ProcessId
$SvcSM= $Item.StartMode
$SvcExCd= $Item.ExitCode
$ServiceState= $Item.State
$SvcStatus= $Item.Status
#You can add multiple elseif statements if you wish to display certain events. I have server state of STOPPED in Red.
if ($ServiceState -eq $warning)
{
Add-Content $fileName "<tr>"
Add-Content $fileName "<td>$SvcName</td>"
Add-Content $fileName "<td>$SvcPID</td>"
Add-Content $fileName "<td bgcolor='#FF4C4C'  align=center>$ServiceState</td>"
#FF4C4C RED #FBB917 ORANGE
Add-Content $fileName "<td>$SvcSM</td>"
Add-Content $fileName "<td>$SvcExCd</td>"
Add-Content $fileName "<td>$SvcStatus</td>"
Add-Content $fileName "</tr>"
}
else
{
Add-Content $fileName "<tr>"
Add-Content $fileName "<td>$SvcName</td>"
Add-Content $fileName "<td>$SvcPID</td>"
Add-Content $fileName "<td>$ServiceState</td>"
Add-Content $fileName "<td>$SvcSM</td>"
Add-Content $fileName "<td>$SvcExCd</td>"
Add-Content $fileName "<td>$SvcStatus</td>"
Add-Content $fileName "</tr>"
}
}

#The function for the sendemail at the end.

#Function sendEmail 
#{ param($from,$to,$subject,$smtphost,$htmlFileName) 
#$body = Get-Content $htmlFileName 
#$smtp= New-Object System.Net.Mail.SmtpClient $smtphost 
#$msg = New-Object System.Net.Mail.MailMessage $from, $to, $subject, $body 
#$msg.isBodyhtml = $true
#$smtp.send($msg) 



writeHtmlHeader $ServiceHealthFileName
foreach ($server in Get-Content $serverlist)
{
 #This adds the Uptime for the server you will need $Display in your add-content.
 $hostname = get-wmiobject win32_computersystem -computername $server | fl model
 $os = Get-WmiObject win32_operatingsystem -ComputerName $server
 $uptime = (Get-Date) - ($os.ConvertToDateTime($os.lastbootuptime))
 $Display = "Uptime: " + $Uptime.Days + " days, " + $Uptime.Hours + " hours, " + $Uptime.Minutes + " minutes"

 #This builds the header for each server and include the name and uptime.
 Add-Content $ServiceHealthFileName "<table width='100%'><tbody>"
 Add-Content $ServiceHealthFileName "<tr bgcolor='#CCCCCC'>"
 Add-Content $ServiceHealthFileName "<td width='100%' align='center' colSpan=6><font face='tahoma' color='#003399' size='2'><strong> $server $Display </strong></font></td>"
 Add-Content $ServiceHealthFileName "</tr>"

 #This writes the header and builds the body of each server health
 writeTableHeader $ServiceHealthFileName
 $store = @()
 $store += Get-WmiObject -Class Win32_Service -ComputerName $server -Filter "Name LIKE 'yourservice'"
 $store += Get-WmiObject -Class Win32_Service -ComputerName $server -Filter "Name LIKE 'yourservice'"
 $store += Get-WmiObject -Class Win32_Service -ComputerName $server -Filter "Name LIKE 'yourservice'"
 $store += Get-WmiObject -Class Win32_Service -ComputerName $server -Filter "Name LIKE 'yourservice'"
 $store += Get-WmiObject -Class Win32_Service -ComputerName $server -Filter "Name LIKE 'yourservice'"
 $store += Get-WmiObject -Class Win32_Service -ComputerName $server -Filter "Name LIKE 'yourservice'"
 $store += Get-WmiObject -Class Win32_Service -ComputerName $server -Filter "Name LIKE 'yourservice'"
 $store += Get-WmiObject -Class Win32_Service -ComputerName $server -Filter "Name LIKE 'yourservice'"

 #This write the body of each header in the order you defined
 foreach ($item in $store)
 {
 #If you dont want on screen read out as the script progresses; comment out the write-host line.
 #Write-Host  $server $item.Name $item.ProcessId $item.State
 writeServiceInfo $ServiceHealthFileName $item.Name $item.ProcessId    $item.State $item.StartMode $Item.ExitCode $Item.Status
 }
 }
#This writes the footer
writeHtmlFooter $ServiceHealthFileName
$date = ( get-date ).ToString('yyyy/MM/dd')
#Use the below line to setup an email of the report,

#sendEmail emailaddress emailaddress "Server Status Report - $Date" EmailServer $ServiceHealthFileName
#代码
#首先让我们创建一个文本文件,稍后我们将在其中保存服务运行状况信息
$ServiceHealthFileName=“ServiceHealth.htm”
$serverlist=“mylistofservers.txt”
$warning=“已停止”
新项目-项目类型文件$ServiceHealthFileName-强制
#函数将HTML头写入文件
函数writeHtmlHeader
{
参数($fileName)
$date=(获取日期).ToString('yyyy/MM/dd')
添加内容$fileName“”
添加内容$fileName“”
添加内容$fileName“”
添加内容$fileName“服务器运行状况”
添加内容$fileName“”
添加内容$fileName“”
添加内容$fileName“”
添加内容$fileName“”
添加内容$fileName“”
添加内容$fileName“”
添加内容$fileName“”
添加内容$fileName“”
添加内容$fileName“服务器运行状况-$date”
添加内容$fileName“”
添加内容$fileName“”
添加内容$fileName“”
}
#函数将HTML头写入文件
函数writeTableHeader
{
参数($fileName)
添加内容$fileName“”
添加内容$fileName“Name”
添加内容$fileName“ProcessId”
添加内容$fileName“State”
添加内容$fileName“StartMode”
添加内容$fileName“ExitCode”
添加内容$fileName“状态”
添加内容$fileName“”
}
函数writeHtmlFooter
{
参数($fileName)
添加内容$fileName“”
添加内容$fileName“”
}
函数writeServiceInfo
{
参数($fileName、$SvcName、$SvcPID、$SvcSM、$svcexd、$ServiceState、$SvcStatus)
$SvcName=$Item.Name
$SvcPID=$Item.ProcessId
$SvcSM=$Item.StartMode
$svcexd=$Item.ExitCode
$ServiceState=$Item.State
$SvcStatus=$Item.Status
if($ServiceState-eq$警告)
{
添加内容$fileName“”
添加内容$fileName“$SvcName”
添加内容$fileName“$SvcPID”
添加内容$fileName“$ServiceState”
##FBB917
添加内容$fileName“$SvcSM”
添加内容$fileName“$svcexd”
添加内容$fileName“$SvcStatus”
添加内容$fileName“”
}
其他的
{
添加内容$fileName“”
添加内容$fileName“$SvcName”
添加内容$fileName“$SvcPID”
添加内容$fileName“$ServiceState”
添加内容$fileName“$SvcSM”
添加内容$fileName“$svcexd”
添加内容$fileName“$SvcStatus”
添加内容$fileName“”
}
}
#发送电子邮件的功能
#{param($from,$to,$subject,$smtphost,$htmlFileName)
#$body=获取内容$htmlFileName
#$smtp=新对象System.Net.Mail.SmtpClient$smtphost
#$msg=New Object System.Net.Mail.MailMessage$from,$to,$subject,$body
#$msg.isBodyhtml=$true
#$smtp.send($msg)
writeHtmlHeader$ServiceHealthFileName
foreach($Get Content$serverlist中的服务器)
{
添加内容$ServiceHealthFileName“”
添加内容$ServiceHealthFileName“”
添加内容$ServiceHealthFileName“$server”
添加内容$ServiceHealthFileName“”
writeTableHeader$ServiceHealthFileName
$store=Get-WmiObject-Class Win32_服务-ComputerName$name-Filter“名称类似于'ss0'”;Get-WmiObject-Class Win32_服务-ComputerName$name-Filter“名称类似于'ss1'”;Get-WmiObject-Class Win32_服务-ComputerName$name-Filter“名称类似于'ss3'”;Get-WmiObject-Class Win32_服务-ComputerName$name-Filter“像‘ss4’一样的名称”;Get-WmiObject-Class Win32_服务-ComputerName$name-Filter“像‘ss5’一样的名称”;Get-WmiObject-Class Win32_服务-ComputerName$name-Filter“像‘ss7’一样的名称”
#下面的命令不允许我在存储中创建对象,因为它有一个错误。
#store=Get WmiObject-Class Win32_服务-ComputerName$name-Filter“类似于'ss0'的名称”
#$store=$store+Get WmiObject-Class Win32_服务-ComputerName$name-Filter“类似于'ss1'的名称”
#$store=$store+Get WmiObject-Class Win32_服务-ComputerName$name-Filter“类似于'ss2'的名称”
#$store=$store+Get WmiObject-Class Win32_服务-ComputerName$name-Filter“类似于'ss3'的名称”
#$store=$store+Get WmiObject-Class Win32_服务-ComputerName$name-Filter“类似于'ss4'的名称”
#$store=$store+Get WmiObject-Class Win32_服务-ComputerName$name-Filter“类似于'ss5'的名称”
#$store=$store+Get WmiObject-Class Win32_服务-ComputerName$name-Filter“类似于'ss6'的名称”
#$store=$store+Get WmiObject-Class Win32_服务-ComputerName$name-Filter“类似于'ss7'的名称”
foreach($store中的项目)
{
写入主机$item.Name$item.Name$item.FreeSpace$item.Size
writeServiceInfo$ServiceHealthFileName$item.Name$item.ProcessId$item.State$item.StartMode$item.ExitCode$item.Status
}
}
writeHtmlFooter$ServiceHealthFileName
$date=(获取日期).ToString('yyyy/MM/dd')
我的输出文件正在正确填充,但仅适用于第一个服务。 我得到的服务器名称和所有状态正是我想要的。 我不确定我错过了什么


出于尊重,这是我从一位同事那里得到的一个diskspace脚本的切碎版本。感谢我不认识的原始作者。

您只将一个服务对象存储到
$store
变量中,其余的只是发送到管道中。制作
$store
一个数组,然后添加e它的元素

$store = @()
$store += Get-WmiObject -Class Win32_Service -ComputerName $name -Filter "Name LIKE 'ss0'"
$store += Get-WmiObject -Class Win32_Service -ComputerName $name -Filter "Name LIKE 'ss1'"

另外,请不要使用
将多行串成一行,除非您有很好的理由这样做(这在PowerShell中几乎从未出现过),它只会使您的代码变得非常不清晰,并且不会做任何回车符不能做的事情。

完成的代码。我在报告中添加了正常运行时间。感谢大家的帮助@MikeGrauccio。由于这是从另一个作者的DiskSpace脚本修改而来的,如果您看到这一点,谢谢
wmic /OUTPUT:%userprofile%\Desktop\%computername%_svc.html SERVICE list full /format:htable