1

I'm trying to add the properties of the object $mailBox_MBS to the object $mailBox_RP.

I used the following code to define the objects:

$mailBox_MBS = (Get-MailboxStatistics -Identity $identity) | select *
$mailBox_RP = (Get-Recipient -Identity $identity) | select *

Then I used the following ForEach loop to add the properties from mailBox_MBS to mailBox_RP:

Foreach($property in $mailbox_MBS)    
{     
  $mailBox_RP | Add-Member -MemberType NoteProperty -Name $property.Name -Value $property.Value 
}

3 Answers 3

2

You can't do it the way you're trying to do. Your foreach will only see a single object and you're only catching the name and value properties. What you want to do is actually iterate the properties of an object. Keep in mind that you might loose object complexity while doing this (implicit conversations to strings). Especially with Exchange this can be a problem.

You could either create a custom PSObject to just contain what you need or you'd need to look into using Get-Member to get the actual object Information. That might look like this.

$object | Get-Member -MemberType Property | %{ $object.$($_.Name); }
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your reply, Seth. Look under the **Edit on my post. Is this what you mean by that? Unfortunately it's not working.
It was an either though certainly you could use that approach to create a custom object. Spend some time to check out what Get-Member does and check what your current code does. My example only shows a rough guideline and what you can do with Get-Member. It doesn't make sense that way to actually change an object. You'd need to combine it with your current approach. Currently you wouldn't even touch your original objects.
Thanks for your help Seth.
1

Let me know if this works for you:

$mailbox_MBS = Get-MailboxStatistics -Identity $identity
$mailbox_RP = Get-Recipient -Identity $identity

Get-Member -InputObject $mailbox_MBS -MemberType "*Property" | foreach {
    Add-Member -InputObject $mailbox_RP -Type "NoteProperty" -Name $_.Name -Value $mailbox_MBS.($_.Name)
}

1 Comment

Thanks for this, I managed to solve it. Very nice to see a different way to approach what I was looking for.
0

I managed to convert the code above into a working function, with the help of Seth's comment and my best friend Google.

Function mailBoxInfo ($identity)
{
    $ExchangeDirectoryObject = New-Object PSObject
    $mailbox_MBS = (Get-MailboxStatistics -Identity $identity) | select *
    $mailbox_RP = (Get-Recipient -Identity $identity) | select *

$mailBox_MBS.psobject.Properties | % {
    $ExchangeDirectoryObject | Add-Member -MemberType $_.MemberType -Name $_.Name -Value $_.Value
}
$mailBox_RP.psobject.Properties | % {
    $ExchangeDirectoryObject | Add-Member -MemberType $_.MemberType -Name $_.Name -Value $_.Value
}

return $ExchangeDirectoryObject
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.