Working with the Exchange Management Shell

Strong Shell

Flexible and Reusable: Scripts

As powerful as the PowerShell commands are when creating or modifying mailbox permissions, they have one major drawback: Multiple use requires multiple input. One solution might be a script provided with parameters, and data containers could be text files, Excel spreadsheets, or databases.

In the example, a simple CSV file contains the values to be set in tabular form. The first line defines the keys separated by commas. On the basis of this first line, values are extracted dynamically from the CSV file, as in this example file:

identity,user,right,inheritance
"Tom Meier",jerry@mydom.com,FullAccess,All
Conferenceroom,tom,ReadItems,All
[...]

The Import CSV cmdlet will then convert this file into a list of key-value pairs. You can now test for the path to your data source and, if successful, load all the data into the variable $ValuesField. If the file is not found, the application backs out with an exit code:

If (test-Path (PathtoCsvFile)){$ValuesField = import-csv PathtoCSVFile; }
Else {write-Host "Unfortunately the file was not found!" -foregroundColor "red"; Exit 1;}

Then, you can evaluate the exit code with the use of the $LastExitCode variable. Now you need a few test routines that test the validity of the data stored in the file:

Function validateUser ($ExObj){
  If (Get-user -Identity $ExObj -EA SilentlyContinue) {
    Return $TRUE;}
  Else {Return $FALSE}}

Listing 1 then starts processing each line in the CSV file.

Listing 1

Processing the CSV File

Foreach ($line in $valuesfield){
$PosIdentity = $line.identity;
$PosUser = $line.user;
$PosRight = $line.right;
$PosInheritance=$line.inheritance;
If (-NOT(validate $PosIdentity){"$PosIdentity was not found!";Exit 2}
ElseIf {(-NOT(validate $PosUser){"$PosUser was not found!";Exit 3}
Else{
$ErrorActionPreference="stop";
#Dynamic change begins
trap {"Error in the allocation. `n let's try the other changes";continue}
Add-MailboxPermission -Identity $PosIdentity -User $PosUser -AccessRights $Posright -Inheritance $PosInheritance;
}

Error handling is implemented in the scope of the loop. If an error occurs when processing because of missing permissions, output is generated and the next line is evaluated. To make this script more dynamic, I recommend transmitting the CSV file by means of a parameter.

Conclusions

PowerShell is increasingly the method of choice for managing Exchange Server. The scripting language demonstrates its strengths as a flexible tool, particularly for mass changes. You will find scripts based on it for repetitions and variable actions. Once you have familiarized yourself with the necessary cmdlets, and created a number of scripts for frequent use cases, Exchange management should be easy.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy ADMIN Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus
Subscribe to our ADMIN Newsletters
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs



Support Our Work

ADMIN content is made possible with support from readers like you. Please consider contributing when you've found an article to be beneficial.

Learn More”>
	</a>

<hr>		    
			</div>
		    		</div>

		<div class=