Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Powershell formatting text output to e-mail

Posted on 2010-09-06
6
Medium Priority
?
983 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
  • 3
  • 2
6 Comments
 
LVL 42

Expert Comment

by:Meir Rivkin
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 2000 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
Lessons on Wi-Fi & Recommendations on KRACK

Simplicity and security can be a difficult  balance for any business to tackle. Join us on December 6th for a look at your company's biggest security gap. We will also address the most recent attack, "KRACK" and provide recommendations on how to secure your Wi-Fi network today!

 
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

WatchGuard Case Study: Museum of Flight

“With limited money and limited staffing, we didn’t have a lot of choices in terms of what we could do to bring efficiency. WatchGuard played a central part in changing that.” To provide strong, secure Wi-Fi access within the museum, Hunter chose to deploy WatchGuard’s AP120 APs.

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
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…
Loops Section Overview

885 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