I am trying to get some user fields in an export of get-mobiledevice

I have a script

Get-User -ResultSize Unlimited | Where { $_.RecipientType -eq 'UserMailbox' } | ForEach { $Users = @{} } { $Users[$_.SamAccountName] = $_ }

Get-Mailbox -ResultSize Unlimited |
  ForEach {
    New-Object psobject |
    Add-Member -PassThru NoteProperty Username $_.Alias |
    Add-Member -PassThru NoteProperty Displayname $Users[$_.SamAccountName].Displayname |
    Add-Member -PassThru NoteProperty "Email Address" $_.primarysmtpaddress |
    Add-Member -PassThru NoteProperty "When mailbox was created in O365" $_.whenmailboxcreated |
    Add-Member -PassThru NoteProperty "Removed Date" $_.LitigationHoldDate |
    Add-Member -PassThru NoteProperty Site $Users[$_.SamAccountName].City |
    Add-Member -PassThru NoteProperty "Mailbox Type" $_.RecipientTypeDetails |
    Add-Member -PassThru NoteProperty "Account Status" $Users[$_.SamAccountName].userAccountControl |
    Add-Member -PassThru NoteProperty "Company" $Users[$_.SamAccountName].Company 
    }  |export-csv C:\mailboxes-Feb-15-2014.csv -notype

which works for get-mailbox but gives me a null error for get-mobiledevice

This is for O365

Thanks

4 Spice ups

Assuming this is Exchange 2013.

What user fields are you trying to get? I would first run the command against a single device with the “| fl” at the end to show what all the attributes you can get then adjust the script.

You could also see if the command accepts the “Properties *” to include all the properties although looking at the technet I don’t think it will.

-Jay

Ya the properties I seek are only available as output from get-user, City, Company and UserPrincipalName, than I Need the Properties from get-mobiledevice DeviceModel, FriendlyName and FirstSyncTime

Thus the need for PSObject and Add-member array

Add-Member is SOOO PowerShell V1. In PS2, you would be doing

New-Object PSObject -Property @{
    'item' = (code or variable; parens only if needed for complex expression)
    'nextitem' = $anotherVariable
}

and from PS 3, you could be using [pscutomobject]@{} or [ordered]@{ditto}

Lots less typing and also easier to read in 6 months or a year.

2 Spice ups

At the risk of ever contradicting Martin, there is a small issue with the above code, which is rectified as follows:

New-Object PSObject -Property @{
    'item' = ("code or variable- parens only if needed for complex expression");
    'nextitem' = $anotherVariable
}

At the end of the 2nd line - there needs to be a ‘;’. And to save even more space:

New-Object PSObject -Property @{
    item = "code or variable- parens only if needed for complex expression";
    nextitem = $anotherVariable
}

In this second example, item and nextitem are not enclosed in string delimiters.

I know Martin knows this - but in case others are watching: I like to avoid wasted characters, even if they work just fine.

Lol, what did I say?!

I appreciate the lesson on using item and nextitem vs add-member

but can you please address the issue where it will work for get-mailbox but not for get-mobiledevice

or another what to get an export for get-mobiledevice and add the field from get-user such as userprincipalname, City and Company basically I need my export to have these fields

Username, UserDisplayName , DeviceModel, FriendlyName, FirstSyncTime, City, Company

tfl: I know that the definition of a hash table specifies a ‘;’ between each element. But the reality is that the ‘;’ is optional when building a hash table with each entry on a separate line.

nb. I use the quote marks for the property name just for consistency, since they are required if the property name includes white space. Sorry for the confusion.

Hi Hudzon, did you ever get this answered? I wish I could help more but I don’t have access to Exchange anymore!

No still no answer, I can give some more info if it will help get some ideas flowing.

Get-Mobiledevice returns users and the fields DeviceModel, FriendlyName and FirstSyncTime

so if I could use a foreach for each result and add the fields from get-user such as Company, City