gpt4 book ai didi

powershell - Powershell脚本不一致

转载 作者:行者123 更新时间:2023-12-03 01:09:36 24 4
gpt4 key购买 nike

我正在使用一个脚本,该脚本对平均CPU和内存使用率执行基本的运行状况检查,并检查特定服务是否正在运行。输出将显示为HTML。如果CPU /内存利用率大于89%,或者该服务未运行,则受影响的服务器将显示为红色。该脚本可以正常工作,除了服务器即使在使用不足时仍会显示为红色的一些不一致之处。

这是我正在使用的powershell脚本:

$ServerListFile = "C:\Scripts\HealthCheck\ServerList.txt"
$ServerList = Get-Content $ServerListFile -ErrorAction SilentlyContinue
$Date = Get-Date -Format M.d.yyyy
$Result = @()

foreach ($computername in $ServerList) {

$AVGProc = Get-WmiObject -ComputerName $computername Win32_Processor |
Measure-Object -Property LoadPercentage -Average |
Select-Object Average
$OS = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $computername |
Select-Object @{Name = "MemoryUsage"; Expression = {"{0:N2}" -f ((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)*100)/ $_.TotalVisibleMemorySize) }}
$Service1 = Get-Service -ComputerName $computername -Name Service1
$Service2 = Get-Service -ComputerName $computername -Name Service2

$Result += [PSCustomObject] @{
ServerName = "$computername"
CPULoad = "$($AVGProc.Average)%"
MemLoad = "$($OS.MemoryUsage)%"
Service1 = "$($Service1.Status)"
Service2 = "$($Service2.Status)"
}

$Outputreport = "<HTML><TITLE> Server Health Check </TITLE>
<BODY background-color:peachpuff>
<font color =""#99000"" face=""Microsoft Tai le"">
<H2> Server Health Check </H2></font>
<Table border=1 cellpadding=7 cellspacing=0>
<font face=""Calibri"">
<TR bgcolor=white align=center>
<TD><B> Server Name </B></TD>
<TD><B> Avrg.CPU </B></TD>
<TD><B> Memory </B></TD>
<TD><B> Service1 </B></TD>
<TD><B> Service2</B></TD></TR>"

foreach ($Entry in $Result) {
if (($Entry.MemLoad -gt "89") -Or ($Entry.CPULoad -gt "89") -or ($Entry.Service1 -eq "Stopped") -or ($Entry.Service2 -eq "Stopped")) {
$Outputreport += "<TR bgcolor=red>"
} else {
$Outputreport += "<TR bgcolor=lightgreen>"
}
$Outputreport += "<TD>$($Entry.Servername)</TD><TD align=center>$($Entry.CPULoad)</TD><TD align=center>$($Entry.MemLoad)</TD><TD align=center>$($Entry.Service1)</TD><TD align=center>$($Entry.Service2)</TD></TR>"
}

$Outputreport += "</Table></BODY></HTML>"
}

$Outputreport | Out-File C:\Scripts\HealthCheck\ServerHealthCheck\HealthCheck_$Date.htm

谢谢!

最佳答案

当您真正对数值比较感兴趣时,请不要使用字符串比较-一串数字的序数不等于其数字对应项的序数。考虑以下比较操作:

PS C:\> 9 -gt 89
False
PS C:\> "9" -gt "89"
True

首先,更改结果对象,以百分比形式代替数字:
$Result += [PSCustomObject] @{
ServerName = "$computername"
CPULoad = $AVGProc.Average
MemLoad = $OS.MemoryUsage
Service1 = "$($Service1.Status)"
Service2 = "$($Service2.Status)"
}

然后更改 if语句以将它们与数字进行比较:
if (($Entry.MemLoad -gt 89) -Or ($Entry.CPULoad -gt 89) -or ($Entry.Service1 -eq "Stopped") -or ($Entry.Service2 -eq "Stopped"))

最后,在最终输出中添加 %符号:
 $Outputreport += "<TD>$($Entry.Servername)</TD><TD align=center>$($Entry.CPULoad)%</TD><TD align=center>$($Entry.MemLoad)%</TD><TD align=center>$($Entry.Service1)</TD><TD align=center>$($Entry.Service2)</TD></TR>" 

关于powershell - Powershell脚本不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41540087/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com