Solved

Powershell

Posted on 2016-08-20
6
75 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
  • 3
6 Comments
 
LVL 78

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 39

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 39

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 39

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now