47,99 €
Microsoft Exchange Server 2013 is a complex messaging system. Windows PowerShell 3 can be used in conjunction with Exchange Server 2013 to automate and manage routine and complex tasks to save time, money, and eliminate errors.Microsoft Exchange Server 2013 PowerShell Cookbook: Second Edition offers more than 120 recipes and solutions to everyday problems and tasks encountered in the management and administration of Exchange Server. If you want to write scripts that help you create mailboxes, monitor server resources, and generate detailed reports, then this Cookbook is for you. This practical guide to Powershell and Exchange Server 2013 will help you automate and manage time-consuming and reoccurring tasks quickly and efficiently. Starting by going through key PowerShell concepts and the Exchange Management Shell, this book will get you automating tasks that used to take hours in no time.With practical recipes on the management of recipients and mailboxes as well as distribution groups and address lists, this book will save you countless hours on repetitive tasks. Diving deeper, you will then manage your mailbox database, client access, and your transport servers with simple but effective scripts.This book finishes with advanced recipes on Exchange Server problems such as server monitoring as well as maintaining high availability and security. If you want to control every aspect of Exchange Server 2013 and learn how to save time with PowerShell, then this cookbook is for you.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 676
Veröffentlichungsjahr: 2013
Copyright © 2013 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: July 2011
Second Edition: May 2013
Production Reference: 1100513
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-84968-942-7
www.packtpub.com
Cover Image by David Gimenez (<[email protected]>)
Authors
Jonas Andersson
Mike Pfeiffer
Reviewers
Marcelo Vighi Fernandes
Anderson Patricio
Acquisition Editor
Andrew Duckworth
Lead Technical Editor
Neeshma Ramakrishnan
Technical Editors
Dennis John
Dominic Pereira
Nitee Shetty
Project Coordinator
Arshad Sopariwala
Proofreaders
Maria Gould
Paul Hindle
Indexer
Hemangini Bari
Production Coordinator
Arvindkumar Gupta
Cover Work
Arvindkumar Gupta
Jonas Andersson is a devoted person who is constantly developing himself and his skills. He started in the IT business in 2004 and worked at first in a support center where he got his basic knowledge. In 2007 he started his career as a Microsoft infrastructure consultant and from 2008 onwards his focus has been on Microsoft Exchange.
Even though his focus is on Microsoft Exchange, his interests include migrations, backup, storage, and archiving. At the start of 2010, he was employed at a large outsourcing company as a messaging specialist, specializing in Microsoft Exchange. His work includes designing, implementing, and developing messaging solutions for enterprise customers.
His unique knowledge makes him a key figure in large and complex migration projects where he works with design and implementation. Examples of these projects include migrations from the IBM Domino mail platform to Microsoft Exchange 2007/2010 and Office 365, using Quest Software with full coexistence between the systems for mail flow, directory synchronization, and free busy lookups.
Apart from his daily job, he was active on TechNet forums, he also writes articles at his blog (http://www.testlabs.se/blog), and Twitter and other social media.
As a reward for the work in the community he was been awarded the Microsoft Community Contributor Award both 2011 and 2012.
Since this is my first book, it's been a great experience and a great honor to get the opportunity to write an update of the great book that Mike Pfeiffer initially wrote for Microsoft Exchange 2010.
I look forward to continuing these kinds of side-projects to my regular work.
There are a lot of people I would like to thank; firstly of course my family, which includes my parents and my fiancée for the love and energy they keep on giving me. Besides my family I want to thank Magnus Björk and Mike Pfeiffer for answering my e-mails when I needed to verify things. I also want to thank Anderson Patricio and Marcelo Vighi for doing great work with the technical review and giving me lots of great feedback.
I hope that you will enjoy the book and that its content will help you to develop your skills in the area.
Mike Pfeiffer has been in the IT field for 15 years, and has been working on Exchange for the majority of that time. He is a Microsoft Certified Master and a former Microsoft Exchange MVP. These days he works at Microsoft as a Premier Field Engineer where he helps customers deploy and maintain Microsoft Exchange and Lync Server solutions. You can find his writings online at mikepfeiffer.net, where he occasionally blogs about Exchange, Lync, and PowerShell-related topics.
Marcelo Vighi Fernandes has over 14 years of experience in the IT field, always focusing on Microsoft Exchange Server, Active Directory, and other Microsoft Infrastructure solutions. Currently he is working at SolarWinds Inc. as a Technical Sales Engineer in Brazil. Marcelo is a well-known writer for a very important Exchange resource website in Portuguese where he and others members of the community add content on a weekly basis. You can reach this site at www.andersonpatricio.org.
He also contributes to many activities within the Exchange and cloud computing communities, such as presentations, articles, tutorials, and he also has two blogs on Exchange Server and cloud computing.
Anderson Patricio is an Exchange Server MVP and a messaging consultant based in Toronto, Canada, designing and deploying solutions for clients located in North and South America. He has been working with Exchange since Version 5 and he had the opportunity to use PowerShell since its beta release (code name Monad at the time).
He contributes to the Microsoft communities in several ways. In English, his blog, www.andersonpatricio.ca, is updated regularly with Exchange, PowerShell, and Microsoft as its general content. In Portuguese, he has an Exchange resource site (www.andersonpatricio.org) and he is also a TechEd presenter in South America and also creator of a couple of Exchange trainings in the Brazilian Microsoft Virtual Academy (MVA).
You can also follow him on Twitter at http://twitter.com/apatricio.
He has also been the reviewer of several books such as Windows PowerShell in Action, Bruce Payette, Manning Publications; PowerShell in Practice, Richard Siddaway, Manning Publications; and Microsoft Exchange 2010 PowerShell Cookbook, Mike Pfeiffer, Packt Publishing.
You might want to visit www.PacktPub.com for support files and downloads related to your book.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
http://PacktLib.PacktPub.com
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's entire library of books.
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access.
Get notified! Find out when new books are published by following @PacktEnterprise on Twitter, or the Packt Enterprise Facebook page.
This book is full of immediately usable task-based recipes for managing and maintaining your Microsoft Exchange 2013 environment with Windows PowerShell 3.0 and the Exchange Management Shell. The focus of this book is to show you how to automate routine tasks and solve common problems. While the Exchange Management Shell literally provides hundreds of cmdlets, we will not cover every single one of them individually. Instead, we'll focus on the common, real world scenarios. You'll be able to leverage these recipes right away, allowing you to get the job done quickly, and the techniques that you'll learn will allow you to write your own amazing one-liners and scripts with ease.
Chapter 1, PowerShell Key Concepts, introduces several PowerShell core concepts such as command syntax and parameters, working with the pipeline, and flow control with loops and conditional logic. The topics covered in this chapter lay the foundation for the remaining code samples in each chapter.
Chapter 2, Exchange Management Shell Command Tasks, covers day-to-day tasks and general techniques for managing Exchange from the command line. The topics include configuring manual remote shell connections, exporting reports to external files, sending e-mail messages from scripts, and scheduling scripts to run with the Task Scheduler.
Chapter 3, Managing Recipients, demonstrates some of the most common recipient-related management tasks, such as creating mailboxes, distribution groups, and contacts. You'll also learn how to manage server-side inbox rules, out of office settings, and import user photos into the Active Directory.
Chapter 4, Managing Mailboxes, shows you how to perform various mailbox management tasks that include moving mailboxes, importing and exporting mailbox data, and the detection and repair of corrupt mailboxes. In addition, you'll learn how to delete and restore items from a mailbox, manage the new public folders, and generate some basic reports.
Chapter 5, Distribution Groups and Address Lists, takes you deeper into distribution group management. The topics include distribution group reporting, distribution group naming policies, and allowing end users to manage distribution group membership. You'll also learn how to create address lists and hierarchal address books.
Chapter 6, Mailbox Database Management, shows how to set database settings and limits. Report generation for mailbox database size, average mailbox size per database, and backup status is also covered in this chapter.
Chapter 7, Managing Client Access, covers the managing of ActiveSync, OWA, POP, and IMAP. It also covers the configuration of these components in Exchange 2013. We'll also take a look at controlling connections from various clients, including ActiveSync devices.
Chapter 8, Managing Transport Service, explains the various methods used to control mail flow within your Exchange organization. You'll learn how to create, send, and receive connectors, allow application servers to relay mail, and manage transport queues.
Chapter 9, High Availability, covers the implementation and management tasks related to Database Availability Groups (DAGs). Topics include creating DAGs, adding mailbox database copies, and performing maintenance on DAG members. It also covers the new feature called automatic reseed.
Chapter 10, Exchange Security, introduces the new Role Based Access Control (RBAC) permissions model. You'll learn how to create custom RBAC roles for administrators and end users, and also how to manage mailbox permissions and implement SSL certificates.
Chapter 11, Compliance and Audit Logging, covers the new compliance and auditing features included in Exchange 2013. Topics such as archiving mailboxes and discovery search are covered here, as well as administrator and mailbox audit logging.
Chapter 12, Server Monitoring and Troubleshooting, shows you how to monitor and report on service availability and resource utilization using PowerShell core cmdlets and WMI. Event log monitoring and Exchange server role troubleshooting tactics are also covered.
Chapter 13, Scripting with the Exchange Web Services Managed API, introduces advanced scripting topics that leverage Exchange Web Services. In this chapter, you'll learn how to write scripts and functions that go beyond the capabilities of the Exchange Management Shell cmdlets.
Appendix A, Common Shell Information, is a reference for the variables, scripts, and the filtering functions. These references will help you when writing scripts or running interactive.
Appendix B, Query Syntaxes, is a reference for the Advanced Query Syntax (AQS). Here are lots of different examples that can be used in the real world.
To complete the recipes in this book, you'll need the following:
The code samples in this book should be run in a lab environment and fully tested before deployed into production. If you don't have a lab environment setup, you can download the software from http://technet.microsoft.com/en-us/exchange/. Then build the servers on your preferred virtualization engine.
This book is for messaging professionals who want to learn how to build real-world scripts with Windows PowerShell 3.0 and the Exchange Management Shell. If you are a network or systems administrator responsible for managing and maintaining the on-premise version of Exchange Server 2013, then this book is for you.
The recipes in this Cookbook touch on each of the core Exchange 2013 server roles, and require a working knowledge of the supporting technologies, such as Windows Server 2008, 2008 R2 or 2012, Active Directory, and DNS.
All of the topics in this book are focused on the on-premises version of Exchange 2013, and we will not cover Microsoft's hosted version of Exchange Online through Office 365. However, the concepts you'll learn in this book will allow you to hit the ground running with that platform since it will give you an understanding of PowerShell's command syntax and object-based nature.
In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "We can read the content of an external file into the shell using the Get-Content cmdlet"
Commands and blocks of code are set as follows:
Commands like this can be invoked interactively in the shell, or from within a script or function.
Most of the commands you'll be working with will be very long. In order for them to fit into the pages of this book, we'll need to use line continuation. For example, here is a command that creates a mailbox:
Notice that the last character on each line is the backtick (`) symbol, also referred to as the grave accent. This is PowerShell's line continuation character. You can run this command as is, but make sure there aren't any trailing spaces at the end of each line. You can also remove the backticks and carriage returns and run the command on one line. Just ensure the spaces between the parameters and arguments are maintained.
You'll also see long pipeline commands formatted like the following example:
PowerShell uses the pipe character (|) to send object output from a command down the pipeline so it can be used as input by another command. The pipe character does not need to be escaped. You can enter the previous command as is, or you can format the command so that everything is on one line.
Any command-line input or output that must be done interactively at the shell console is written as follows:
New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "Click on the Exchange Management Shell shortcut".
Warnings or important notes appear in a box like this.
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.
To send us general feedback, simply send an e-mail to <[email protected]>, and mention the book title via the subject of your message.
If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or e-mail <[email protected]>.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the erratasubmissionform link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.
Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <[email protected]> with a link to the suspected pirated material.
We appreciate your help in protecting our authors, and our ability to bring you valuable content.
You can contact us at <[email protected]> if you are having a problem with any aspect of the book, and we will do our best to address it.
The Exchange Management Shell includes over 750 cmdlets (pronounced command-lets), each with a set of multiple parameters. For instance, the New-Mailbox cmdlet accepts more than 60 parameters, and the Set-Mailbox cmdlet has over 160 available parameters. It's safe to say that even the most experienced PowerShell expert would be at a disadvantage without a good help system. In this recipe, we'll take a look at how to get help in the Exchange Management Shell.
To get help information for a cmdlet, typeGet-Help, followed by the cmdlet name. For example, to get help information about the Get-Mailbox cmdlet, run the following command:
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.
When runningGet-Help for a cmdlet, a synopsis and description for the cmdlet will be displayed in the shell. The Get-Help cmdlet is one of the best discovery tools to use in PowerShell. You can use it when you're not quite sure how a cmdlet works or what parameters it provides.
You can use the following switch parameters to get specific information using the Get-Help cmdlet:
Some parameters accept simple strings as input, while others require an actual object. When creating a mailbox using the New-Mailbox cmdlet, you'll need to provide a secure string object for the -Password parameter. You can determine the data type required for a parameter using Get-Help:
You can see from the command output that we get several pieces of key information about the -Password parameter. In addition to the required data type of <SecureString>, we can see that this is a named parameter. It is required when running the New-Mailbox cmdlet and it does not accept wildcard characters. You can use Get-Help when examining the parameters for any cmdlet to determine whether or not they support these settings.
You could run Get-HelpNew-MailboxExamples to determine the syntax required to create a secure string password object and how to use it to create a mailbox. This is also covered in detail in the recipe entitled Adding, modifying, and removing mailboxes in Chapter 3, Managing Recipients.
There will be times when you'll need to search for a cmdlet without knowing its full name. In this case, there are a couple of commands you can use to find the cmdlets you are looking for.
To find all cmdlets that contain the word "mailbox", you can use a wildcard, as shown in the following command:
You can use the -Verb parameter to find all cmdlets starting with a particular verb:
To search for commands that use a particular noun, specify the name with the -Noun parameter:
The Get-Command cmdlet is a built-in PowerShell core cmdlet, and it will return commands from both Windows PowerShell as well as the Exchange Management Shell. The Exchange Management Shell also adds a special function called Get-Ex command that will return only Exchange-specific commands.
In addition to getting cmdlet help for cmdlets, you can use GetHelp to view supplementary help files that explain general PowerShell concepts that focus primarily on scripting. To display the help file for a particular concept, type Get-Helpabout_ followed by the concept name. For example, to view the help for the core PowerShell commands, type the following:
You can view the entire list of conceptual help files using the following command:
Don't worry about trying to memorize all the Exchange or PowerShell cmdlet names. As long as you can remember GetCommand and Get-Help, you can search for commands and figure out the syntax to do just about anything.
One of the things that can be confusing at first is the distinction between cmdlets and functions. When you launch the Exchange Management Shell, a remote PowerShell session is initiated to an Exchange server and specific commands, called proxy functions, are imported into your shell session. These proxy functions are essentially just blocks of code that have a name, such as GetMailbox, and that correspond to the compiled cmdlets installed on the server. This is true even if you have a single server and when you are running the shell locally on a server.
When you run the Get-Mailbox function from the shell, data is passed between your machine and the Exchange server through a remote PowerShell session. The Get-Mailbox cmdlet is actually executing on the remote Exchange server, and the results are being passed back to your machine. One of the benefits of this is that it allows you to run the cmdlets remotely regardless of whether your servers are on-premise or in the cloud. Additionally, this core change in the tool set is what allows Exchange 2010 and 2013 to implement its new security model by allowing and restricting which cmdlets administrators and end users can actually use through the shell or the web-based control panel.
We'll get into the details of all this throughout the remaining chapters in the book. The bottom line is that, for now, you need to understand that, when you are working with the help system, the Exchange 2013 cmdlets will show up as functions and not as cmdlets.
Consider the following command and the output:
Here we are running GetCommand against a PowerShell v3 core cmdlet. Notice that the CmdletType shows that this is a Cmdlet.
Now try the same thing for the Get-Mailbox cmdlet:
As you can see, the CommandType for the Get-Mailbox cmdlet shows that it is actually a Function. So, there are a couple of key points to take away from this. First, throughout the course of this book, we will refer to the Exchange 2013 cmdlets as cmdlets, even though they will show up as functions when running GetCommand. Second, keep in mind that you can run Get-Help against any function name, such as Get-Mailbox, and you'll still get the help file for that cmdlet. But if you are unsure of the exact name of a cmdlet, use Get-Command to perform a wildcard search as an aid in the discovery process. Once you've determined the name of the cmdlet you are looking for, you can run GetHelp against that cmdlet for complete details on how to use it.
Try using the help system before going to the Internet to find answers. You'll find that the answers to most of your questions are already documented within the built-in cmdlet help.
The single most important concept in PowerShell is the use of its flexible, object-based pipeline. You may have used pipelines in Unix-based shells, or when working with the cmd.exe command prompt. The concept of pipelines is similar to that of sending the output from one command to another. But, instead of passing plain text, PowerShell works with objects, and we can accomplish some very complex tasks in just a single line of code. In this recipe, you'll learn how to use pipelines to string together multiple commands and build powerful one-liners.
The following pipeline command would set the office location for every mailbox in the DB1 database:
In a pipeline, you separate a series of commands using the pipe (|) character. In the previous example, the Get-Mailbox cmdlet returns a collection of mailbox objects. Each mailbox object contains several properties that contain information such as the name of the mailbox, the location of the associated user account in Active Directory, and more. The Set-Mailbox cmdlet is designed to accept input from the Get-Mailbox cmdlet in a pipeline, and with one simple command we can pass along an entire collection of mailboxes that can be modified in one operation.
You can also pipe output to filtering commands, such as the Where-Object cmdlet. In this example, the command retrieves only the mailboxes with a MaxSendSize equal to 10 megabytes:
The code that the Where-Object cmdlet uses to perform the filtering is enclosed in curly braces ({}). This is called a script block, and the code within this script block is evaluated for each object that comes across the pipeline. If the result of the expression is evaluated as true, the object is returned; otherwise, it is ignored. In this example, we access the MaxSendSize property of each mailbox using the $_ object, which is an automatic variable that refers to the current object in the pipeline. We use the equals (-eq) comparison operator to check that the MaxSendSize property of each mailbox is equal to 10 megabytes. If so, only those mailboxes are returned by the command.
Comparison operators allow you to compare results and find values that match a pattern. For a complete list of comparison operators, run Get-Helpabout_Comparison_Operators.
When running this command, which can also be referred to as a one-liner, each mailbox object is processed one at a time using stream processing. This means that as soon as a match is found, the mailbox information is displayed on the screen. Without this behavior, you would have to wait for every mailbox to be found before seeing any results. This may not matter if you are working in a very small environment, but without this functionality in a large organization with tens of thousands of mailboxes, you would have to wait a long time for the entire result set to be collected and returned.
One other interesting thing to note about the comparison being done inside our Where-Object filter is the use of the mb multiplier suffix. PowerShell natively supports these multipliers and they make it a lot easier for us to work with large numbers. In this example, we've used 10mb, which is the equivalent of entering the value in bytes because behind the scenes, PowerShell is doing the math for us by replacing this value with 1024*1024*10. PowerShell provides support for the following multipliers: kb, mb, gb, tb, and pb.
You can use advanced pipelining techniques to send objects across the pipeline to other cmdlets that do not support direct pipeline input. For example, the following one-liner adds a list of users to a group:
This pipeline command starts off with a simple filter that returns only the users that have their title set to "Exchange Admin". The output from that command is then piped to the ForEach-Object cmdlet that processes each object in the collection. Similar to the Where-Object cmdlet, the ForEach-Object cmdlet processes each item from the pipeline using a script block. Instead of filtering, this time we are running a command for each user object returned in the collection and adding them to the "Organization Management" role group.
Using aliases in pipelines can be helpful because it reduces the number of characters you need to type. Take a look at the following command where the previous command is modified to use aliases:
Notice the use of the question mark (?) and the percent sign (%) characters. The ? character is an alias for the Where-Object cmdlet, and the % character is an alias for the ForEach-Object
