Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $disk = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='C:'" | Select-Object Size, FreeSpace
- Write-Host ("{0}GB total" -f [math]::truncate($disk.Size / 1GB))
- Write-Host ("{0}GB free on C: OS" -f [math]::truncate($disk.FreeSpace / 1GB))
- CompanyDC
- CompanyFileServer
- CompanyExchange
- CompanyRandomServer
- $cred = Get-Credential -Credential 'domainuser'
- $ServerName = Get-Content "C:tempservers.txt"
- $ConvertToGB = (1024 * 1024 * 1024)
- $enter1 = "`r"
- $enter2 = "`r`n"
- # Smtp deets
- $smtpServer = "relay.appriver.com"
- $smtpPort = "2525"
- $smtpFrom = "reporting@yourcompany.com"
- $smtpTo = "MyEmail@company.com"
- $messageSubject = "Daily Server Report"
- # Set up an SmtpClient
- $smtpClient = New-Object Net.Mail.SmtpClient
- $smtpClient.Host = $smtpServer
- $smtpClient.Port = $smtpPort
- # Create the MailMessage
- $mailMessage = New-Object Net.Mail.MailMessage
- $mailMessage.From = $smtpFrom
- $mailMessage.To.Add($smtpTo)
- $mailMessage.Subject = $messageSubject
- $mailMessage.IsBodyHtml = $true
- # style
- $htmlReport += "<style>"
- $htmlReport += "BODY{background-color:white;}"
- $htmlReport += "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
- $htmlReport += "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}"
- $htmlReport += "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}"
- $htmlReport += "</style>"
- # table
- $htmlReport += "<table>"
- $htmlReport += "`n"
- $htmlReport += "<tr>"
- $htmlReport += "<th>ServerName</th>"
- $htmlReport += "<th>Total Space</th>"
- $htmlReport += "<th>Free Space</th>"
- $htmlReport += "<th>Percent Free</th>"
- $htmlReport += "</tr>"
- foreach($Server in $ServerName)
- {
- $disk = Get-WmiObject -Credential $cred Win32_LogicalDisk -ComputerName $Server -Filter "DeviceID='C:'" | Select-Object Size,FreeSpace
- $htmlReport += "<tr>"
- $htmlReport += "<td>$($Server)</td>"
- $htmlReport += "<td>$([Math]::Truncate($disk.Size / $ConvertToGB)) GB </td>"
- $htmlReport += "<td>$([Math]::Truncate($disk.FreeSpace / $ConvertToGB)) GB </td>"
- if([Math]::Truncate(($disk.FreeSpace / $disk.size) * 100) -le 10)
- {
- $htmlReport += "<td><font color=red> $([Math]::Truncate(($disk.FreeSpace / $disk.size) * 100)) % </font></td>"
- }
- if([Math]::Truncate(($disk.FreeSpace / $disk.size) * 100) -gt 10 -and [Math]::Truncate(($disk.FreeSpace / $disk.size) * 100) -le 20)
- {
- $htmlReport += "<td><font color=orange> $([Math]::Truncate(($disk.FreeSpace / $disk.size) * 100)) % </font></td>"
- }
- if([Math]::Truncate(($disk.FreeSpace / $disk.size) * 100) -gt 20)
- {
- $htmlReport += "<td><font color=green> $([Math]::Truncate(($disk.FreeSpace / $disk.size) * 100)) % </font></td>"
- }
- $htmlReport += "</tr>"
- }
- $htmlReport += "</table>"
- # Now create an AlternateView from the HTML contents
- $messageBody = [Net.Mail.AlternateView]::CreateAlternateViewFromString($htmlReport, 'text/html')
- # Add the HTML view to the MailMessage
- $mailMessage.AlternateViews.Add($messageBody)
- # And finally send the message
- $smtpClient.Send($mailMessage)
- pause
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement