• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 17
  • Last Modified:

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

Hi,

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?

J.
0
janhoedt
Asked:
janhoedt
  • 4
  • 4
1 Solution
 
QlemoBatchelor, 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

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

Open in new window


or probably more something like this:

$result=@{
"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     
$result=@{
"a"=$CASMailbox.a
"b"=$CASMailbox.b
"c"=$CASMailbox.c
"x"=$mailbox.x
"y"=$mailbox.y
"z"=$mailbox.z
}

Open in new window

1
 
janhoedtAuthor Commented:
Thanks.
Howto extend if multiple results?

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

Then export to csv?

J.
0
Worried about phishing attacks?

90% of attacks start with a phish. It’s critical that IT admins and MSSPs have the right security in place to protect their end users from these phishing attacks. Check out our latest feature brief for tips and tricks to keep your employees off a hackers line!

 
janhoedtAuthor Commented:
Note: the+ does not work.
Bot are of different type:
Microsoft.Exchange.Data.Directory.Management.Mailbox
Microsoft.Exchange.Data.Directory.Management.CASMailbox
0
 
QlemoBatchelor, 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?
1
 
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.
0
 
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
0
 
QlemoBatchelor, 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.
0
 
QlemoBatchelor, 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}},
           UserPrincipalName,
           ProhibitSendQuota
} | Export-CSV -NoType C:\Result.csv

Open in new window

1
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Worried about phishing attacks?

90% of attacks start with a phish. It’s critical that IT admins and MSSPs have the right security in place to protect their end users from these phishing attacks. Check out our latest feature brief for tips and tricks to keep your employees off a hackers line!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now