Powershell script

Posted on 2016-11-03
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 15

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 250 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
IoT Devices - Fast, Cheap or Secure…Pick Two

The IoT market is growing at a rapid pace and manufacturers are under pressure to quickly provide new products. Can you be sure that your devices do what they're supposed to do, while still being secure?

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 15

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 70

Accepted Solution

Qlemo earned 250 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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
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…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

623 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