Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Powershell script

Posted on 2016-11-03
Medium Priority
Last Modified: 2016-11-23
Requirements of the script:

Input: Display name, alias or SMTP address of a mailbox
Output:  Can be selected by user which is one of the properties of the command "Get-Mailbox -Identity <username> |fl "

Example for Output selection by user: ExtensionCustomAttribute1
Question by:Exchange Admin
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
LVL 16

Expert Comment

by:Jason Crawford
ID: 41873503
I'm trying to understand what the requirements are and I'm having a hard time.  Do you need a GUI?  Should a list of all possible parameters for Get-Mailbox exist in something like a drop down menu?

Expert Comment

ID: 41873705

As a basic script you can do something like this:
$UserInputName = Read-Host "`t`tPlease enter the user displayname"
$AttributeInput = Read-Host "`t`tPlease enter the required attribute"
get-mailbox -identity $UserInputName | select $AttributeInput

Assisted Solution

Andy earned 1000 total points
ID: 41873828

I've updated with the other requirements for you.
This is all using the Exchange Shell, hence the import of the AD module
Import-Module ActiveDirectory

$UserDispName = Read-Host "`t`tPlease enter the user displayname"
$AttributeInput = Read-Host "`t`tPlease enter the required attribute"
get-mailbox -identity $UserInputName | select $AttributeInput

$userAlias = Read-Host "`t`tPlease enter the user Alias"
$UserADAlias = Get-ADUser -Filter {mailNickName -eq $userAlias} | %{$_.SamAccountName}
$AttributeInput = Read-Host "`t`tPlease enter the required attribute"
get-mailbox -identity $UserADAlias | select $AttributeInput

$usermail = Read-Host "`t`tPlease enter the user e-mail address"
$UserADName = Get-ADUser -Filter {Emailaddress -eq $usermail} | %{$_.SamAccountName}
$AttributeInput = Read-Host "`t`tPlease enter the required attribute"
get-mailbox -identity $UserADName | select $AttributeInput
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

ID: 41874948
I read like Jason did.  Needing a GUI?   Close as I can get.

# GUI version using Input Box.  This is for the alias.

[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
	$ClientUser = [Microsoft.VisualBasic.Interaction]::InputBox("Please enter users alias", "Enter")

	Get-Mailbox -Filter * -Identity $clientUser -ResultSize unlimited | select name, customattribute1 |Out-GridView

Open in new window


Author Comment

by:Exchange Admin
ID: 41875145
Hi Todd and Jason,

Thanks for your response. The requirement is a powershell script which should meet the below criteria:

      An excel file with a set of users (which could be either email address or alias or display name) is provided to script during its run time as input.
     Script should prompt user to select the output which could be any attributes of Get-mailbox command result. And the selection of user can be one or many
    If mailbox is not present, output should be "Not found"

Please check the attachment.

LVL 16

Expert Comment

by:Jason Crawford
ID: 41875362
Just to make sure I'm on the same page.  A .csv file containing one column populated with rows of users will be provided.  The script should import the list of users, then display a full list of all parameters for the Get-Mailbox cmdlet (I'm guessing a numbered list).  The user will enter the number mapped to the parameter they want included in the output.  For each user in the .csv file the script will run Get-Mailbox and gather the pre-selected parameters.  I assume you want the output to be a ,csv file on the desktop?  Is that all right?
LVL 71

Accepted Solution

Qlemo earned 1000 total points
ID: 41875505
We can use Excel - if really needed. But it is not recommended, as access is slow. Better, and probably what you really want, is the use of CSV files as Jason mentioned above. Though using a single column CSV is overkill; you usually leave out the column header and store as text file then.

Mixing CSV in/output with an interactive grid view for the attributes choice (even allowing for selecting multiple attributes):
$attr = '*'
$forcedAttr = 'DisplayName', 'UserPrincipalName', 'Database', 'WhenCreated'
Import-CSV C:\Temp\EE\Users.csv |
  % {
    $mb = Get-Mailbox $_.{Display Name} -Filter $attr
    if ($attr -eq '*')   # we need to show an attribute choice opportunity once
      $attr = $mb | Get-Member -Type Property | Select Name |
                    ? { $forcedAttr -notcontains $_.Name } | 
                    Out-GridView -OutputMode Multiple -Title "Select attributes to export"
      $attr = $forcedAttr + $attr
    $mb | select $attr  
  } |
  Export-CSV -NoType C:\Temp\EE\Results.csv

Open in new window

With some more effort we can display example values (of the first mailbox) when showing the grid, or exclude attributes which should never be exportable.

Author Comment

by:Exchange Admin
ID: 41876216
Hi Jason,

You are correct, but the output selection by user is not a number. User can enter the parameter required by them during run of script. And this parameter can be one or many.


Author Closing Comment

by:Exchange Admin
ID: 41899024
Solutions were provided

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

670 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