PS: how do I get one hashtable of two results/queries?


This works ...
$CasMailbox = Get-CASMailbox $username | select abc
$Mailbox =  Get-mailbox $username | select xyz
$Result = $CasMailbox,$Mailbox

but it creates two object
$Result | gm
TypeName: Selected.Microsoft.Exchange.Data.Directory.Management.CASMailbox
TypeName: Selected.Microsoft.Exchange.Data.Directory.Management.Mailbox

Need to execute this to few hundred mailaccounts, how do I get them in one object, preferably without using foreach?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Just add them. But make sure the properties are the same, otherwise you'll get incomplete dumps with export-csv and format-* aso.
$Result = $CasMailbox + $Mailbox

Open in new window

rastoiWindows DTS expertCommented:
I believe that autor is asking for hashtable like this
"CasMailbox" = Get-CASMailbox $username | select abc
"Mailbox" =  Get-mailbox $username | select xyz

Open in new window

or probably more something like this:

"a"=(Get-CASMailbox $username).a
"b"=(Get-CASMailbox $username).b
"c"=(Get-CASMailbox $username).c
"x"=(Get-mailbox $username).x
"y"=(Get-mailbox $username).y
"z"=(Get-mailbox $username).z

Open in new window

or the same, but a bit more effective for processing time

$CasMailbox = Get-CASMailbox $username | select abc
$Mailbox =  Get-mailbox $username | select xyz     

Open in new window

janhoedtAuthor Commented:
Howto extend if multiple results?

$CasMailbox = Get-CASMailbox -resultsize 10
$Mailbox =  Get-mailbox -ResultSize 10

Then export to csv?

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

janhoedtAuthor Commented:
Note: the+ does not work.
Bot are of different type:
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
If they have a different type and different structure, you need to "normalize" them, i.e. create new objects with common properties. You cannot export mixed object types into CSV (that well), as I already stated. And it really doesn't make sense to do a full export of such objects, with their nested object information and real load of data.

So: What do you really want to achieve? What are the generated data to use for?
janhoedtAuthor Commented:
What I want to achieve:
*get certain properties of a list of users via Get-CASMailbox $username | select abc
*get other properties of the same list of users via Get-mailbox $username | select xyz

Combine them in an $Result and have this exported to a csv so the csv has the specific properties of all those users.
janhoedtAuthor Commented:
Let me give you an example:

$CasMailbox = Get-CASMailbox | select PrimarySMTPAddress, SamAccountname
$Mailbox =  Get-mailbox | select UserPrincipalName, prohibitsendquota

$ResultPSCustomObject = [PSCustomObject]@{
      SMTP = $CasMailbox.PrimarySmtpAddress
      SamAccountName = $CasMailbox.Identity
      UPN = $Mailbox.UserPrincipalName
      ProhibitSendQuota = $Mailbox.ProhibitSendQuota
$ResultPSCustomObject | Out-File C:\Result.csv

Everything works BUT the Result.csv does not contain the data which is in $ResultPSCustomObject
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Just using Out-File is a bad way to get file output. But it should work, if applied to a single object with simple types. And it doesn't generate a CSV format file.

Did you want to provide the same username for both cmdlets, and then mix/combine some of the properties for a single user, as shown in your question?
If you run the commands as shown later, you do not have any relation between CASMailbox and Mailbox properties - they can be from different users.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Let's see if that fits:
Get-CASMailbox |
% {
  $CAS = $_
  Get-Mailbox $CAS.Identity |
    Select @{n='SMTP'          ; e={$CAS.PrimarySmtpAddress}},
           @{n='SamAccountName'; e={$CAS.Identity}},
} | Export-CSV -NoType C:\Result.csv

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.