您需要做的就是附加到您的脚本:
$Result | Export-Csv "C:\Users\HOSDUM01\Desktop\Auto\Server_health_check\Test.csv" -NoType
或您要存储文件的任何路径
编辑将脚本重新编写为好消息。
简化路径使用
已插入function DriveFreePercent并删除了中间(不必要的)变量。
将html创建移到foreach之外
示例Csv文件
"ServerName","CPULoad","MemLoad","CDrive","EDrive","FDrive"
"HP-G1610","4","32,61","94,56","",""
## Q:\Test\2018\05\31\SO_50617742.ps1
$BaseDir = "C:\Users\HOSDUM01\Desktop\Auto\Server_health_check\"
$ServerListFile = Join-Path $BaseDir "Server_list.txt"
$ReportHtm = Join-Path $BaseDir "Server_Test.htm"
$ReportCsv = Join-Path $BaseDir "Server_Test.csv"
function DriveFreePercent {
param ([string]$ComputerName,
[string]$DriveLetter)
Get-WmiObject -Class win32_Volume -ComputerName $ComputerName -Filter "DriveLetter = '$($DriveLetter):'" |
ForEach {"{0:N2}" -f (($_.FreeSpace / $_.Capacity)*100) }
}
$ServerList = Get-Content $ServerListFile -ErrorAction SilentlyContinue
$Result = @()
ForEach($computername in $ServerList) {
$Result += [PSCustomObject] @{
ServerName = "$computername"
CPULoad = ("{0}" -f (Get-WmiObject -ComputerName $computername win32_processor|Measure LoadPercentage -Average).Average)
MemLoad = (gwmi -Class win32_operatingsystem -ComputerName $computername |
ForEach{"{0:N2}" -f ((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)*100)/$_.TotalVisibleMemorySize)})
CDrive = (DriveFreePercent $ComputerName C)
EDrive = (DriveFreePercent $ComputerName E)
FDrive = (DriveFreePercent $ComputerName F)
}
}
$Outputreport = "
Server Health ReportServer Health Report
EP_Server_Name | CPULOAD | MemLoad | C_Free | E_Free | F_Free |
Foreach($Entry in $Result) {
if( ($Entry.CpuLoad) -ge "80" -or
($Entry.Memload) -ge "80" -or
($Entry.Cdrive) -le "20" -or
($Entry.Edrive) -le "20" -or
($Entry.Fdrive) -le "20") {
$Outputreport += "
"} else {
$Outputreport += "
"}
$Outputreport += "
$($Entry.Servername)"$Outputreport += "
$($Entry.CPULoad)%"$Outputreport += "
$($Entry.MemLoad)%"$Outputreport += "
$($Entry.Cdrive)%"$Outputreport += "
$($Entry.Edrive)%"$Outputreport += "
$($Entry.Fdrive)%"$Outputreport += "
"}
$Outputreport | Out-File $ReportHtm
$Result | Export-Csv $ReportCsv -NoTypeInformation
#Invoke-Expression $ReportHtm
#gc $ReportCsv
##Send email functionality from below line, use it if you want
$smtpServer = "Random"
$smtpFrom = "test@test.com"
$smtpTo = "test@test.com"
$messageSubject = "Servers Health report"
$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$message.Subject = $messageSubject
$message.IsBodyHTML = $true
$message.Body = "
$style"
$message.Body += $Outputreport
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
#$smtp.Send($message)