Все, что вам нужно сделать, это добавить к вашему сценарию:
$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","",""
<code>## 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 = "<HTML><TITLE> Server Health Report </TITLE>
<BODY background-color:peachpuff>
<font color =""#99000"" face=""Microsoft Tai le"">
<H2> Server Health Report </H2></font>
<Table border=1 cellpadding=0 cellspacing=0>
<TR bgcolor=Cyan align=center>
<TD><B>EP_Server_Name</B></TD>
<TD><B>CPULOAD</B></TD>
<TD><B>MemLoad</B></TD>
<TD><B>C_Free</B></TD>
<TD><B>E_Free</B></TD>
<TD><B>F_Free</B></TD></TR>"
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 += "<TR bgcolor=red>"
} else {
$Outputreport += "<TR bgcolor=green>"
}
$Outputreport += "<TD>$($Entry.Servername)</TD>"
$Outputreport += "<TD align=center>$($Entry.CPULoad)%</TD>"
$Outputreport += "<TD align=center>$($Entry.MemLoad)%</TD>"
$Outputreport += "<TD align=center>$($Entry.Cdrive)%</TD>"
$Outputreport += "<TD align=center>$($Entry.Edrive)%</TD>"
$Outputreport += "<TD align=center>$($Entry.Fdrive)%</TD></TR>"
$Outputreport += "</Table></BODY></HTML>"
}
$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 = "<head><pre>$style
"$ message.Body + = $ Outputreport$ smtp = New-Object Net.Mail.SmtpClient ($ smtpServer) # $ smtp.Send ($ message)