Solved

Powershell formatting text output to e-mail

Posted on 2010-09-06
6
952 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:sedgwick
Comment Utility
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
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 13

Expert Comment

by:soostibi
Comment Utility
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
Comment Utility
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
Comment Utility
select-object -expandproperty works really on properties, not on methods called on properties...
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This article will help you understand what HashTables are and how to use them in PowerShell.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 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

10 Experts available now in Live!

Get 1:1 Help Now