3

i am currently using:

$emailFrom = "[email protected]" $emailTo = "[email protected]" $subject = "subject" $body = "message" $smtpServer = "mail.host.com" $smtp = new-object Net.Mail.SmtpClient($smtpServer) $smtp.Send($emailFrom, $emailTo, $subject, $body) 

and i know that with the folowing command i can find out the free space on all of my hdd:

Get-WmiObject WIN32_logicaldisk | sort -desc freespace | select -first 3 | format-table -autosize deviceid,devicetype,providername,freespace,size,volumename; 

when i try to do this:

$body = Get-WmiObject WIN32_logicaldisk | sort -desc freespace | select -first 3 | format-table -autosize deviceid,devicetype,providername,freespace,size,volumename; 

but all i get in my email is the folowing:

 Microsoft.PowerShell.Commands.Internal.Format.FormatStartData Microsoft.PowerShell.Commands.Internal.Format.GroupStartData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.GroupEndData Microsoft.PowerShell.Commands.Internal.Format.FormatEndData 

how can i get my free space via email using powershell???

4 Answers 4

5

Try:

$body = Get-WmiObject WIN32_logicaldisk | sort -desc freespace | select -first 3 | format-table -autosize deviceid,devicetype,providername,freespace,size,volumename | out-string

I've just tried it on my PC and it worked.

For some background on why this works see:

http://blogs.msdn.com/powershell/archive/2006/04/25/how-does-select-string-work-with-pipelines-of-objects.aspx

JR

3
  • +1 - out-string is a good trick; that's going in my bag of tricks. Commented May 29, 2009 at 13:38
  • out-string is the answer. You can't really use format-table if you're assigning...it's really meant for output to the console. Commented May 29, 2009 at 14:36
  • works like a charm :) Commented May 29, 2009 at 18:41
3

You can try this:

$body_array = Get-WmiObject WIN32_logicaldisk | sort -desc freespace | select -first 3 | format-table -autosize deviceid,devicetype,providername,freespace,size,volumename; $body = $body_array.ToString(); 
1
  • nope. ain't working, all i get in my email now it's System.Object[] Commented May 29, 2009 at 18:38
1

You could possibly adapt from my post in this thread.

1
  • i will try, but i haven't been able to email the contents of a variable like $body = Get-WmiObject WIN32_logicaldisk i assume there's someting i am missing, but i must mention i have just started using powershell last week Commented May 29, 2009 at 10:07
0

PowerShell is taking a stream of objects from Get-WMIObject, sorting and selecting a subset, then producing a text rendition of the remaining objects-- not just working with pure text. If you want to get a pure text rendition, you can just change your call to format-table to use select-object instead.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.