Link to home
Start Free TrialLog in
Avatar of K B
K BFlag for United States of America

asked on

PowerShell: Use of subproperties in a Select statement

I get this error when I attempt to run this script... any ideas?  How does one expand then use the subproperty?

Select-Object : Cannot convert System.Object[] to one of the following types {System.String, System.Management.Automation.ScriptBlock}.
At C:\scripts\subtotal2.ps1:4 char:5
+     Select-Object -Unique -Property $target, @{
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Select-Object], NotSupportedException
    + FullyQualifiedErrorId : DictionaryKeyUnknownType,Microsoft.PowerShell.Commands.SelectObjectCommand

$report = Get-MoveRequest -ResultSize 5 | Get-MoveRequestStatistics | Select {$_.targetdatabase.name},totalmailboxsize
$target = $report.{$_.targetdatabase.name}
$report | Group-Object -Property $target | 
    Select-Object -Unique -Property $target, @{
    Label = "Sum";
    Expression = {
        # Sum all the counts for each domain
        ($PSItem.group | Measure-Object -Property totalmailboxsize -sum).Sum
    }
} |
    Sort-Object -Property Sum -Descending |
    Out-GridView -Title "Size Per Database"

Open in new window




Thank you.
Avatar of footech
footech
Flag of United States of America image

What is the type of $target?
$target.gettype().name
Hi KB

Just so I understand the purpose of the code you posted. It looks like you're attempting to query mailbox move request and gather the overall mailbox size being moved to specific targetmailboxdatabase(s).
ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of K B

ASKER

That is absolutely perfect! thank you!

Is there any way to make the Totalmailboxsize output as GBs?

And my main question is, where does $_.Name come from?  I see it used as the expression in the select-object command and cannot figure it out.

For example, when I execute these commands:

$report = Get-MoveRequest -ResultSize 5 | Get-MoveRequestStatistics | Select @{n="DatebaseName";e={$_.targetdatabase.name}},totalmailboxsize
$report | GM 

Open in new window


... there is no Name property.

I really appreciate your help.. this is great thank you again.
Avatar of K B

ASKER

I am guessing for the GB conversion?...

$report = Get-MoveRequest -ResultSize 5 | Get-MoveRequestStatistics | Select @{n = "DatabaseName"; e = {$_.targetdatabase.name}}, @{n='total';e={($_.totalmailboxsize/1GB)}}
$report | Group-Object -Property DatabaseName | 
    Select-Object -Property @{n = "DatabaseName"; e = {$_.Name}}, @{
    Label = "Sum";
    Expression = {
        ($PSItem.group | Measure-Object -Property total -sum).Sum
    }
} |
    Sort-Object -Property Sum -Descending |
    Out-GridView -Title "Size Per Database"

Open in new window



ohhh..

$report | Group-Object -Property databasename | GM

Open in new window

Avatar of K B

ASKER

Thank you again footech!
Yep, you've got it.  The Name property comes from Group-Object.

Is there any way to make the Totalmailboxsize output as GBs?
I could only answer that if I knew what the Totalmailboxsize property is currently.  If it's just a number of bytes, then you can create a new calculated property and give the value as Totalmailboxsize divided by 1GB.  So yes, your example/guess above would be my guess as well.