Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Microsoft Exchange Server 2013 PowerShell Cookbook: Second Edition

You're reading from   Microsoft Exchange Server 2013 PowerShell Cookbook: Second Edition Benefit from over 120 recipes that tackle the everyday issues that arise with Microsoft Exchange Server. Using PowerShell you'll learn to add scripts that provide new functions and efficiencies. Only basic knowledge required.

Arrow left icon
Product type Paperback
Published in May 2013
Publisher Packt
ISBN-13 9781849689427
Length 504 pages
Edition 2nd Edition
Concepts
Arrow right icon
Toc

Table of Contents (23) Chapters Close

Microsoft Exchange Server 2013 PowerShell Cookbook
Credits
About the Authors
Acknowledgement
About the Reviewers
www.PacktPub.com
Preface
1. PowerShell Key Concepts FREE CHAPTER 2. Exchange Management Shell Common Tasks 3. Managing Recipients 4. Managing Mailboxes 5. Distribution Groups and Address Lists 6. Mailbox Database Management 7. Managing Client Access 8. Managing Transport Service 9. High Availability 10. Exchange Security 11. Compliance and Audit Logging 12. Server Monitoring and Troubleshooting 13. Scripting with the Exchange Web Services Managed API Common Shell Information Query Syntaxes Index

Creating and running scripts


You can accomplish many tasks by executing individual cmdlets or running multiple commands in a pipeline, but there may be times where you want to create a script that performs a series of operations or that loads a library of functions and predefined variables and aliases into the shell. In this recipe, we'll take a look at how you can create and run scripts in the shell.

How to do it...

  1. Let's start off by creating a basic script that automates a multi-step process. We'll start up a text editor, such as Notepad, and enter the following code:

    param(
      $name,
      $maxsendsize,
      $maxreceivesize,
      $city,
      $state,
      $title,
      $department
    )
    
    Set-Mailbox -Identity $name `
    -MaxSendSize $maxsendsize `
    -MaxReceiveSize $maxreceivesize
    
    Set-User -Identity $name `
    -City $city `
    -StateOrProvince $state `
    -Title $title `
    -Department $department
    
    Add-DistributionGroupMember -Identity DL_Sales `
    -Member $name
    
  2. Next, we'll save the file on the C:\ drive using the name Update-SalesMailbox.ps1.

  3. We can then run this script and provide input using parameters that have been declared using the param keyword:

    C:\Update-SalesMailbox.ps1 -name testuser `
    -maxsendsize 25mb `
    -maxreceivesize 25mb `
    -city Phoenix `
    -state AZ `
    -title Manager `
    -department Sales
    
  4. When the script runs, the specified mailbox will be updated with the settings provided.

How it works...

The concept of a PowerShell script is similar to batch files used with cmd.exe, but, instead of a .bat extension, PowerShell scripts use a .ps1 extension. To create a script, you can use a basic text editor such as Notepad or you can use the Windows PowerShell Integrated Scripting Environment (ISE).

Just like a function, our script accepts a number of parameters. As you can see from the code, we're using this script to automate a task that modifies several properties of a mailbox and add it to a distribution group. Since this requires the use of three separate cmdlets, it makes sense to use a script to automate this task.

If we wanted to run this script against a collection of mailboxes, we could use a foreach loop, as shown:

foreach($i in Get-Mailbox -OrganizationalUnit contoso.com/sales) {
  c:\Update-SalesMailbox.ps1 -name $i.name `
  -maxsendsize 100mb `
  -maxreceivesize 100mb `
  -city Phoenix `
  -state AZ `
  -title 'Sales Rep' `
  -department Sales
}

Here you can see we're simply looping through each mailbox in the Sales OU and running the script against each one. You can modify the script to run any number of cmdlets. Also, keep in mind that although we're using parameters with our script, they are not required.

Tip

Comments can be added to a script using the pound (#) character.

Think of a script as the body of a function. We can use the same three phases of execution such as Begin, Process, and End blocks, and add as many parameters as required. You may find it easier to create all of your code in the form of functions as opposed to scripts, although one of the nice things about scripts is that they can easily be scheduled to run as a task using the task scheduler.

There's more…

Here's something that seems a little strange at first and might take a little getting used to. When you want to execute a PowerShell script in the current directory, you need to prefix the command with a dot slash (.\) as shown:

[PS] C:\>.\New-SalesMailbox.ps1

We can use either the forward or backslash characters; it doesn't matter which. This is just a security mechanism which prevents you from executing a script in an unknown location. As you might expect, you can still run a script using its full path, just as you would with an executable or batch file.

Another thing to be aware of is the concept of dot-sourcing a script. This gives us the ability to execute commands in a script and also load any custom aliases, functions, or variables that are present within the script into your PowerShell session. To dot-source a script, use the dot operator: type a period, followed by a space, and then the path to the script as shown next:

[PS] C:\>. .\functions.ps1

This technique can be used to load functions, modules, variables, and aliases from within other scripts.

Execution policy

Windows PowerShell implements script security to keep unwanted scripts from running in your environment. You have the option of signing your scripts with a digital signature to ensure that scripts that are run are from a trusted source. In order to implement this functionality, PowerShell provides four script execution modes that can be enabled:

  • Restricted: In this mode the scripts will not run even if they are digitally signed

  • AllSigned: In this mode all scripts must be digitally signed

  • RemoteSigned: In this mode you can run local scripts, but scripts downloaded from the Internet will not run

  • Unrestricted: In this mode all scripts will run whether they are signed or not, or have been downloaded from an Internet site

The default execution policy on a machine is Restricted. When you install Exchange 2013 on a server, or the Exchange Management Tools on a workstation, the execution policy is automatically set to RemoteSigned. This is required by Exchange in order to implement the remote shell functionality.

It is possible to manage Exchange 2013 through PowerShell remoting on a workstation or server without Exchange Tools installed. In this case, you'll need to make sure your script execution policy is set to either RemoteSigned or Unrestricted. To set the execution policy, use the following command:

Set-ExecutionPolicy RemoteSigned

Make sure you do not change the execution policy to AllSigned on machines where you'll be using the Exchange cmdlets. This will interfere with importing the commands through a remote PowerShell connection which is required for the Exchange Management Shell cmdlets to run properly.

You can reference the help system on this topic by running Get-Helpabout_Execution_Policies.

See also

  • The Setting up a profile recipe

You have been reading a chapter from
Microsoft Exchange Server 2013 PowerShell Cookbook: Second Edition - Second Edition
Published in: May 2013
Publisher: Packt
ISBN-13: 9781849689427
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image