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
Solved

Powershell

Posted on 2016-08-20
6
96 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 80

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
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 …

839 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