Book Image

Learning PowerCLI - Second Edition

By : Robert van den Nieuwendijk
Book Image

Learning PowerCLI - Second Edition

By: Robert van den Nieuwendijk

Overview of this book

VMware vSphere PowerCLI, a free extension to Microsoft Windows PowerShell, enables you to automate the management of a VMware vSphere or vCloud environment. This book will show you how to automate your tasks and make your job easier. Starting with an introduction to the basics of PowerCLI, the book will teach you how to manage your vSphere and vCloud infrastructure from the command line. To help you manage a vSphere host overall, you will learn how to manage vSphere ESXi hosts, host profiles, host services, host firewall, and deploy and upgrade ESXi hosts using Image Builder and Auto Deploy. The next chapter will not only teach you how to create datastore and datastore clusters, but you’ll also work with profile-driven and policy-based storage to manage your storage. To create a disaster recovery solution and retrieve information from vRealize Operations, you will learn how to use Site Recovery Manager and vRealize Operations respectively. Towards the end, you’ll see how to use the REST APIs from PowerShell to manage NSX and vRealize Automation and create patch baselines, scan hosts against the baselines for missing patches, and re-mediate hosts. By the end of the book, you will be capable of using the best tool to automate the management and configuration of VMware vSphere.
Table of Contents (22 chapters)
Learning PowerCLI Second Edition
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Preface

VMware PowerCLI is a command-line automation and scripting tool that provides a Microsoft PowerShell interface to the VMware vSphere and vCloud products. Learning PowerCLI shows you how to install and use PowerCLI to automate the management of your VMware vSphere environment. With lots of examples, this book will teach you how to manage vSphere from the command line and how to create advanced PowerCLI scripts.

What this book covers

Chapter 1 , Introduction to PowerCLI, gets you started using PowerCLI. First, you will see how to download and install PowerCLI. Then, you will learn to connect to and disconnect from the vCenter and ESXi servers and retrieve a list of all of your hosts and virtual machines.

Chapter 2 , Learning Basic PowerCLI Concepts, introduces the Get-Help, Get-Command, and Get-Member cmdlets. It explains the difference between PowerShell Providers and PSdrives. You will see how you can use the raw vSphere API objects from PowerCLI and how to use the New-VIProperty cmdlet to extend a PowerCLI object.

Chapter 3 , Working with Objects in PowerShell, concentrates on objects, properties, and methods. This chapter shows how you can use the pipeline to use the output of one command as the input of another command. You will learn how to use the PowerShell object cmdlets and how to create PowerShell objects.

Chapter 4 , Managing vSphere Hosts with PowerCLI, covers the management of the vSphere ESXi servers. You will see how to add hosts to the vCenter server and how to remove them. You will work with host profiles, host services, Image Builder, and Auto Deploy, as well as with the esxcli command and the vSphere CLI commands from PowerCLI.

Chapter 5 , Managing Virtual Machines with PowerCLI, examines the lifecycle of virtual machines-from creating to removing them. Creating templates, updating VMware Tools and upgrading virtual hardware, running commands in the guest OS, and configuring fault tolerance are some of the topics discussed in this chapter.

Chapter 6 , Managing Virtual Networks with PowerCLI, walks you through vSphere Standard Switches and vSphere Distributed Switches, port groups, and network adapters. It shows you how to configure host networking and how to configure the network of a virtual machine.

Chapter 7 , Managing Storage, explores creating and removing datastores and datastore clusters, working with Raw Device Mapping, configuring software iSCSI initiators, Storage I/O Control, and Storage DRS.

Chapter 8 , Managing High Availability and Clustering, covers HA and DRS clusters, DRS rules and DRS groups, resource pools, and Distributed Power Management.

Chapter 9 , Managing vCenter Server, shows you how to work with privileges, work with roles and permissions, manage licenses, configure alarm definitions, alarm action triggers, and retrieve events.

Chapter 10 , Patching ESXi Hosts and Upgrading Virtual Machines, focusses on using VMware vSphere Update Manager to download patches, creating baselines and baseline groups, testing virtual machines and hosts for compliance, staging patches, and remediating inventory objects.

Chapter 11 , Managing VMware vCloud Director and vCloud Air, covers connecting to vCloud servers, retrieving organizations, virtual datacenters, organization networks, and users, using vCloud virtual machines and appliances, and using snapshots.

Chapter 12 , Using Site Recovery Manager, explores the Meadowcroft.SRM module to manage SRM protection groups, protecting virtual machines and running recovery plans to migrate or fail-over virtual machines from the protected site to the recovery site.

Chapter 13 , Using vRealize Operations Manager, shows you to use alerts, retrieve recommendations, statistical data, solutions, and traversalSpecs, manage local user accounts and user roles and create and retrieve reports.

Chapter 14 , Using REST API to Manage NSX and vRealize Automation, walks you through REST APIs with examples from VMware NSX and vRealize Automation using basic authentication and bearer tokens, XML, and JSON.

Chapter 15 , Reporting with PowerCLI, concentrates on retrieving log files and log bundles, performance reporting, exporting reports to CSV files, generating HTML reports, sending reports by e-mail, and reporting the health of your vSphere environment with the vCheck script.

What you need for this book

To run the example PowerCLI scripts given in this book, you need the following software:

  • VMware PowerCLI

  • Microsoft PowerShell

  • VMware vCenter Server

  • VMware ESXi

  • VMware vSphere Update Manager

  • VMware vCloud Director

  • VMware Site Recovery Manager

  • VMware vSphere Replication

  • VMware vRealize Operations Manager

  • VMware NSX

  • VMware vRealize Automation

If you don't have specific software installed, you can use the VMware Hands-on Labs at https://labs.hol.vmware.com/ to test the scripts.

The scripts in this book are tested using VMware PowerCLI 6.5 Release 1, VMware vCenter Server 6.5, and VMware ESXi 6.5. Microsoft PowerShell and VMware PowerCLI are free. You can download a free 60-day evaluation of VMware vCenter Server and VMware ESXi from the VMware website. It is not possible to modify the settings on the free VMware vSphere Hypervisor using PowerCLI.

Who this book is for

This book is written for VMware vSphere administrators who want to automate their vSphere environment using PowerCLI. It is assumed that you have at least a basic knowledge of VMware vSphere. If you are not a vSphere administrator, but you are interested in learning more about PowerCLI, then this book will also give you some basic knowledge of vSphere.

Conventions

In this book, you will find a number of text styles 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: "The script uses the Get-Cluster cmdlet to retrieve all the clusters."

A block of code is set as follows:

$HostName = '192.168.0.133' 
$iSCSITarget = '192.168.0.157' 
$VirtualSwitchName = 'vSwitch2' 
$NicName = 'vmnic3' 
$PortGroupName = 'iSCSI Port group 1' 
$ChapType = 'Preferred' 
$ChapUser = 'Cluster01User' 
$ChapPassword = ' Cluster01Pwd' 
$DatastoreName = 'Cluster01_iSCSI01' 

Any command-line input or output is written as follows:

PowerCLI C:\> New-VM -Name VM1 -ResourcePool (Get-Cluster
  -Name Cluster01) 

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "If your cluster is incorrectly configured, the vSphere Web Client will show you the issues in the  Summary  tab."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important to us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail [email protected], and mention the book's title in the subject of your message.

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 at www.packtpub.com/authors .

Customer support

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.

Downloading the example code

You can download the example code files for this book 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.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.

  2. Hover the mouse pointer on the SUPPORT tab at the top.

  3. Click on Code Downloads & Errata.

  4. Enter the name of the book in the Search box.

  5. Select the book for which you're looking to download the code files.

  6. Choose from the drop-down menu where you purchased this book from.

  7. Click on Code Download.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows

  • Zipeg / iZip / UnRarX for Mac

  • 7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Learning-PowerCLI-Second-Edition. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Errata

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 could 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/submit-errata , selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted, and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted 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.

Questions

If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.