PowerShell for Office 365
eBook - ePub

PowerShell for Office 365

  1. 222 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

PowerShell for Office 365

About this book

Learn the art of leveraging PowerShell to automate Office 365 repetitive tasksAbout This Book• Master the fundamentals of PowerShell to automate Office 365 tasks.• Easily administer scenarios such as user management, reporting, cloud services, and many more.• A fast-paced guide that leverages PowerShell commands to increase your productivity.Who This Book Is ForThe book is aimed at sys admins who are administering office 365 tasks and looking forward to automate the manual tasks. They have no knowledge about PowerShell however basic understanding of PowerShell would be advantageous.What You Will Learn• Understand the benefits of scripting and automation and get started using Powershell with Office 365• Explore various PowerShell packages and permissions required to manage Office 365 through PowerShell• Create, manage, and remove Office 365 accounts and licenses using PowerShell and the Azure AD• Learn about using powershell on other platforms and how to use Office 365 APIs through remoting• Work with Exchange Online and SharePoint Online using PowerShell• Automate your tasks and build easy-to-read reports using PowerShellIn DetailWhile most common administrative tasks are available via the Office 365 admin center, many IT professionals are unaware of the real power that is available to them below the surface. This book aims to educate readers on how learning PowerShell for Office 365 can simplify repetitive and complex administrative tasks, and enable greater control than is available on the surface.The book starts by teaching readers how to access Office 365 through PowerShell and then explains the PowerShell fundamentals required for automating Office 365 tasks.You will then walk through common administrative cmdlets to manage accounts, licensing, and other scenarios such as automating the importing of multiple users, assigning licenses in Office 365, distribution groups, passwords, and so on.Using practical examples, you will learn to enhance your current functionality by working with Exchange Online, and SharePoint Online using PowerShell. Finally, the book will help you effectively manage complex and repetitive tasks (such as license and account management) and build productive reports.By the end of the book, you will have automated major repetitive tasks in Office 365 using PowerShell.Style and approachThis step by step guide focuses on teaching the fundamentals of working with PowerShell for Office 365. It covers practical usage examples such as managing user accounts, licensing, and administering common Office 365 services. You will be able to leverage the processes laid out in the book so that you can move forward and explore other less common administrative tasks or functions.

Tools to learn more effectively

Saving Books

Saving Books

Keyword Search

Keyword Search

Annotating Text

Annotating Text

Listen to it instead

Listen to it instead

Information

Script Automation

Up until this point, we have covered the different Office 365 APIs, and you should have a good foundation for scripting for the reviewed products. In this chapter, we will cover topics that help you develop a scripting infrastructure.
By the end of this chapter, you will have learned techniques to successfully implement, deploy, secure, and monitor automated scripts.
In this chapter, we will cover the following topics:
  • PowerShell modules
  • Certificates and code signing
  • Credential management
  • Tracing and script automation

PowerShell modules

In the previous chapters, we have used files to package scripts, functions to easily reuse our code, and the modules for each of the Office 365 APIs. As your code gets more complex, the logical progression is to implement your own modules.
Advantages of modules are as follows:
  • Package functionality for distribution and reuse
  • Provides documentation infrastructure
  • Can have private and public functions
We will get started by reviewing the available commands to work with modules.
To showcase the advantages of modules, we will create an example. Our module will support a dashboard in SharePoint Online. The dashboard will display daily sales for each of the products in the database.
So that we do not deviate too much from the scope of this chapter, we will use the AdventureWorks schema (http://msftdbprodsamples.codeplex.com/releases/view/55330). This database contains a simple model of products, customers, and sales that we will use to calculate daily sales.
In this scenario, we calculate the product daily sales and update a SharePoint list with that information. Our module will contain functions to read from a SQL Server database and to connect to SharePoint Online and update a list:
If you can expose your database publicly, this scenario could be better implemented with SharePoint Business Connectivity Services (BCS). BCS allows direct integration with SQL Server and other data sources.

Module manifest

A PowerShell module consists of at least a manifest file (the extension .psd1). It is common to at least have a main module file (the extension .psm1). A manifest file can reference additional modules, scripts, and supporting files as required.
The manifest file contains metadata of the script, including the name, dependencies, and version. The New-ModuleManifest command will generate the manifest file:
 New-ModuleManifest -Path SalesDashboardModule.psd1 -Author Martin
-CompanyName Packt -Copyright 2017
You can pass all of the parameters to the command, but it is usually easier to modify the manifest file directly after it's generated since it is a text file and will require constant updates during the development stage:
Use Show-Command to inspect a new command or one with many parameters.
The following screenshot shows the output for Show-Command. For the time being we will fill out the parameters used in the previous example. We will review the remaining parameters throughout the chapter:
In the following manifest, we have modified the RootModule, FileList, FunctionsToExport, and NestedModules properties:
 # Module manifest for module 'SalesDashboardModule'

@{ # Script module or binary module file associated with this manifest.
RootModule = 'SalesDashboardModule'

# Version number of this module.
ModuleVersion = '1.0.0.1'

# Functions to export from this module
FunctionsToExport = @('Get-DailyProductSalesQuery','Get-DailyProductSalesTotals')

# List of all files packaged with this module
FileList = @("DailyProductSalesQuery.sql")

# Modules to import as nested modules of the module specified in RootModule
NestedModules = @('SqlFunctions.psm1', 'SPOFunctions.psm1')
... }
RootModule is the filename of the main script file that will be loaded by the module. In this case, the SalesDashboardModule.psm1 file must be in the same folder as the manifest file.
FileList is an array of filenames associated with the module. This property is public and can be used to reference additional resources required by the module. In this example, we added a SQL file that will contain the query to retrieve data from the sample database. It is common to have inline strings within a PowerShell script, but if your string is long or requires a specialized editor, this approach might be better.
The FunctionsToExport property (in addition to CmdletsToExport and AliasesToExport) indicates which functions will be available by users of your module. Use this array to keep functions private.
Lastly, we have two NestedModules. The two files specified aggregate functions related to SQL Server and SharePoint Online. This might be unnecessary for such a small example, but it is a good practice as your scripts get more complex.
ModuleVersion is also worth updating as it will help while troubleshooting.
In the following session, we load and inspect the module. Note the ErrorAction parameter in the Remove-Module command. This command will likely fail since the module should not be loaded at the beginning of the script. The Ignore setting will prevent the error message from being shown and continue execution:
 PS C:> Remove-Module SalesDashboardModule -ErrorAction Ignore
PS C:> $module = Get-Module SalesDashboardModule
PS C:> if($module -eq $null) {
Import-Module -Name C:\temp\SalesDashboardModule -Verbose
$module = Get-Module SalesDashboardModule
}

VERBOSE: Loading module from path 'C:\temp\SalesDashboardModule\SalesDashboardModule.psd1'.
VERBOSE: Loading module from path 'C:\temp\SalesDashboardModule\SqlFunctions.psm1'.
VERBOSE: Loading module from path 'C:\temp\SalesDashboard\ModuleSPOFunctions.psm1'.
...
VERBOSE: Loading module from path 'C:\temp\SalesDashboard\ModuleSalesDashboardModule.psm1'.
VERBOSE: Exporting function 'Get-DailyProductSalesQuery'.
VERBOSE: Exporting function 'Get-DailyProductSalesTotals'.
...
VERBOSE: Exporting function 'Start-DbSync'.
VERBOSE: Importing function 'Get-DailyProductSalesQuery'.
VERBOSE: Importing function 'Get-DailyProductSalesTotals'.

PS C:> $module.Version
Major Minor Build Revision
----- ----- ----- --------
1 0 0 4

PS C:> $module.FileList
C:\temp\SalesDashboard\ModuleDailyProductSalesQuery.sql

PS C:> $module.ExportedFunctions.Keys
Get-DailyProductSalesQuery
Get-DailyProductSalesTotals
...
Removing the module at the beginning of the script is a good practice, particularly during development, when you will run the script repeatedly.

Script modules

In the SqlFunctions module, we have two functions. Get-DailyProductsSalesQuery will return the contents of the SQL file. Note that within the function, we get a reference to the module. We then use the Fi...

Table of contents

  1. Title Page
  2. Copyright
  3. Credits
  4. About the Authors
  5. About the Reviewer
  6. www.PacktPub.com
  7. Customer Feedback
  8. Preface
  9. PowerShell Fundamentals
  10. Managing Office 365 with PowerShell
  11. Azure AD and Licensing Management
  12. Managing SharePoint Online Using PowerShell
  13. Managing Exchange Online Using PowerShell
  14. Script Automation
  15. Patterns and Practices PowerShell
  16. OneDrive for Business
  17. PowerShell Core

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn how to download books offline
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 990+ topics, we’ve got you covered! Learn about our mission
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more about Read Aloud
Yes! You can use the Perlego app on both iOS and Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app
Yes, you can access PowerShell for Office 365 by Martin Machado, Prashant G Bhoyar in PDF and/or ePUB format, as well as other popular books in Computer Science & Cloud Computing. We have over one million books available in our catalogue for you to explore.