Microsoft Office 365 Administration Cookbook
上QQ阅读APP看书,第一时间看更新

Setting up the PowerShell environment

PowerShell is an ever-growing scripting language that allows network and system admins to interact with Microsoft products in an elevated manner. Admins can use PowerShell to create new users, make changes to users, interact with MS-SQL tables, move and manipulate files, and much more. PowerShell cmdlets are single-purpose functions with specific rules and syntax, but there are hundreds of these cmdlets, giving admins a flexible and extensible toolset with which to customize, administer, and run their tenant.

As there are so many possible uses of PowerShell, Chapter 3, Administering Office 365 with PowerShell, is dedicated to the topic. However, every admin must start somewhere, and that is why the basic steps to setting up your PowerShell environment are presented here in the following recipe—steps for connecting your desktop or laptop to your Office 365 tenant.

Getting ready

To start, an admin must have PowerShell installed, either the command-line, black screen version or an Integrated Scripting Environment (ISE). You must have valid credentials and have an appropriate admin role (such as Global Admin).

As PowerShell for Office 365 has advanced, additional tools have become available to minimize previously known obstacles when making a connection to your tenant. For this, you must download the Microsoft Online Services Sign-In Assistant (the link for this appears in the See also section of this recipe). Download the files and install it on your PC.

The first time you connect to your Office 365 tenant, you'll want to set the Execution Policy to RemoteSigned. This is done by opening a PowerShell window with elevated rights (Run as administrator) and executing the following code:

Set-ExecutionPolicy RemoteSigned

Once prompted, press Y and hit Enter.

Additionally, you will need to execute and install the following PowerShell modules:

Install-Module MSOnline

You may be prompted that the NuGet provider is required in order to continue. Press Y and press Enter to continue. As it downloads, you may see an additional warning regarding an untrusted repository. The repository is Microsoft and is trusted, so press Y and press Enter to continue:

Figure 1.3 – Screenshot of PowerShell commands when installing the MSOnline module

Figure 1.3 – Screenshot of PowerShell commands when installing the MSOnline module

How to do it…

Open Windows PowerShell or the PowerShell ISE as an administrator by searching for PowerShell in your applications, right-click, and select Run as administrator:

Figure 1.4 – Right-clicking PowerShell from Start menu provides Run as administrator option

Figure 1.4 – Right-clicking PowerShell from Start menu provides Run as administrator option

Assuming a standard login (see There's more… section for information about multi-factor authentication (MFA)), you'll create a Windows PowerShell credentials object by executing the following command:

$credential = Get-Credential

Once executed, the PowerShell window will prompt you for your credentials. Click OK to proceed:

Figure 1.5 – PowerShell prompts for credentials when using Get-Credential

Figure 1.5 – PowerShell prompts for credentials when using Get-Credential

With the credentials saved to the $credential variable, you can connect to Office 365 by executing the following command:

Connect-MsolService –Credential $credential

This is shown in the following screenshot:

Figure 1.6 – PowerShell screen showing use of stored credential variable to connect to MsolService

Figure 1.6 – PowerShell screen showing use of stored credential variable to connect to MsolService

As the preceding screenshot shows, it is not clear if the system is connected. At this point, you can test to see if you are connected by executing the following command:

Get-MsolDomain

The Get-MsolDomain cmdlet shows you all domains associated with the connected tenant and proves you are ready to start using PowerShell as an admin tool on your tenant:

Figure 1.7 – PowerShell screen showing domains found using Get-MsolDomain

Figure 1.7 – PowerShell screen showing domains found using Get-MsolDomain

How it works…

You've just configured the essentials needed to complete most PowerShell cmdlets you'll need as an Office 365 admin.

PowerShell has been around for many years but historically has not required setup. On-prem network admins are expected to log in to a server and start executing commands. Interacting from your PC with an online tenant is technically a remote session (something PowerShell experts will recognize). Connecting in this way requires passing credentials to gain access to the tenant.

This recipe focused on getting you ready to use PowerShell in your tenant. Chapter 3, Administering Office 365 with PowerShell will cover many of the options available to an admin.

There's more…

If using MFA (not standard login), you'd authenticate by skipping Step 2 and removing the credential parameter from Step 3. Step 3 would instead resemble the following:

Connect-MsolService

This brings up the modern Office 365/Azure login screen that supports MFA.

See also

Microsoft Online Services Sign-In Assistant for IT Professionals RTW download page: https://www.microsoft.com/en-us/download/details.aspx?id=41950