Book Image

Mastering PowerCLI

By : Sajal Debnath
Book Image

Mastering PowerCLI

By: Sajal Debnath

Overview of this book

Have you ever wished that every morning you could automatically get a report with all the relevant information about your datacenter in exactly the same format you want? Or whether you could automate that boring, exhausting task? What if some crucial task needs to be performed on a regular basis without any error? PowerCLI scripts do all that and much more for VMware environments. It is built on top of the popular Windows PowerShell, with which you can automate server tasks and reduce manual input, allowing you to focus on more important tasks. This book will help you to achieve your goals by starting with a short refresher on PowerShell and PowerCLI and then covering the nuances of advanced functions and reusable scripts. Next you will learn how to build a vSphere-powered virtualized datacenter using PowerCLI while managing different aspects of the environment including automated installation, network, and storage. You will then manage different logical constructs of vSphere environment and different aspects of a virtual machine. Later, you will implement the best practices for a security implementation in vSphere Environment through PowerCLI before discovering how to manage other VMware environments such as SRM, vCloud Director and vCloud Air through PowerCLI. You will also learn to manage vSphere environments using advanced properties by accessing vSphere API and REST APIs through PowerCLI. Finally, you will build a Windows GUI application using PowerShell followed by a couple of sample scripts for reporting and managing vSphere environments with detailed explanations of the scripts. By the end of the book, you will have the required in-depth knowledge to master the art of PowerCLI scripting.
Table of Contents (21 chapters)
Mastering PowerCLI
Credits
Foreword
About the Author
Acknowledgment
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

If you are a system administrator who manages a considerable-sized environment, then I do not need to elaborate on the importance of scripting to you. Scripting was and always will be one of the most important arsenals in a system administrator's weaponry. With the term scripting, till very recently typically bash or other such shell scripts used to come to mind and more advanced ones, such as Perl, PHP, or Ruby. I love this scripting language for the sheer beauty and power that it presents. If you are coming from a *NIX environment, PowerShell will completely change your perception about scripting. If you are managing a vSphere environment, then besides vRealize Orchestrator, PowerCLI is the most powerful tool available to help you automate the different aspects of a vSphere environment. Probably, if I need to get something done really quickly, I will still rely on PowerCLI scripting.

In all my years of experience as a working professional and before that as a student and teacher, I have seen primarily two methods of explanation: the first approach, and the most widely used one, is to take an example problem and solve it while explaining the solution to the student. This way, the student learns how to solve a particular type of problem. The second approach is the one in which a teacher explains the basic logic and principles of a solution behind the problem, and then asks the students to solve the problem all by themselves. As a student, I always found myself struggling with the first approach. Though the first approach was easier to understand, it tends to limit my knowledge to solving only similar problems. Because of the lack of the understanding of the underlying logic, faced with a new problem, I could not solve it most of the time. This happened especially in mathematical problems. It was like showing me a program written in C to implement Dijkstra's algorithm and explaining how this program was written. Knowing this, will I be able to implement any other algorithm in C or utilize Dijkstra's algorithm for my advantage? Probably not. Instead, if someone teaches me the different aspects of C language and how to write programs using C, then I can utilize this knowledge to write any programs.

You may disagree or agree with me, but I always preferred the second approach as it worked for me and gave me a better understanding and hold on the topic.

So, throughout this book, I tried to explain all the building blocks of advanced PowerShell and PowerCLI scripting and then provided examples to showcase what I am trying to say. I tried this approach with the hope that it will give you a better understanding and clarity of the underlying constructs so that you can build on top of this.

What this book covers

Chapter 1, PowerShell and PowerCLI Refresher, refreshes the basics of PowerShell and introduces you to concepts that are necessary to develop, run, and test PowerShell scripts using PowerCLI cmdlets.

Chapter 2, Reusable Advanced Functions and Scripts, covers how to create reusable advanced functions and scripts. Advanced functions can be used like cmdlets in PowerShell and give much more power over normal functions.

Chapter 3, Deploying vSphere Hosts, covers how to automatically deploy and configure ESXi hosts using different tools, such as Image Builder, Auto Deploy, and Host Profiles.

Chapter 4, Managing Networks, discusses how to configure and manage vSphere networking through standard and distributed switches using PowerCLI.

Chapter 5, Managing Storage, discusses how to create and manage storage in a vSphere environment, including SAN, NAS, iSCSI, and VSAN.

Chapter 6, Managing Clusters and Other Constructs, covers how to manage logical constructs in a vCenter environment, including but not limited to HA and DRS clusters.

Chapter 7, Managing Virtual Machines, covers how to configure the different aspects of virtual machines and guest operating systems using PowerCLI.

Chapter 8, Managing vSphere Security, SRM, vCloud Air, and vROps, covers security hardening and patching vSphere environments along with other VMware solutions, such as SRM, vCloud Air, and vRealize Operations Manager.

Chapter 9, Managing the vSphere API, discusses how to use the vSphere API in PowerCLI and how we can manage advanced aspects of a vSphere environment using APIs.

Chapter 10, Using REST APIs, discusses Representational state transfer (REST) APIs and how PowerCLI can be used to manage the VMware vRealize Automation environment using REST APIs.

Chapter 11, Creating Windows GUI, discusses how to create a Windows graphical user interface (GUI) using PowerShell and other tools.

Chapter 12, Best Practices and Sample Scripts, describes PowerShell scripting best practices. This chapter also covers two sample scripts, one to get a security report and another to find the capacity of a vSphere environment.

What you need for this book

To test the examples provided in Chapters 1 to 7 and to practice further, you need to have access to a vSphere environment with vCenter and ESXi servers. For Chapter 8, Managing vSphere Security, SRM, vCloud Air, and vROps, you need to have access to the vCloud Air and vRealize Operations Manager environment. For Chapter 10, Using REST APIs, you need to have access to the vRealize Automation environment. I have used the following versions of different software to write this book:

  • PowerShell Version 5.0.10532.0

  • For most part PowerCLI 6.0 R1, and for Chapter 8, Managing vSphere Security, SRM, vCloud Air, and vROps PowerCLI 6.0 R2

  • ESXi 6.0

  • vCenter 6.0

  • vRealize Automation 6.2

  • vRealize Operations 6.2

If you do not have access to physical server-grade systems, you can build your test setup in a lab environment as well. For most of the book, I used a Whitebox desktop with 32 GB RAM in it. To build a vSphere environment, I have used VMware Workstation Version 12 and nested VMs for ESXi hosts. For vRealize Operations Manager, SRM, and vCloud Air, I had to rely on traditional resources.

So, before you start reading the chapters, I suggest that you get ready to build a vSphere lab. Also, as the chapters progress, we will build the lab step by step because different chapters cover different areas of a vSphere environment, starting from the ground up.

Who this book is for

This book is aimed at anyone who has a working knowledge of PowerShell and PowerCLI and who wants to script a vSphere environment like a pro with PowerCLI and PowerShell. This book is written from a system admin's perspective, so you do not need to be a development wizard. You just need to know the basics of programming and programming logic so that you can utilize the knowledge gained in this book to your advantage.

Conventions

In this book, you will find a number of styles of text 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: "I strongly suggest that you type Get-Help in PowerShell and read the output."

A block of code is set as follows:

Switch (value) {
Pattern 1 {Script Block}
Pattern 2 {Script Block}
Pattern n {Script Block}
Default   {Script Block}
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

{
  "firstName": "Sajal",
  "lastName": "Debnath",
  "isWorking": true,
  "age": 35,
  "address": {
    "streetAddress": "2435 A.B.C Road",
    "city": "New Delhi",
    "state": "Karnataka",
    "postalCode": "123456"
  },
  "contactNumbers": [
    {
      "type": "home",
      "number": "1234567897"
    },
    {
      "type": "office",
      "number": "123456789876"
    }
  ],
  "children": [],
  "spouse": null
}

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

PS C:\> Connect-CIServer -Server <server name> -User <user name>-Password <Password>

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "To unblock it, right-click on the file, and select Properties. From the Properties menu, select unblock."

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 may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via 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 on 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 all Packt books you have purchased 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.

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 would 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 on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright 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 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

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.