Help with a conditional statement when generating a report in Powershell

Hello Powershell Experts –
I feel like I’m always asking you the dumbest questions, but I really am trying to learn this stuff ¿
We are migrating our on-premise Exchange to Office 365.  My boss wants a report of all of the users in our Active Directory along with their mailbox size.  If the user doesn’t have a mailbox, then “No Mailbox” would be returned instead of a mailbox size.  I would like the report to look like the following:
LastName            FirstName      Title            MailboxSize (MB)      Department
Jones            Bob      Accountant      56                            sales            
Smith            Sally      Sales Person      578                            sales            
Franks            Eugene      Sales Person      608                            sales            
Salazar            Eve      Intern            No mailbox          sales

I’m almost there.  When I run my code, my report is generated for users that have a mailbox.  However, if a user does not have a mailbox, they are left off the report.  My code is as follows:

$Users = get-user | Select-object lastname, firstname, title, department, samaccountname, recipienttype
$Report = Foreach ($user in $Users)
{
    If ($user.recipientType -eq "UserMailbox")
    {
        Get-MailboxStatistics $user.samaccountname | Select-Object `
            @{n="LastName";e={$user.lastname}}, `
            @{n="firstName";e={$user.firstname}}, `
            @{n="Job Title";e={$user.title}}, `
            @{n="Mailbox Size (MB)";e={$_.totalitemsize.value.ToMB()}}, `
            @{n="Department";e={$user.Department}}
          }
     If ($user.recipientType -ne "UserMailbox")
    {
            @{n="LastName";e={$user.lastname}}, `
            @{n="firstName";e={$user.firstname}}, `
            @{n="Job Title";e={$user.title}}, `
            @{n="Mailbox Size (MB)";e={"No Mailbox"}}, `
            @{n="Department";e={$user.Department}}
    }
}
$Report | Sort-object "Department" | Format-Table | Out-file "C:\MasterMigReport.txt"

Any and all help with this would be greatly appreciated.

Regards,
Nick
LVL 1
ndalmolin_13Asked:
Who is Participating?
 
Jamie McKillopIT ManagerCommented:
Hello,

Try this:

$Users = get-user -OrganizationalUnit 'CN=Users,DC=amr,DC=SMTF,DC=DS' | Select-object lastname, firstname, title, department, samaccountname, recipienttype
$Report = Foreach ($user in $Users)
{
    If ($user.recipientType -eq "UserMailbox")
    {
        Get-MailboxStatistics $user.samaccountname | Select-Object `
            @{n="LastName";e={$user.lastname}}, `
            @{n="firstName";e={$user.firstname}}, `
            @{n="Job Title";e={$user.title}}, `
            @{n="Mailbox Size (MB)";e={$_.totalitemsize.value.ToMB()}}, `
            @{n="Department";e={$user.Department}}
          }
     If ($user.recipientType -ne "UserMailbox")
    {
		Get-User $user.SamAccountName | Select-Object `
			@{n="LastName";e={$user.lastname}}, `
            @{n="firstName";e={$user.firstname}}, `
            @{n="Job Title";e={$user.title}}, `
            @{n="Mailbox Size (MB)";e={"No Mailbox"}}, `
            @{n="Department";e={$user.Department}}
    }
}
$Report | Sort-object "Department" | Format-Table | Out-file "C:\MasterMigReport.txt"

Open in new window


JJ
0
 
Dale HarrisProfessional Services EngineerCommented:
Can you confirm whether or not all users are being put into the list and just not getting into the report at the end?

If they are all in there, all we should need to is look at the possible values of .recipienttype.

I'm of the opinion you would only find "UserMailbox", so that means everyone else should be put in the "no mailbox category".  You could do this by changing your second if statement into an else.  See below.

else
    {
            @{n="LastName";e={$user.lastname}}, `
            @{n="firstName";e={$user.firstname}}, `
            @{n="Job Title";e={$user.title}}, `
            @{n="Mailbox Size (MB)";e={"No Mailbox"}}, `
            @{n="Department";e={$user.Department}}
    }

If that doesn't work, then I would return just a list of all the values of recipienttype to see what the actual value are (test by getting a user that doesn't have a mailbox and see what kind of tests you can return.)
0
 
ndalmolin_13Author Commented:
This did the trick.  Thanks for your help.  I appreciate the post.

Nick
0
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.

All Courses

From novice to tech pro — start learning today.