Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning PowerCLI

You're reading from   Learning PowerCLI A comprehensive guide on PowerCLI

Arrow left icon
Product type Paperback
Published in Feb 2017
Publisher
ISBN-13 9781786468017
Length 562 pages
Edition 2nd Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Robert van den Nieuwendijk Robert van den Nieuwendijk
Author Profile Icon Robert van den Nieuwendijk
Robert van den Nieuwendijk
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

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

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.

You have been reading a chapter from
Learning PowerCLI - Second Edition
Published in: Feb 2017
Publisher:
ISBN-13: 9781786468017
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime