Learning PowerCLI - Second Edition

Book description

Learn to leverage the power of PowerCLI to automate your VMware vSphere environment with ease

About This Book

  • 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

  • Who This Book Is 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

  • In Detail

    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.

    Style and approach

    This comprehensive book will teach system administrators everything about PowerCLI 6 and how to utilize it to automate VMware vSphere.

    Table of contents

    1. Learning PowerCLI Second Edition
      1. Learning PowerCLI Second Edition
      2. Credits
      3. About the Author
      4. About the Reviewer
      5. www.PacktPub.com
        1. Why subscribe?
      6. Customer Feedback
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Introduction to PowerCLI
        1. Downloading and installing PowerCLI
          1. Requirements for using PowerCLI 6.5 Release 1
          2. Downloading PowerCLI 6.5 Release 1
          3. Installing PowerCLI
        2. Participating in the VMware Customer Improvement Program
        3. Modifying the PowerShell execution policy
        4. Creating a PowerShell profile
        5. Connecting and disconnecting servers
          1. Connecting to a server
          2. Connecting to multiple servers
          3. Suppressing certificate warnings
          4. Disconnecting from a server
          5. Retrieving the PowerCLI configuration
        6. Using the credential store
        7. Retrieving a list of all of your virtual machines
          1. Suppressing deprecated warnings
          2. Using wildcard characters
          3. Filtering objects
          4. Using comparison operators
          5. Using aliases
        8. Retrieving a list of all of your hosts
          1. Displaying the output in a grid view
        9. Summary
      9. 2. Learning Basic PowerCLI Concepts
        1. Using the Get-Command, Get-Help, and Get-Member cmdlets
          1. Using Get-Command
          2. Using Get-VICommand
          3. Using Get-Help
          4. Using Get-PowerCLIHelp
          5. Using Get-PowerCLICommunity
          6. Using Get-Member
        2. Using providers and PSDrives
          1. Using providers
          2. Using PSDrives
          3. Using the PowerCLI Inventory Provider
          4. Using the PowerCLI Datastore Provider
          5. Copying files between a datastore and your PC
        3. Using arrays and hash tables
        4. Creating calculated properties
        5. Using raw API objects with ExtensionData or Get-View
          1. Using the ExtensionData property
          2. Using the Get-View cmdlet
          3. Using managed object references
          4. Using the Get-VIObjectByVIView cmdlet
        6. Extending PowerCLI objects with the New-VIProperty cmdlet
        7. Working with vSphere folders
        8. Summary
      10. 3. Working with Objects in PowerShell
        1. Using objects, properties, and methods
          1. Using methods
        2. Expanding variables and subexpressions in strings
          1. When will a string be expanded?
          2. Expanding a string when it is used
        3. Using here-strings
        4. Using the pipeline
          1. Using the ByValue parameter binding
          2. Using the ByPropertyName parameter binding
        5. Using the PowerShell object cmdlets
          1. Using the Select-Object cmdlet
          2. Using the Where-Object cmdlet
          3. Using the ForEach-Object cmdlet
          4. Using the Sort-Object cmdlet
          5. Using the Measure-Object cmdlet
            1. Rounding a value
          6. Using the Group-Object cmdlet
          7. Using the Compare-Object cmdlet
          8. Using the Tee-Object cmdlet
        6. Creating your own objects
          1. Using the New-Object cmdlet
          2. Using a hash table to create an object
          3. Creating objects using the Select-Object cmdlet
          4. Creating objects using [pscustomobject]
          5. Adding properties to an object with Add-Member
        7. Using COM objects
        8. Summary
      11. 4. Managing vSphere Hosts with PowerCLI
        1. Adding hosts to a VMware vCenter Server
          1. Creating a data center
          2. Creating a cluster
          3. Adding a host
        2. Enabling and disabling maintenance mode
        3. Working with host profiles
          1. Creating a host profile
          2. Attaching the host profile to a cluster or a host
          3. Testing the host profile for compliance
          4. Applying a host profile to a host or cluster
          5. Using host profile answer files
          6. Exporting a host profile
          7. Importing a host profile
        4. Working with host services
          1. Retrieving information about host services
          2. Starting a host service
          3. Stopping a host service
          4. Restarting a host service
          5. Modifying the startup policy of a host service
        5. Configuring the host firewall
          1. Getting the host firewall default policy
          2. Modifying the host firewall default policy
          3. Getting the host firewall exceptions
          4. Modifying a host firewall exception
        6. Using vSphere Image Builder and Auto Deploy
          1. Using Image Builder
            1. Adding ESXi software depots to your PowerCLI session
            2. Retrieving the ESXi software depots added to your PowerCLI session
            3. Retrieving the image profiles in your PowerCLI session
            4. Creating image profiles
            5. Retrieving VIB objects from all of the connected depots
            6. Adding VIBs to an image profile or updating existing VIBs
            7. Exporting an image profile to an ISO or ZIP file
          2. Configuring Auto Deploy
            1. Creating deploy rules
            2. Adding deploy rules to a ruleset
            3. Retrieving deploy rulesets
            4. Adding host profiles to a deploy ruleset
        7. Using esxcli from PowerCLI
        8. Removing hosts from a VMware vCenter Server
        9. Summary
      12. 5. Managing Virtual Machines with PowerCLI
        1. Creating virtual machines
          1. Creating virtual machines from scratch
          2. Creating virtual machines from templates
          3. Cloning virtual machines
        2. Registering virtual machines
        3. Using OS customization specifications
        4. Importing OVF or OVA packages
          1. Retrieving the required properties
          2. Assigning values to the required properties
          3. Importing the vMA OVF file
        5. Starting and stopping virtual machines
          1. Starting virtual machines
          2. Suspending virtual machines
          3. Shutting down the virtual machine's guest operating systems
          4. Stopping virtual machines
        6. Modifying the settings of virtual machines
          1. Using the VMware vSphere API to modify virtual machine settings
          2. Adding devices to a virtual machine
            1. Adding a hard disk
            2. Adding a SCSI controller
            3. Adding a network adapter
            4. Adding a floppy drive
            5. Adding a CD drive
          3. Modifying devices added to a virtual machine
            1. Modifying a hard disk
              1. Moving a hard disk to another datastore
            2. Modifying a SCSI controller
            3. Modifying a network adapter
            4. Modifying a floppy drive
            5. Modifying a CD drive
          4. Removing devices from a virtual machine
            1. Removing a hard disk
            2. Removing a network adapter
            3. Removing a floppy drive
            4. Removing a CD drive
        7. Converting virtual machines into templates
          1. Converting templates into virtual machines
          2. Modifying the name of a template
          3. Removing templates
        8. Moving virtual machines to another folder, host, cluster, resource pool, or datastore
        9. Updating VMware Tools
          1. Using the Update-Tools cmdlet
          2. Enabling the Check and upgrade VMware Tools before each power on checkbox
        10. Upgrading virtual machine compatibility
        11. Using snapshots
          1. Creating snapshots
          2. Retrieving snapshots
          3. Reverting to a snapshot
          4. Modifying snapshots
          5. Removing snapshots
        12. Running commands in the guest OS
        13. Configuring Fault Tolerance
          1. Turning Fault Tolerance on
          2. Turning Fault Tolerance off
        14. Opening the console of virtual machines
        15. Removing virtual machines
        16. Using tags
          1. Managing tag categories
            1. Creating tag categories
            2. Retrieving tag categories
            3. Modifying tag categories
            4. Removing tag categories
          2. Managing tags
            1. Creating tags
            2. Retrieving tags
            3. Modifying tags
            4. Removing tags
          3. Managing tag assignments
            1. Creating tag assignments
            2. Retrieving tag assignments
            3. Retrieving virtual machines by tag
            4. Removing tag assignments
          4. Converting custom attributes and annotations to tags
            1. Creating tag categories from custom attributes
            2. Creating tags from annotations
        17. Summary
      13. 6. Managing Virtual Networks with PowerCLI
        1. Using vSphere Standard Switches
          1. Creating vSphere Standard Switches
          2. Configuring vSphere Standard Switches
          3. Adding network adapters to a switch
          4. Removing vSphere Standard Switches
        2. Using host network adapters
          1. Creating host network adapters
          2. Retrieving host network adapters
          3. Configuring host network adapters
            1. Configuring network speed and duplex setting
            2. Configuring the management network
            3. Configuring vMotion
          4. Removing host network adapters
          5. Configuring NIC teaming
        3. Using standard port groups
          1. Creating standard port groups
          2. Configuring standard port groups
          3. Removing standard port groups
        4. Using vSphere Distributed Switches
          1. Creating vSphere Distributed Switches
            1. Creating a new vSphere Distributed Switch from scratch
            2. Cloning a vSphere Distributed Switch
            3. Creating a vSphere Distributed Switch from an export
          2. Retrieving vSphere Distributed Switches
          3. Configuring vSphere Distributed Switches
            1. Rolling back the configuration of a vSphere Distributed Switch
            2. Importing the configuration of a vSphere Distributed Switch from a backup
            3. Upgrading a vSphere Distributed Switch
          4. Adding hosts to vSphere Distributed Switches
          5. Retrieving hosts connected to vSphere Distributed Switches
          6. Adding host physical network adapters to a vSphere Distributed Switch
          7. Removing host physical network adapters from a vSphere Distributed Switch
          8. Removing hosts from a vSphere Distributed Switch
          9. Exporting the configuration of vSphere Distributed Switches
          10. Removing vSphere Distributed Switches
        5. Using distributed virtual port groups
          1. Creating distributed virtual port groups
            1. Creating distributed virtual port groups from a reference group
            2. Creating distributed virtual port groups from an export
          2. Retrieving distributed virtual port groups
          3. Modifying distributed virtual port groups
            1. Renaming a distributed virtual port group
            2. Rolling back the configuration of a distributed virtual port group
            3. Restoring the configuration of a distributed virtual port group
          4. Configuring network I/O control
            1. Enabling network I/O control
            2. Retrieving the network I/O control enabled status
            3. Disabling network I/O control
          5. Exporting the configuration of distributed virtual port groups
          6. Migrating a host network adapter from a standard port group to a distributed port group
          7. Removing distributed virtual port groups
        6. Configuring host networking
        7. Configuring the network of virtual machines
          1. Setting the IP address
          2. Setting the DNS server addresses
          3. Retrieving the network configurations
        8. Summary
      14. 7. Managing Storage
        1. Rescanning for new storage devices
        2. Creating datastores
          1. Creating NFS datastores
          2. Getting SCSI LUNs
          3. Creating VMFS datastores
            1. Creating software iSCSI VMFS datastores
        3. Retrieving datastores
        4. Setting the multipathing policy
        5. Configuring vmhba paths to an SCSI device
          1. Retrieving vmhba paths to an SCSI device
          2. Modifying vmhba paths to an SCSI device
        6. Working with Raw Device Mappings
        7. Configuring storage I/O control
          1. Retrieving Storage I/O Control settings
        8. Configuring Storage DRS
          1. Creating a datastore cluster
          2. Retrieving datastore clusters
          3. Modifying datastore clusters
          4. Adding datastores to a datastore cluster
          5. Retrieving the datastores in a datastore cluster
          6. Removing datastores from a datastore cluster
          7. Removing datastore clusters
        9. Upgrading datastores to VMFS-5
        10. Removing datastores
        11. Using VMware vSAN
          1. Configuring VMware vSAN networking
          2. Enabling VMware vSAN on vSphere clusters
          3. Retrieving the devices available for VMware vSAN
          4. Creating VMware vSAN disk groups
          5. Retrieving VMware vSAN disk groups
          6. Adding a host SCSI disk to a VMware vSAN disk group
          7. Retrieving the host disks that belong to a VMware vSAN disk group
          8. Removing disks from a VMware vSAN disk group
          9. Removing VMware vSAN disk groups
        12. Using vSphere storage policy-based management
          1. Retrieving storage capabilities
          2. Using tags to define storage capabilities
          3. Creating SPBM rules
          4. Creating SPBM rule sets
          5. Creating SPBM storage policies
          6. Retrieving SPBM storage policies
          7. Modifying SPBM storage policies
          8. Retrieving SPBM compatible storage
          9. Using SPBM to create virtual machines
          10. Retrieving SPBM-related configuration data of clusters, virtual machines, and hard disks
          11. Associating storage policies with virtual machines and hard disks and enabling SPBM on clusters
          12. Exporting SPBM storage policies
          13. Importing SPBM storage policies
          14. Removing SPBM storage policies
        13. Summary
      15. 8. Managing High Availability and Clustering
        1. Creating vSphere HA and DRS clusters
        2. Retrieving clusters
          1. Retrieving the HA master or primary hosts
          2. Retrieving cluster configuration issues
        3. Modifying the cluster settings
          1. Configuring enhanced vMotion compatibility (EVC) mode
          2. Disabling HA
          3. Disabling or enabling host monitoring
          4. Enabling VM and application monitoring
          5. Configuring the heartbeat datastore selection policy
        4. Moving hosts to clusters
        5. Moving clusters
        6. Using DRS rules
          1. Creating VM-VM DRS rules
          2. Creating VM-host DRS rules
            1. Creating virtual machines DRS groups
            2. Creating hosts DRS groups
            3. Retrieving DRS groups
            4. Modifying DRS groups
              1. Adding virtual machines to a DRS group
              2. Removing virtual machines from a DRS group
            5. Removing DRS groups
            6. Creating Virtual Machines to Hosts DRS rules
          3. Retrieving DRS Rules
          4. Modifying DRS rules
          5. Removing DRS rules
        7. Using DRS recommendations
        8. Using resource pools
          1. Creating resource pools
          2. Retrieving resource pools
          3. Modifying resource pools
          4. Moving resource pools
          5. Configuring resource allocation between virtual machines
          6. Removing resource pools
        9. Using Distributed Power Management
          1. Enabling DPM
          2. Configuring hosts for DPM
          3. Testing hosts for DPM
            1. Putting hosts into standby mode
            2. Starting hosts
          4. Retrieving the DPM configuration of a cluster
          5. Disabling DPM
        10. Removing clusters
        11. Summary
      16. 9. Managing vCenter Server
        1. Working with roles and permissions
          1. Retrieving privileges
          2. Using roles
            1. Creating roles
            2. Retrieving roles
            3. Modifying roles
            4. Removing roles
          3. Using permissions
            1. Creating permissions
            2. Retrieving permissions
            3. Modifying permissions
            4. Removing permissions
        2. Managing licenses
          1. Adding license keys to the license inventory
          2. Retrieving license keys from the license inventory
          3. Removing license keys from the license inventory
          4. Assigning licenses to hosts
          5. Retrieving assigned licenses
          6. Using the LicenseDataManager
            1. Associating license keys with host containers
            2. Applying the associated license key to all hosts
            3. Retrieving license key associations
              1. Retrieving all of the license key associations to the host containers in your environment
              2. Retrieving the license keys associated with a specific host container
              3. Retrieving the effective license key of a host container
            4. Modifying license key associations
            5. Removing license key associations
        3. Configuring alarms
          1. Retrieving alarm definitions
          2. Modifying alarm definitions
          3. Creating alarm actions
            1. Configuring the vCenter Server mail server and sender settings
          4. Retrieving alarm actions
          5. Removing alarm actions
          6. Creating alarm action triggers
          7. Retrieving alarm action triggers
          8. Removing alarm action triggers
        4. Retrieving events
        5. Summary
      17. 10. Patching ESXi Hosts and Upgrading Virtual Machines
        1. Downloading new patches into the Update Manager repository
        2. Retrieving patches in the Update Manager repository
        3. Using baselines and baseline groups
          1. Retrieving baselines
          2. Retrieving patch baselines
          3. Creating patch baselines
          4. Modifying patch baselines
          5. Attaching baselines to inventory objects
          6. Detaching baselines from inventory objects
          7. Removing baselines
        4. Testing inventory objects for compliance with baselines
        5. Retrieving baseline compliance data
        6. Initializing staging of patches
        7. Remediating inventory objects
          1. Upgrading or patching ESXi hosts
          2. Upgrading virtual machine hardware
        8. Summary
      18. 11. Managing VMware vCloud Director and vCloud Air
        1. Connecting to vCloud Air servers and vCloud Director servers
        2. Retrieving organizations
        3. Retrieving organization virtual datacenters
        4. Retrieving organization networks
        5. Retrieving vCloud users
        6. Using vCloud virtual appliances
          1. Retrieving vApp templates
          2. Creating vCloud vApps
          3. Retrieving vCloud vApps
          4. Starting vCloud vApps
          5. Stopping vCloud vApps
        7. Managing vCloud virtual machines
          1. Creating vCloud virtual machines
          2. Retrieving vCloud virtual machines
          3. Starting vCloud virtual machines
          4. Stopping vCloud virtual machines
        8. Using the vCloud Director API with Get-CIView
          1. Removing vCloud virtual machines
          2. Removing vCloud virtual appliances
          3. Creating snapshots
          4. Retrieving snapshots
          5. Reverting to snapshots
          6. Removing snapshots
        9. Disconnecting from vCloud Director servers
        10. Summary
      19. 12. Using Site Recovery Manager
        1. Installing SRM
        2. Connecting to SRM servers
        3. Downloading and installing the Meadowcroft.SRM module
        4. Pairing SRM sites
          1. Retrieving the name of the local vCenter Server
          2. Retrieving the remote vCenter Server
        5. Retrieving the SRM user info
        6. Managing protection groups
          1. Creating protection groups
          2. Retrieving protection groups
        7. Protecting virtual machines
        8. Retrieving protected virtual machines
        9. Unprotecting virtual machines
        10. Managing recovery plans
          1. Retrieving recovery plans
          2. Running recovery plans
          3. Retrieving the historical results of recovery plans
        11. Disconnecting from SRM servers
        12. Summary
      20. 13. Using vRealize Operations Manager
        1. Connecting to vRealize Operations Manager servers
        2. Retrieving vRealize Operations Manager resource objects
        3. Using alerts
          1. Retrieving alert definitions
          2. Retrieving alert types
          3. Retrieving alert subtypes
          4. Modifying alerts
        4. Retrieving recommendations
        5. Retrieving statistic keys
        6. Retrieving statistical data
        7. Retrieving local user accounts
        8. Using the vRealize Operations Manager API
          1. Getting the user roles
          2. Creating users
          3. Removing users
          4. Retrieving solutions
          5. Retrieving traversalSpecs
          6. Creating reports
          7. Retrieving reports
        9. Disconnecting from vRealize Operations Manager servers
        10. Summary
      21. 14. Using REST API to manage NSX and vRealize Automation
        1. Connecting to REST API servers
        2. Managing NSX logical switches
          1. Creating NSX logical switches
          2. Retrieving NSX logical switches
          3. Removing NSX logical switches
        3. Managing NSX logical (distributed) routers
          1. Creating NSX logical (distributed) routers
          2. Retrieving NSX logical (distributed) routers
          3. Removing NSX logical (distributed) routers
        4. Managing NSX Edge services gateways
          1. Retrieving NSX Edge services gateways
          2. Removing NSX Edge services gateways
        5. Connecting to vRA servers
        6. Managing vRA tenants
          1. Creating vRA tenants
          2. Retrieving vRA tenants
          3. Removing vRA tenants
        7. Retrieving vRA business groups
        8. Managing vRA reservations
          1. Creating vRA reservations
          2. Retrieving vRA reservations
        9. Managing vRA machines and applications
          1. Retrieving entitled catalog items
          2. Retrieving a template request for an entitled catalog item
          3. Creating vRA machines
          4. Viewing details of a machine request
          5. Retrieving provisioned resources
        10. Summary
      22. 15. Reporting with PowerCLI
        1. Retrieving log files
        2. Creating log bundles
        3. Performance reporting
          1. Retrieving the statistical intervals
          2. Retrieving performance statistics
          3. Retrieving metric IDs
        4. Exporting reports to CSV files
        5. Generating HTML reports
        6. Sending reports by e-mail
        7. Reporting the health of your vSphere environment with vCheck
        8. Summary

    Product information

    • Title: Learning PowerCLI - Second Edition
    • Author(s): Robert van den Nieuwendijk
    • Release date: February 2017
    • Publisher(s): Packt Publishing
    • ISBN: 9781786468017