Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Learning PowerCLI
Learning PowerCLI

Learning PowerCLI: A comprehensive guide on PowerCLI , Second Edition

Arrow left icon
Profile Icon van den Nieuwendijk
Arrow right icon
$9.99 $43.99
Full star icon Full star icon Full star icon Full star icon Full star icon 5 (1 Ratings)
eBook Feb 2017 562 pages 2nd Edition
eBook
$9.99 $43.99
Paperback
$54.99
Subscription
Free Trial
Renews at $19.99p/m
Arrow left icon
Profile Icon van den Nieuwendijk
Arrow right icon
$9.99 $43.99
Full star icon Full star icon Full star icon Full star icon Full star icon 5 (1 Ratings)
eBook Feb 2017 562 pages 2nd Edition
eBook
$9.99 $43.99
Paperback
$54.99
Subscription
Free Trial
Renews at $19.99p/m
eBook
$9.99 $43.99
Paperback
$54.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Learning PowerCLI

Chapter 1. Introduction to PowerCLI

Have you ever had to create 200 virtual machines in a short period of time, change a setting on all of your hosts, or make an advanced report for your boss to show how full the hard disks of your virtual machines are? If you have, you know that performing these tasks using the vSphere web client will take a lot of time. This is where automation can make your job easier. VMware PowerCLI is a powerful tool that can perform these tasks and much more. And the best thing is that it is free!

VMware PowerCLI is a command-line interface (CLI) distributed as a collection of Microsoft PowerShell modules and snap-ins. Microsoft PowerShell is Microsoft's command shell and scripting language, designed with the systems administrator in mind. Microsoft PowerShell is available on every Microsoft Windows server or workstation since Windows Server 2008 R2 and Windows 7. VMware PowerCLI is an extension to Microsoft PowerShell. This means that all of the features of PowerShell can be used in PowerCLI. You can use PowerCLI to automate your vSphere hosts, virtual machines, virtual networks, storage, clusters, vCenter Servers, and more.

In this chapter, you will learn:

  • Downloading and installing PowerCLI
  • Participating in the VMware Customer Improvement Program
  • Modifying the PowerShell execution policy
  • Creating a PowerShell profile
  • Connecting and disconnecting servers
  • Using the credential store
  • Retrieving a list of all of your virtual machines
  • Retrieving a list of all of your hosts

Downloading and installing PowerCLI

In this section, you will learn how to download and install PowerCLI 6.5 Release 1. First, we will list the requirements for PowerCLI 6.5 Release 1. After downloading PowerCLI from the VMware website, we will install PowerCLI on your system.

Requirements for using PowerCLI 6.5 Release 1

You can install VMware PowerCLI 6.5 Release 1, the version used for writing this book, on the following 64-bit operating systems:

  • Windows Server 2012 R2
  • Windows Server 2008 R2 Service Pack 1
  • Windows 10
  • Windows 8.1
  • Windows 7 Service Pack 1

VMware PowerCLI 6.5 Release 1 is compatible with the following PowerShell versions:

  • Microsoft PowerShell 3.0
  • Microsoft PowerShell 4.0
  • Microsoft PowerShell 5.0
  • Microsoft PowerShell 5.1

If you want to work with VMware PowerCLI 6.5 Release 1, make sure that the following software is present on your system:

  • Microsoft PowerShell 3.0, 4.0, 5.0, or 5.1
  • NET Framework 4.5, 4.5.x, 4.6, or 4.6.x

Downloading PowerCLI 6.5 Release 1

Before you can install PowerCLI, you have to download the PowerCLI installer from the VMware website. You will need a My VMware account to do this.

Perform the following steps to download PowerCLI:

  1. Visit http://www.vmware.com/go/powercli . On this page, you will find a Resources section.

    Downloading PowerCLI 6.5 Release 1

  2. Click on the Download button to download PowerCLI.
  3. You have to log in with a My VMware account. If you don't have a My VMware account, you can register for free.

    Downloading PowerCLI 6.5 Release 1

  4. After you log in, you will be taken to the VMware PowerCLI download page. Click on the Download Now button to start downloading PowerCLI.

Installing PowerCLI

Perform the following steps to install PowerCLI:

  1. Run the PowerCLI installer that you just downloaded.
  2. Click Yes in the User Account Control window to accept the Do you want to allow this app to make changes to your device? option.
  3. If the PowerShell execution policy on your computer is not set to RemoteSigned, you will get a warning that tells you It is recommended that you set the execution policy to "RemoteSigned" in order to be able to execute scripts. After the installation of PowerCLI, I will show you how to set the execution policy. Click on Continue to continue to the installation of PowerCLI.

    Installing PowerCLI

  4. Click on Next > in the Welcome to the InstallShield Wizard for VMware PowerCLI window.

    Installing PowerCLI

  5. Select I accept the terms in the license agreement and click on Next >.

    Installing PowerCLI

  6. If you are not using vCloud Air, VMware vCloud Director, vSphere Update Manager, vRealize Operations Manager, or Horizon View, you can click on the little arrow to the left of a feature and select This feature will not be available. I recommend installing all of the features, to be able to run the scripts in all of the chapters in this book. If you want, you can change the installation directory by clicking on Change.... Click on Next >.

    Installing PowerCLI

  7. Click on Install to begin the installation.

    Installing PowerCLI

  8. Click on Finish to exit the installation wizard.

    Installing PowerCLI

After installing PowerCLI, you will have a VMware PowerCLI icon on your desktop. If you installed PowerCLI on a 64-bit computer, you will also have a VMware PowerCLI (32-Bit) icon. Some PowerCLI commands only work in the 32-bit version of PowerCLI. So keep both versions.

Participating in the VMware Customer Improvement Program

When you start PowerCLI for the first time, you will get the following screen:

Participating in the VMware Customer Improvement Program

You are asked to participate in the VMware Customer Improvement Program (CEIP). Type J to participate in the CEIP or type L to leave.

If you didn't get the text from the preceding screenshot, you may get the following error message:

. : File C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170.

Then, read the following section,  Modifying the PowerShell execution policy, to solve this problem.

Modifying the PowerShell execution policy

If this is the first time that you are using Microsoft PowerShell on the computer on which you installed PowerCLI, you have to change the execution policy to be able to start PowerCLI.

The Microsoft PowerShell execution policies define when you can run scripts or load configuration files. The possible values for the execution policy are Restricted, AllSigned, RemoteSigned, Unrestricted, Bypass, and Undefined.

Policy

Description

Restricted

This is the default execution policy. It allows you to run commands at the Command Prompt, but disables the execution of scripts. It will also disable the start of PowerCLI.

AllSigned

With the AllSigned execution policy, scripts can run, but they must be signed by a trusted publisher. If you run a script by a publisher that is not trusted yet, you will see a prompt asking whether you trust the publisher of the script.

RemoteSigned

The RemoteSigned execution policy allows you to run scripts that you have written on the local computer. Any script downloaded from the Internet must be signed by a trusted publisher or must be unblocked.

Unrestricted

When the execution policy is set to Unrestricted, unsigned scripts can run. If you run a script that has been downloaded from the Internet, you will get a security warning saying that this script can potentially harm your computer and asking whether you want to run this script.

Bypass

The Bypass execution policy blocks nothing and displays no warnings or prompts. This execution policy is designed for configurations in which a Microsoft PowerShell script is built into a larger application that has its own security model.

Undefined

The Undefined execution policy removes the execution policy from the current scope. If the execution policy in all scopes is Undefined, the effective execution policy is Restricted, which is the default execution policy. The Undefined execution policy will not remove an execution policy that is set in a Group Policy scope.

You can check the current execution policy setting with the following command:

PowerCLI C:\> Get-ExecutionPolicy

Get-ExecutionPolicy is a Microsoft PowerShell commandlet (cmdlet). Cmdlets are commands built into PowerShell or PowerCLI. They follow a verb-noun naming convention. The get cmdlets retrieve information about the item that is specified as the noun part of the cmdlet.

Set the execution policy to RemoteSigned to be able to start PowerCLI and run scripts written on the local computer with the Set-ExecutionPolicy -ExecutionPolicy RemoteSigned command.

Note

You have to run the Set-ExecutionPolicy -ExecutionPolicy RemoteSigned command from a PowerShell or PowerCLI session that you started using the Run as Administrator option, or you will get the following error message:

Set-ExecutionPolicy : Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied.

If you are using both the 32-bit and the 64-bit versions of PowerCLI, you have to run this command in both versions.

In the following screenshot of the PowerCLI console, you will see the output of the Set-ExecutionPolicy -ExecutionPolicy RemoteSigned command if you run this command in a PowerCLI session started with Run as Administrator.

Modifying the PowerShell execution policy

You can get more information about execution policies by typing the following command:

PowerCLI C:\> Get-Help about_Execution_Policies

To get more information about signing your scripts, type the following command:

PowerCLI C:\> Get-Help about_signing

Note

If you get an error message saying Get-Help could not find about_Execution_Policies in a help file, you have to run the Update-Help cmdlet in a PowerShell, or PowerCLI session started with Run as Administrator first. The Update-Help cmdlet downloads the newest help files for Microsoft PowerShell modules and installs them on your computer. Because Microsoft updates the Microsoft PowerShell help files on a regular basis, it is recommended to run the Update-Help cmdlet on a regular basis also.

Creating a PowerShell profile

If you want certain PowerCLI commands to be executed every time you start a PowerCLI session, you can put these commands in a PowerShell profile. The commands in a PowerShell profile will be executed every time you start a new PowerCLI session. There are six PowerShell profiles, two specific for the PowerShell console, two specific for the PowerShell Integrated Scripting Environment (ISE), and two used by both the PowerShell console and the PowerShell ISE. The PowerShell console and the PowerShell ISE have their own profiles for:

  • All users, current host
  • Current user, current host

The two profiles used by both the PowerShell console and the PowerShell ISE are:

  • All users, all hosts
  • Current user, all hosts

You can retrieve the locations for the different profiles of the PowerShell console by executing the following command in the PowerShell console. In this command, the $PROFILE variable is a standard PowerShell variable that returns an object containing the locations of the PowerShell profiles. This object is piped to the Format-List -Force command to display all of the properties of the $PROFILE object in a list:

PowerCLI C:\> $PROFILE | Format-List -Force


    AllUsersAllHosts       : C:\Windows\System32\WindowsPowerShell\v1.0\p
                         rofile.ps1
AllUsersCurrentHost    : C:\Windows\System32\WindowsPowerShell\v1.0\M
                         icrosoft.PowerShell_profile.ps1
CurrentUserAllHosts    : C:\Users\robert\Documents\WindowsPowerShell\
                         profile.ps1
CurrentUserCurrentHost : C:\Users\robert\Documents\WindowsPowerShell\
                         Microsoft.PowerShell_profile.ps1
Length                 : 76

Note

Downloading the example code

Detailed steps to download the code bundle are mentioned in the Preface of this book. Please have a look.

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

As you can see in the output of the preceding command, the $PROFILE object has four properties AllUsersAllHosts, AllUsersCurrentHost, CurrentUserAllHosts, and CurrentUserCurrentHost that contain the locations of the different profiles.

To list the locations of the PowerShell profiles of the PowerShell ISE, you have to execute the preceding command in the PowerShell ISE. This gives the following output:

PS C:\> $PROFILE | Format-List -Force


    AllUsersAllHosts       : C:\Windows\System32\WindowsPowerShell\v1.0\p
                         rofile.ps1
AllUsersCurrentHost    : C:\Windows\System32\WindowsPowerShell\v1.0\M
                         icrosoft.PowerShellISE_profile.ps1
CurrentUserAllHosts    : C:\Users\robert\Documents\WindowsPowerShell\
                         profile.ps1
CurrentUserCurrentHost : C:\Users\robert\Documents\WindowsPowerShell\
                         Microsoft.PowerShellISE_profile.ps1
Length                 : 79 

Note

You can start the PowerShell ISE from a Command Prompt by running powershell_ise.exe. You can start the PowerShell ISE from within a PowerShell console with the alias ise.

The default value for the $PROFILE variable is the value of the $PROFILE.CurrentUserCurrentHost property. So you can use $PROFILE instead of $PROFILE.CurrentUserCurrentHost.

You can determine if a specific profile exists by using the Test-Path cmdlet. The following command will test if the profile specified by $PROFILE exists:

PowerCLI C:\> Test-Path -Path $PROFILE
False

If a profile does not exist, as in the preceding example, you can create the profile using the New-Item cmdlet. If the directories in the path do not exist, by using the -Force parameter the New-Item cmdlet will create the directories. The following command will create the current user/current host profile and will also create the missing directories in the path:

PowerCLI C:\> New-Item -Path $PROFILE -ItemType file -Force

         Directory: C:\Users\robert\Documents\WindowsPowerShell

    Mode             LastWriteTime        Length  Name
----           ------------------     ------  ----
-a--           1/7/2017   2:01 PM          0  Microsoft.PowerShell_pro
                                              file.ps1

After creating the PowerShell profile, you can edit the profile using the PowerShell ISE with the following command:

PowerCLI C:\> ise $PROFILE

If you put the commands from the preceding section, Modifying the PowerShell execution policy, the new colors of the messages will be used in all of your PowerCLI sessions.

Left arrow icon Right arrow icon

Key benefits

  • This is first book on the market that will enlighten you on the latest version of PowerCLI and how to implement it
  • Effectively manage virtual machines, networks, and reports with the latest features of PowerCLI
  • A comprehensive and practical book on automating VMware vSphere

Description

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.

Who is this book for?

This book is ideal for you if you want to learn how to automate your VMware vSphere or vCloud infrastructure by getting the most out of PowerCLI. It’s assumed that you have some experience in administrating a vSphere or vCloud environment. Knowledge of Microsoft’s Windows PowerShell is not a prerequisite.

What you will learn

  • • Explore PowerShell and PowerCLI cmdlets and their output objects
  • • See how to manage virtual machines and work with virtual networks
  • • Manage vCloud Director from PowerCLI
  • • Use Site Recovery Manager from PowerCLI to create a disaster recovery solution
  • • Manage NSX and vRealize Automation using REST API with PowerCLI
  • • Create and configure vSphere HA and DRS clusters
  • • Use vSphere Update Manager with PowerCLI to create patch baselines and scan hosts
  • • Explore reporting techniques to retrieve log files

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Feb 23, 2017
Length: 562 pages
Edition : 2nd
Language : English
ISBN-13 : 9781786466921
Vendor :
VMware
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Feb 23, 2017
Length: 562 pages
Edition : 2nd
Language : English
ISBN-13 : 9781786466921
Vendor :
VMware
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 170.97
Learning PowerCLI
$54.99
Mastering PowerCLI
$60.99
Mastering VMware vSphere 6.5
$54.99
Total $ 170.97 Stars icon
Banner background image

Table of Contents

15 Chapters
1. Introduction to PowerCLI Chevron down icon Chevron up icon
2. Learning Basic PowerCLI Concepts Chevron down icon Chevron up icon
3. Working with Objects in PowerShell Chevron down icon Chevron up icon
4. Managing vSphere Hosts with PowerCLI Chevron down icon Chevron up icon
5. Managing Virtual Machines with PowerCLI Chevron down icon Chevron up icon
6. Managing Virtual Networks with PowerCLI Chevron down icon Chevron up icon
7. Managing Storage Chevron down icon Chevron up icon
8. Managing High Availability and Clustering Chevron down icon Chevron up icon
9. Managing vCenter Server Chevron down icon Chevron up icon
10. Patching ESXi Hosts and Upgrading Virtual Machines Chevron down icon Chevron up icon
11. Managing VMware vCloud Director and vCloud Air Chevron down icon Chevron up icon
12. Using Site Recovery Manager Chevron down icon Chevron up icon
13. Using vRealize Operations Manager Chevron down icon Chevron up icon
14. Using REST API to manage NSX and vRealize Automation Chevron down icon Chevron up icon
15. Reporting with PowerCLI Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Full star icon 5
(1 Ratings)
5 star 100%
4 star 0%
3 star 0%
2 star 0%
1 star 0%
Kim Bottu Feb 28, 2017
Full star icon Full star icon Full star icon Full star icon Full star icon 5
If you are new to PowerCLI and you have no idea how to start learning it, a technical book for beginners such as this one will be very welcome.I remember that when I started looking into PowerCLI, I devoured articles on the internet and books such as this one. The combination of both resources thaught me the basics I still use and welcome every day.What this book does not do is teach you litterally everything about PowerCLI and how to use it. However it will get you started on PowerCLI using clear examples and exersizes. What better way than learning a new skillset than by actually putting it to use?Kim, technical reviewer for Learning PowerCLI 2nd edition.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.