Book Image

Microsoft Exchange Server 2016 PowerShell Cookbook - Fourth Edition

By : Jonas Andersson, Nuno Mota, Mike Pfeiffer
Book Image

Microsoft Exchange Server 2016 PowerShell Cookbook - Fourth Edition

By: Jonas Andersson, Nuno Mota, Mike Pfeiffer

Overview of this book

We start with a set of recipes on core PowerShell concepts. This will provide you with a foundation for the examples in the book. Next, you'll see how to implement some of the common exchange management shell tasks, so you can effectively write scripts with this latest release. You will then learn to manage Exchange recipients, automate recipient-related tasks in your environment, manage mailboxes, and understand distribution group management within the Exchange Management Shell. Moving on, we'll work through several scenarios where PowerShell scripting can be used to increase your efficiency when managing databases, which are the most critical resources in your Exchange environment. Towards the end, you'll discover how to achieve Exchange High Availability and how to secure your environment, monitor the health of Exchange, and integrate Exchange with Office Online Server, Skype for Business Server, and Exchange Online (Office 365). By the end of the book, you will be able to perform administrative tasks efficiently.
Table of Contents (17 chapters)

Working with script repositories

Windows Management Framework (WMF) version 5.1 includes a package manager called PowerShellGet, which enabled functionality such as find, get, install, and uninstall packages from internal and public sources. However, this recipe is not specific to Exchange. See this recipe as a tips and tricks recipe, since it's more PowerShell general than Exchange specific.

PowerShellGet is a package manager for Windows PowerShell. Basically, it is a wrapper around the Package Manager component which simplifies the package management for PowerShell modules. PowerShellGet is built on top of the well-known package management solution, NuGet.

Package Manager is a unified package management component which allows you to search for software install, uninstall, and inventory of any type of software that it supports through the provider interface.

Package Manager works with the community based software repository called PowerShell Gallery. Currently PowerShell Gallery has more than 1,500 unique packages to download and use for free.

Chocolatey is also still available; it's also a community based software repository. Some examples of how to use these can be found in the There's more section.

There are a bunch of galleries (also referred to as providers) to use and select between, such as Chocolatey, PowerShell Resource Gallery (Microsoft supported), MyGet, Inedo ProGet, JFrog Artifactory, and many more.

For a better understanding, let's take a look at the first example.

How to do it...

In this example, we will use PowerShellGet to install two example modules from PowerShell Gallery:

    Import-Module -Name PackageManagement
    
    Get-Command -Module PackageManagement
    
    Find-Package | Out-GridView
    
    Find-Package -Name "AzureAD"
    
    Find-Package -Name "GetUptime"
    
    Install-Package -Name "AzureAD"
    
    Install-Package -Name "GetUptime"
    
    Get-Package -ProviderName PowerShellGet  

How it works...

For illustrating how the Package Manager works, see the preceding example.

First we imported the module of Package Management, for using the cmdlets for the Package Manager. We then used the Get-Command cmdlet to see what commands are available with this module.

With the Find-Package cmdlet, we searched for the available packages. First we piped the results to a GridView, since this can be user friendly to watch instead of text. Once we found the packages we were looking for (in this example Notepad++ and 7zip), we were using the Install-Package cmdlet to install these packages. The following screenshot shows when the installation had taken place, the packages were now available for use and could be found from the start button.

Once the packages/modules have been successfully installed they can be imported and utilized by using the following cmdlets:

    Import-Module GetUptime
    
    Get-Command -Module GetUptime
    
    Get-Uptime  

The following screenshot shows an example of the GetUptime module, where the module shows the server uptime:

Once the packages are in place and it has been verified that everything is working as expected, let's finalize this by uninstalling them. Some examples of cmdlets for uninstalling packages are shown here:

    Uninstall-Package -Name "AzureAD"
    
    Uninstall-Package -Name "GetUptime"  

There's more...

We have now been using the built-in package manager based on PowerShellGet. In the previous book called "Microsoft Exchange Server PowerShell Cookbook - Third Edition", we also used Chocolatey, where we can install third-party rich applications.

We'll take a look at how we can utilize chocolatey with Windows 2016 Server and Exchange 2016:

    iwr https://chocolatey.org/install.ps1 -UseBasicParsing | iex
    choco upgrade chocolatey
    
    choco install notepadplusplus.install
    choco upgrade notepadplusplus.install
    choco uninstall notepadplusplus.install
    
    choco install 7zip.install
    choco upgrade 7zip.install
    choco uninstall 7zip.install  

In the preceding examples, we start by installing chocolatey. The second cmdlet is used for upgrading the existing installation of chocolatey. This is followed by two examples of how to install, upgrade, and uninstall two third-party application packages (Notepad++ and 7zip).

Chocolatey is great in many ways, but probably most companies, or at least enterprise companies, want to have their own "internal", more trusted and reliable repository, but still hosted on the internet.

Let's take a look at how this can be established. First let's sign up for an account at an optional provider.

In my case, I've used http://www.myget.org as the provider and created a feed when the account was created.

Now, let's see how the feed can be used as a repository. The feed that was created had an URL of: https://www.myget.org/F/tlpowershell/. Once it's created, we have to register it as a repository in PowerShell using the Register-PSRepository cmdlet:

    Register-PSRepository -Name MyGet -SourceLocation `
    https://www.myget.org/F/tlpowershell/api/v1 `
    -PublishLocation https://www.myget.org/F/tlpowershell/ `
    -InstallationPolicy Trusted
    
    Find-Package -Source MyGet  

Since the MyGet repository is brand new, there are currently no packages. So the next action is to upload a package to MyGet. For being able to upload a module, the module itself should have a file extension of .psm1 together with the module manifest using an extension of .psd1. In the manifest, it's necessary to include the values of Author and Description, but I want to recommend that the value of RootModule, ModuleVersion, and CompanyName, are also included. The following examples show how the manifest was created and also how the modules were published to MyGet.

    New-ModuleManifest -Path ` C:\Windows\System32\WindowsPowerShell\v1.0\Modules\mailboxes.psd1` -Author "Jonas Andersson" -CompanyName "Testlabs, Inc." `
-RootModule "mailboxes" -Description `
"Module that lists mailboxes" -ModuleVersion "1.0"
Import-Module PowerShellGet

$PSGalleryPublishUri = ` 'https://www.myget.org/F/tlpowershell/api/v2/package'

$PSGallerySourceUri = ` 'https://www.myget.org/F/tlpowershell/api/v2'

Publish-Module -Name mailboxes -NuGetApiKey `
a2d5b281-c862-4125-9523-be42ef21f55a -Repository MyGet
Find-Package -Source MyGet Install-Package -Name "mailboxes" -Source MyGet

Before ending this recipe, we might want to remove the repository for some reason. This is done simply by running the following cmdlet:

    Unregister-PSRepository -Name MyGet  

See also

  • The Understanding the new execution policy recipe in this chapter
  • The Creating custom objects recipe in this chapter