Solved

Powershell formatting text output to e-mail

Posted on 2010-09-06
6
954 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
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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

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…
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

19 Experts available now in Live!

Get 1:1 Help Now