« Previous 1 2
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.
« Previous 1 2
Buy this article as PDF
(incl. VAT)