Solved

Powershell

Posted on 2016-08-20
6
95 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 79

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Utilizing an array to gracefully append to a list of EmailAddresses
Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

803 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