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

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.