Solved

Powershell formatting text output to e-mail

Posted on 2010-09-06
6
973 Views
Last Modified: 2012-05-10
This is a very basic and simple Powershell question, but I'm not sure what terms to search on and my MS Powershell book doesn't cover the topic.  Sorry I'm very much a Powershell newbie.

I want to format e-mails so they say something like:

Dear X, your Outbox folder is currently Y MB is size.  Please review.

The problem I'm getting is the variables are coming out like @{Name=X} @{$_.FolderAndSubFolderSize.ToMB()=Y

I know this must be the most basic of formatting questions, or I'm just not approaching it right.  Any suggestions?  Sample from the script and how I'm trying to extract the variable:

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin
foreach($mailbox in get-mailbox) {
      foreach($outbox in (Get-MailboxFolderStatistics -identity $mailbox -FolderScope Outbox | where {$_.FolderType -ne "User Created" -and $_.FolderAndSubfolderSize -gt 20000000})) {
            $name = $mailbox | select Name -last 1
            $folder = $outbox | select Identity
            $foldersize = $outbox | select {$_.FolderAndSubfolderSize.ToMB()}
            write-host "$name $foldersize"
      }
}
0
Comment
Question by:RenovoIT
[X]
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
  • 3
  • 2
6 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 33615580
try debug the script in PowerGui script editor and check the properties of $name and $foldersize.
it support intellisense so while debug you can dig in and view the object properties.
0
 
LVL 13

Accepted Solution

by:
soostibi earned 500 total points
ID: 33616197
Try this:
foreach($mailbox in get-mailbox) {
      foreach($outbox in (Get-MailboxFolderStatistics -identity $mailbox -FolderScope Outbox  | where {$_.FolderType -ne "User Created" -and $_.FolderAndSubfolderSize -gt 20000000})) {
            $name = $mailbox | select -expandproperty Name
            $folder = $outbox | select Identity
            $foldersize = $outbox | %{$_.FolderAndSubfolderSize.ToMB()}
            write-host "$name $foldersize"
      }
}

Open in new window

0
 

Author Closing Comment

by:RenovoIT
ID: 33617130
Looks like the % is key to just pulling the value, so should give me something to google.  So the following seems to work, I guess I just need to find out why (and buy a better book).

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin

foreach($mailbox in get-mailbox) {
      foreach($outbox in (Get-MailboxFolderStatistics -identity $mailbox -FolderScope Outbox | where {$_.FolderType -ne "User Created" -and $_.FolderAndSubfolderSize -gt 20000000})) {
            $name = %{$mailbox.Name}
            $folder = $outbox | select Identity
            $foldersize = %{$outbox.FolderAndSubfolderSize.ToMB()}
            write-host "$name $foldersize"
      }
}
0
IoT Devices - Fast, Cheap or Secure…Pick Two

The IoT market is growing at a rapid pace and manufacturers are under pressure to quickly provide new products. Can you be sure that your devices do what they're supposed to do, while still being secure?

 
LVL 13

Expert Comment

by:soostibi
ID: 33617378
An expression like this:
$yourcomplexobject | select-object oneproperty

creates a simple object from the complex one, but it is still an object, with one property. Converting an object to a string - which you did when you used write-host - is a risky operation, because you can not always predict what will happen, how the string will look like. In your case you got that string in the @{...} form.

To make PowerShell's life easy you should help him by creating the string or some other raw data type to write-host. What I did is:

$yourcomplexobject | foreach-object {$_.oneproperty.withsomemethod()}

(% is the alias for foreach-object) I selected the VALUE of the result of the method of the property, which was a number (raw data type), so there was no problem with converting it to a string.

And I also did:
$yourcomplexobject | select-object -expandproperty oneproperty

Which has very similar result to
$yourcomplexobject | %{$_.oneproperty }

With the expandproperty parameter select-object "grabs" the selected property to one level higher, so it is now not just a property of an object but THE object. If it is a string, there is no problem writing it to the console.
0
 

Author Comment

by:RenovoIT
ID: 33624737
Thanks for the additional information, this is what I assumed was happening.  From my very limited experience I think the object concept works really well in PowerShell.  Just being able to query one field without having to try and parse the information out is pretty good.  I'd randomly tried {$_.oneproperty.value} and $object.oneproperty.value to see if it works, but obviously not.  All queries on Google had come back with formating the text for console (fl, ft etc) and not returning just the value of a property.

I'd tried the -expandproperty but it didn't work, returning error messages.  But the % works perfectly.  Thanks again for your help.
0
 
LVL 13

Expert Comment

by:soostibi
ID: 33625266
select-object -expandproperty works really on properties, not on methods called on properties...
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

624 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