Solved

Powershell

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Why would I want to create a function for tracking messages? I am glad you asked. As with most monotonous/routine tasks, human error tends to creep in after doing the same task over and over again. By creating a function, you load the function once…
This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 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

18 Experts available now in Live!

Get 1:1 Help Now