$ErrorActionPreference = "silentlycontinue" $a = New-Object -comobject Excel.Application $a.visible = $True $b = $a.Workbooks.Add() $c = $b.Worksheets.Item(1) $c.Cells.Item(1,1) = "Machine Name " $d = $c.UsedRange $d.Interior.ColorIndex = 19 $d.Font.ColorIndex = 11 $d.Font.Bold = $True $date = get-date -uformat "%Y%m%d" $save = "c:\powershell\diskspace_$date.xls" $intRow = 2 $exrow = 2 $VMrow = 1 $pass = cat c:\powershell\pw\pw1.txt | ConvertTo-SecureString $mycred1 = New-Object -TypeName system.Management.Automation.PSCredential -ArgumentList "domain\administrator",$pass get-content c:\powershell\files\servers.txt | ForEach-Object { gwmi win32_logicaldisk -Filter "drivetype = 3" -ComputerName $_ -Credential $mycred1 } | foreach-object { if($_.__server -eq $server) {$exrow = $exrow + 1 $introw = $introw-2} else { $exrow = 2} $server = $_.__server $c.Cells.Item($intRow,$exrow).Interior.ColorIndex = 4 $c.Cells.Item($intRow, 1) = $server $c.cells.item($introw, $exrow ) = $_.deviceid, '{0:0.0}GB' -f ($_.size / 1GB) $c.Cells.Item($intRow+1, $exrow) = '{0:0.0}GB' -f ($_.freespace / 1GB) if (($_.freespace / 1GB) -le 10) {$c.Cells.Item($intRow+1, $exrow).Interior.ColorIndex = 45} if (($_.freespace / 1GB) -le 2) {$c.Cells.Item($intRow+1, $exrow).Interior.ColorIndex = 29} if (($_.freespace / 1GB) -le 1) {$c.Cells.Item($intRow+1, $exrow).Interior.ColorIndex = 3} $introw = $introw + 2 } $c.cells.item($introw+1, 1) = "10GB or less" $c.Cells.Item($intRow+1, 1).Interior.ColorIndex = 45 $c.cells.item($introw+2, 1) = "2GB or less" $c.Cells.Item($intRow+2,1).Interior.ColorIndex = 29 $c.cells.item($introw+3, 1) = "1GB or less" $c.Cells.Item($intRow+3,1).Interior.ColorIndex = 3 $d.EntireColumn.AutoFit() $b.SaveAs($save) $a.quit() start-sleep -s 15 $mail = New-Object System.Net.Mail.MailMessage $att = new-object Net.Mail.Attachment($save) $mail.From = "sender " $mail.To.Add("receiver@e-mail.com") $mail.Subject = "Diskspace $date" $mail.Body = "The log file is attached" $mail.Attachments.Add($att) $smtp = New-Object System.Net.Mail.SmtpClient("mail-server") $smtp.Credentials = New-Object System.Net.NetworkCredential("domain\user","P@Ssw0rD") $smtp.Send($mail) $att.Dispose() remove-item "c:\powershell\diskspace.xls" exit