Getting users' mailbox

Colleagues, what I need is to get the list of mailboxes based on domain\logon name only. Shared mailboxes contain mostly the users that are located on other domains hence I have to use command for getting the data for linked accounts.

First of all I export the list of names to a csv file.

Get-Mailbox -Identity GROUP  | Get-MailboxPermission | where {$_.user.tostring() -ne "NT AUTHORITY\SELF" -and $_.IsInherited -eq $false} | Export-csv -path C:\Users\mplumsky\Desktop\GROUP_list.csv NoTypeInformation

When I try the following command on a single user it works well:

get-mailbox -Resultsize Unlimited | ? { $_.LinkedMasterAccount -eq 'domain\user' }  | select Name, Primarysmtpaddress > mailbox.txt

Of course I have a long list of users and it would be great when powershell could do the job in the background. That's why I've come up with the idea:

$users = import-csv C:\Users\mplumsky\Desktop\GROUP_list.csv

 

ForEach ($item in $users)

{

$username = $item.User

get-mailbox -Resultsize Unlimited | ? { $_.LinkedMasterAccount -eq '$username' }  | select Name, Primarysmtpaddress > mailbox_list.csv

}

As a result I get a blank csv or txt file. Any ideas what could be wrong?

September 11th, 2015 7:34am

I've not tested it, but as a first try: I think ">" overwrites existing files. Use Cmdlet "Add-Content" instead. Of course you have to ensure that the file exists before with "Test-Path" and "New-Item" if needed. Another way would be to store everything in an array and use "Export-CSV" for output.

Furthermore, I think it doesn't make sense to run Get-Mailbox in every turn of the loop, if there is no depended parameter. Do that before the foreach-loop and only call the variable inside.

Free Windows Admin Tool Kit Click here and download it now
September 11th, 2015 7:56am

import-csv C:\Users\mplumsky\Desktop\GROUP_list.csv |
    ForEach-Object{
        $username=$_.User
        get-mailbox -Resultsize Unlimited | ? { $_.LinkedMasterAccount -eq $username }  
    } |
    select Name, Primarysmtpaddress |
    Export-Csv mailbox_list.csv		
September 11th, 2015 7:57am

I was sure the solution is very close. Anyway, thank you for the advice, it works fine now!
Free Windows Admin Tool Kit Click here and download it now
September 14th, 2015 5:53am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics