Solved

Powershell formatting text output to e-mail

Posted on 2010-09-06
6
959 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
powershell query 2 25
Exchange 2013 powershell add missing domain to specific users 1 24
powershell question need assistance 10 32
problem with default throttling policy 2 23
Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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