Solved

Powershell

Posted on 2016-08-20
6
104 Views
Last Modified: 2016-09-03
I can't use "ConvertTo-Html" to convert it into table format. Any idea ??

$Body =  Get-WmiObject Win32_LogicalDisk -filter "DriveType=3" -Computer (Get-Content .\computer.csv) -ErrorAction silentlycontinue | select SystemName, DeviceID, @{Name="Free (%)";Expression={"{0:N0}" -f (($_.freespace /1gb) / ($_.size /1gb) * 100) }}, @{Name="Size (GB)";Expression={"{0:N1}" -f($_.size/1gb)}},@{Name="Free (GB)";Expression={"{0:N1}" -f($_.freespace/1gb)}} | ConvertTo-Html  



$CredUser = "info@abc.com"
$CredPassword = "*****"

$EmailFrom = $CredUser
$EmailTo = $CredUser 
$Subject = "Servers Disk Space Analysis"

$SMTPServer = "smtp.office365.com"
$Body =  Get-WmiObject Win32_LogicalDisk -filter "DriveType=3" -Computer (Get-Content .\computer.csv) -ErrorAction silentlycontinue | select SystemName, DeviceID, @{Name="Free (%)";Expression={"{0:N0}" -f (($_.freespace /1gb) / ($_.size /1gb) * 100) }}, @{Name="Size (GB)";Expression={"{0:N1}" -f($_.size/1gb)}},@{Name="Free (GB)";Expression={"{0:N1}" -f($_.freespace/1gb)}} | ConvertTo-Html                         
$emailCreds = New-Object System.Management.Automation.PSCredential($CredUser,(ConvertTo-SecureString -String $CredPassword -AsPlainText -Force))
Send-MailMessage -from $CredUser -to $CredUser -Subject $Subject -Body $Body -Credential $emailCreds -UseSsl -SmtpServer $SMTPServer -Port 587

Open in new window

0
Comment
Question by:AXISHK
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
6 Comments
 
LVL 81

Expert Comment

by:David Johnson, CD, MVP
ID: 41763666
changed get-wmiobject to get-ciminstance to comply with WS-MAN
#Requires –Version 3
$body=@()
$computers = get-content .\computers.txt
foreach($computer in $computers) {
$driveinfo = Get-CimInstance -ComputerName $computer -ClassName Win32_LogicalDisk -filter 'DriveType=3' -ErrorAction silentlycontinue | Select-Object SystemName, DeviceID, @{Name='Free (%)';Expression={'{0:N0}' -f (($_.freespace /1gb) / ($_.size /1gb) * 100) }}, @{Name='Size (GB)';Expression={'{0:N1}' -f($_.size/1gb)}},@{Name='Free (GB)';Expression={'{0:N1}' -f($_.freespace/1gb)}} 
$body += $driveinfo
}
$body | convertto-html | out-file .\drivereport.html
start-process drivereport.html

Open in new window

this is a table.
so to plug into your code

#Requires –Version 3
$CredUser = 'info@abc.com'
$CredPassword = '*****'
$EmailFrom = $CredUser
$EmailTo = $CredUser
$Subject = 'Servers Disk Space Analysis'
$SMTPServer = 'smtp.office365.com'
$body=@()
$computers = get-content .\computers.txt
foreach($computer in $computers) {
$driveinfo = Get-CimInstance -ComputerName $computer -ClassName Win32_LogicalDisk -filter 'DriveType=3' -ErrorAction silentlycontinue | Select-Object SystemName, DeviceID, @{Name='Free (%)';Expression={'{0:N0}' -f (($_.freespace /1gb) / ($_.size /1gb) * 100) }}, @{Name='Size (GB)';Expression={'{0:N1}' -f($_.size/1gb)}},@{Name='Free (GB)';Expression={'{0:N1}' -f($_.freespace/1gb)}} 
$body += $driveinfo
}
$Body_html  = $body| ConvertTo-Html                        
$emailCreds = New-Object System.Management.Automation.PSCredential($CredUser,(ConvertTo-SecureString -String $CredPassword -AsPlainText -Force))
Send-MailMessage -from $CredUser -to $CredUser -Subject $Subject -Body $Body_html -Credential $emailCreds -UseSsl -SmtpServer $SMTPServer -Port 587 

Open in new window

0
 
LVL 40

Accepted Solution

by:
footech earned 500 total points
ID: 41763917
The only issue I see is that you're not using the -BodyAsHtml parameter for Send-MailMessage (David's is missing it too).
If you're constructing an HTML table, the email has to be HTML as well for the table to display properly.  If you're just sending a plain-text email, then the table can't be HTML (you could do something like | Format-Table | Out-String instead of ConvertTo-HTML, but you'd have to ensure that the email client displaying the message was using a fixed-width font or else the columns might not line up).
1
 
LVL 40

Expert Comment

by:footech
ID: 41764551
One other thing I forgot to point out.  Whatever you supply to the -Body parameter, it has to be a string (not an array of strings), so you should also pipe ConvertTo-Html to Out-String.
1
 
LVL 40

Expert Comment

by:footech
ID: 41764804
Sorry, no offense intended to anyone, but David's answer is demonstrably incorrect.  Changing Get-WmiObject to get Get-CimInstance has no effect on the returned data, just how it is retrieved.  The other changes affect how the returned data is collected into an array, but it's no better than in the original question.  The code does not address the question of displaying the data as a table in an email.

My two posts addressed the two changes needed to show the data as a table in an HTML email (presumably what the author wanted due to the inclusion of ConvertTo-Html), and also described what would be needed to show a table if sending a plain-text email instead.  For that reason, I'm going to have to object to how this question was closed.
1
 

Author Closing Comment

by:AXISHK
ID: 41782804
Thx
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question