?
Solved

Help with a conditional statement when generating a report in Powershell

Posted on 2012-09-07
3
Medium Priority
?
386 Views
Last Modified: 2012-09-10
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
0
Comment
Question by:ndalmolin_13
3 Comments
 
LVL 16

Expert Comment

by:Dale Harris
ID: 38378825
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
 
LVL 37

Accepted Solution

by:
Jamie McKillop earned 2000 total points
ID: 38380760
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
 
LVL 1

Author Closing Comment

by:ndalmolin_13
ID: 38384714
This did the trick.  Thanks for your help.  I appreciate the post.

Nick
0

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Screencast - Getting to Know the Pipeline

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question