VMware vSphere PowerCLI -All About-

VMware Vsphere PowerCLI-

What is PowerCLI?
PowerCLI is a Windows PowerShell interface for managing VMware vSphere. VMware describes PowerCLI as “a powerful command-line tool that lets you automate all aspects of vSphere management, including network, storage, VM, guest OS.

PowerCli in context

PowerCLI in context

*Reference
http://thecrazyconsultant.com/powercli-study-guide-core-concepts/
The above diagram is extracted from “PowerCLI study guide from rookie to guru
PowerCLI could be considered as an “extension” from PowerShell and PowerShell is built on top of “.Net”
This is why you need to learn PowerShell and “.net” in order to make the most out of PowerCLI

What is new in VMware Vsphere PowerCLI 6.5

The PowerCLI Storage module has been a big focus on this release. A lot of functionality has been added around vSAN, VVOLs, and the handling of virtual disks. The vSAN cmdlets have been bolstered to more than a dozen cmdlets which are focused on the entire lifecycle of a vSAN cluster. The entire vSAN cluster creation process can be automated with PowerCLI as well as running tests, updating the HCL database, and much more!

  • Get-VsanClusterConfiguration
  • Get-VsanDisk
  • Get-VsanDiskGroup
  • Get-VsanFaultDomain
  • Get-VsanResyncingComponent
  • Get-VsanSpaceUsage
  • New-VsanDisk
  • New-VsanDiskGroup
  • New-VsanFaultDomain
  • Remove-VsanDisk
  • Remove-VsanDiskGroup
  • Remove-VsanFaultDomain
  • Set-VsanClusterConfiguration
  • Set-VsanFaultDomain
  • Test-VsanClusterHealth
  • Test-VsanNetworkPerformance
  • Test-VsanStoragePerformance
  • Test-VsanVMCreation
  • Update-VsanHclDatabase

vSphere 6.5 introduces a new way to handle the management of virtual disks. Instead of managing a VM’s hard disks through the VM, they can now be managed independently with new PowerCLI cmdlets. This allows the handling of a virtual disk’s lifecycle to be decoupled from the lifecycle of a VM. This adds a ton of flexibility!

  • Copy-VDisk
  • Get-VDisk
  • Move-VDisk
  • New-VDisk
  • Remove-VDisk
  • Set-VDisk

PowerCLI has been updated to include new cmdlets that allow for the automated management of the VVOL replication features which are new to vSphere 6.5. Some of the new features include finding fault domains and replication groups, syncing replication groups, as well as preparing for and starting the failover process to the target site.

  • Get-SpbmFaultDomain
  • Get-SpbmReplicationGroup
  • Get-SpbmReplicationPair
  • Start-SpbmReplicationFailover
  • Start-SpbmReplicationPrepareFailover
  • Sync-SpbmReplicationGroup

VMware vSphere PowerCLI Install Guide.

VMware provides a single installer for VMware vSphere PowerCLI.

Download PowerCLI –
https://www.vmware.com/support/developer/PowerCLI/

To install VMware vSphere PowerCLI components

  1. Download VMware vSphere PowerCLI
  2. Navigate to the local folder that contains the PowerCLI installer file you downloaded and double-click the executable file.

powercli60

  1. On the Welcome page, click Next.

powercli60a

  1. On the VMware Patents page click Next.
  1. Accept the license agreement terms and click Next.
  2. On the Custom Setup page, select the PowerCLI components you want to install.

powercli60b

  1. If you want to change the default location to install VMware vSphere PowerCLI click Change and select a different Destination Folder. After you are done click Next.
  2. On the Ready to Install the Program page, click Install to proceed with the installation.
  3. Click Finish to complete the installation process.

powercli60c

powercli60d

I will be using theVMware vSphere PowerCLI Reference -Automating vSphere Administration
http://www.sybex.com/go/vmwarevspherepowercliref to help provide more indept overview the capabilities PowerCLI and integration Powershell CMDLET calls to initate specific tasks and jobs make VMware Admin job easier.

  1. Install, Configure and Manage The vSphere Environment.

I. Automating vCenter Server Deployment and Configuration.

We all know one key piece in automation is make repeatable tasks and routines done manually ease of System Administration.  For example, creating an automated Vcenter Server installation. i.e doing it manually you have download the bits and run through the series of wizards of configuration. A very time consuming tasks.

Getting Started with PowerCLI

To discover what you can do, you can use the Get-VICommand. Running Get-PowerCLIHelp will launch a CHM based help window.

VMware vSphere PowerCLI: getting started

Although once you know some commands you can also use PowerShell help to view documentation.

VMware vSphere PowerCLI

What I especially like is that VMware offers online help as well.

You could do all of your PowerCLI work in this session, but if you are like me, you’ve already configured a PowerShell session and console, so why not simply add the PowerCLI bits? Type Exit to quit this session, and let’s add the bits ourselves.

This is quite easy. All of the PowerCLI commands are packaged as snap-ins, as opposed to modules. This means you have to run the install on any computer on which you want to use them. You can see all of the registered snap-ins in your session:

Before you can use any PowerCLI commands, you need to add the snap-in to your session. To get started, all we need is the core snapin.

You can also use wildcards.

Once loaded, I can use Get-Command to view the commands.

That’s all the Get-VICommand tool is really doing. As you can see, there are many commands at our disposal.

 

There are commands are packaged as snap-ins, you don’t get autoloading of commands like you may be used to. PowerShell will automatically import modules when you use one of the commands, but this feature doesn’t apply to snap-ins.

Also, if you happen to be running, or have installed, the Hyper-V module, you will run into naming collisions. For example, Microsoft and VMware have a Get-VM cmdlet. If you run Get-VM you most likely will get the Hyper-V cmdlet. Unless you’ve added the VMware snap-ins, in which case you’ll get the PowerCLI version. There are a few ways to mitigate.

First, you can preface the command with the module name.

Or, import the Hyper-V module and use a prefix.

Now, all of the Hyper-V commands will have a MS prefix, eliminating the collision.

Because PowerCLI does not require PowerShell 3.0, but the Hyper-V cmdlets do, I tend to run PowerCLI in separate session running PowerShell 2.0. Create a shortcut for this command:

The window title is optional. Configure the shortcut with whatever size and colors you’d like. But now I have a PowerShell window more to my liking and I don’t have to worry about name collisions. If you don’t have to worry, then you can ignore all of this and simply add the snap-ins when you need them.

For now, I encourage you to download and install the latest PowerCLI package.

Extract information about ESXi hosts

Extract the following information about all ESXI hosts connected to vcenter and export it as a CSV file.
Name
State
Model
Version
Build

Identify in the official documentation the most relevant PowerCLI cmdlet.
PowerCLI is using PowerShell approved verb, so you should search within the cmdlet starting by Get
1
Get-vmhost | select name,state,model,version,build| export-csv

Goal is to find the right PowerCLI cmdlet

Here are great Video and Sample Automation Scripts

  • PowerCLI Intro Video-From Pluralsight
  • Sample script for an automated Installation of vCenter Server:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
## =====================================================================
## Title       : vCenter51_Install
## Description : This script will install all the necessary components
##               needed for vCenter 5.1
## Author      : Maish Saidel-Keesing
## Date        : 19/02/2013
## Usage       : PS>  .\vcenter51_install.ps1
## Notes       : More information about this script can be found at
##               http://technodrone.blogspot.com
## Version     : 1.0.1
## =====================================================================
## Start Logging
$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"
$transcriptpath = ".\vCenter_install_" + (Get-Date -Format dd-MM-yyyy_HH-mm) + ".log"
Start-Transcript -path $transcriptpath -append | Out-Null
#Update Hosts file
$vcenterdb = Read-Host "Please Enter the IP address of the vCenterDB (Oracle) Server"
$hostsfile = "C:\Windows\System32\drivers\etc\hosts"
if (!$((Get-Content $hostsfile) | Select-String "vcenterdb")) {
    Write-host "Adding vcenterDB to the hosts file..." -ForegroundColor Green
    Add-Content "`r`n`n$vcenterdb`tvcenterdb" -Path $hostsfile
    if (((Get-Content $hostsfile) | Select-String "vcenterdb") -eq $null) {
        Write-Warning "Hosts file was not updated correctly!! Exiting.."
        break
    }
}
if (!$(Test-Path -Path 'c:\temp')) {
    New-Item -ItemType Directory -Path 'c:\temp' | Out-Null
}
#Define Parameters
$VCMedia = "C:\installs\VMware-VIMSetup-all-5.1.0-947939"
$LIKey = ""
$Username = "Maish"
$CompanyName = "maish"
$ODBCName = "vCenter"
$DBUser = "vpxadmin"
$DBPass = "vpxadmin"
$SSOpasswd = "Hello!2"
$RSA_USER = "RSA_USER"
$RSA_DBA = "RSA_DBA"
$SSO_ADMIN_USER = "admin@System-Domain"
$wipedb = "FORMAT_DB=1"
$vcenterIP = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName .).IPAddress
#SSO installation
Write-Host "Installing Single Sign On" -ForegroundColor Green
$exe = "$VCmedia\Single Sign On\VMware-SSO-Server.exe"
$myargs = '/L1033 /v"/qr MASTER_PASSWORD=' + $SSOpasswd + ' RSA_DBA_PASSWORD=' + $RSA_DBA + ' RSA_USER_PASSWORD=' + $RSA_USER
$myargs = $myargs + ' CONFIG_TYPE=Setup SETUP_TYPE=Basic SSO_DB_SERVER_TYPE=Custom JDBC_DBTYPE=Oracle'
$myargs = $myargs + ' JDBC_DBNAME=VCENTER JDBC_HOST_PORT=1540 JDBC_HOSTNAME_OR_IP=vcenterdb ORACLE_SERVICE_OR_SID=VCENTER'
$myargs = $myargs + ' SKIP_DB_USER_CREATION=1 DBA_JDBC_USERNAME=' + $RSA_DBA + ' DBA_JDBC_PASSWORD=' + $RSA_DBA
$myargs = $myargs + ' JDBC_USERNAME=' + $RSA_USER + ' JDBC_PASSWORD=' + $RSA_USER + ' COMPUTER_FQDN=\"' + $vcenterIP
$myargs = $myargs + '\" IS_SSPI_NETWORK_SERVICE_ACCOUNT=1 SSO_HTTPS_PORT=7444 /L*v \"C:\temp\ssoinstall.log\""'
Start-process $exe $myargs -Wait
#Inventory Service installation
Write-Host "Installing Inventory Service" -ForegroundColor Green
$exe = "$VCmedia\Inventory Service\VMware-inventory-service.exe"
$myargs = '/L1033 /v"/qr QUERY_SERVICE_NUKE_DATABASE=1 SSO_ADMIN_USER=\"' + $SSO_ADMIN_USER + '\" SSO_ADMIN_PASSWORD=\"' + $SSOpasswd + '\"'
$myargs = $myargs + ' LS_URL=\"https://' + $vcenterIP + ':7444/lookupservice/sdk\" HTTPS_PORT=10443 FEDERATION_PORT=10111 XDB_PORT=10109'
$myargs = $myargs + ' TOMCAT_MAX_MEMORY_OPTION=S /L*v \"C:\temp\inventoryservice_install.log\""'
Start-process $exe $myargs -Wait
# Install vCenter
Write-Host "Installing vCenter Server" -ForegroundColor Green
$exe = "$VCmedia\vCenter-Server\VMware-vcserver.exe"
$myargs = '/L1033 /v" /qr DB_SERVER_TYPE=Custom DB_DSN=\"' + $ODBCName + '\"  DB_USERNAME=\"' + $DBUser +'\" DB_PASSWORD=\"' + $DBPass + '\" ' + $wipedb
$myargs = $myargs + ' JVM_MEMORY_OPTION=S SSO_ADMIN_USER=\"' + $SSO_ADMIN_USER + '\" SSO_ADMIN_PASSWORD=\"' + $SSOpasswd + '\"'
$myargs = $myargs + ' LS_URL=\"https://' + $vcenterIP + ':7444/lookupservice/sdk\" IS_URL=\"https://' + $vcenterIP + ':10443\"'
$myargs = $myargs + ' VC_JDBC_URL=\"jdbc:oracle:thin:@vcenterdb:1540:VCENTER\" VPX_USES_SYSTEM_ACCOUNT=1 COMPUTER_FQDN=\"' + $vcenterIP + '\"'
$myargs = $myargs + ' VC_ADMIN_USER=\"Administrators\" VC_ADMIN_IS_GROUP_VPXD_TXT=true VCS_GROUP_TYPE=Single VCS_ADAM_LDAP_PORT=389'
$myargs = $myargs + ' VCS_ADAM_SSL_PORT=636 VCS_HTTPS_PORT=443 VCS_HTTP_PORT=80 TC_HTTP_PORT=8080 TC_HTTPS_PORT=8443 VCS_WSCNS_PORT=60099'
$myargs = $myargs + ' VCS_HEARTBEAT_PORT=902 /L*v \"C:\temp\vcenter_install.log\""'
Start-process $exe $myargs -Wait
#Install Web Client
Write-Host "Installing Web Client" -ForegroundColor Green
$exe = "$VCmedia\vSphere-WebClient\VMware-WebClient.exe"
$myargs = '/L1033 /v" /qr SSO_ADMIN_USER=\"' + $SSO_ADMIN_USER + '\" SSO_ADMIN_PASSWORD=\"' + $SSOpasswd + '\"'
$myargs = $myargs + ' LS_URL=\"https://' + $vcenterIP + ':7444/lookupservice/sdk\" HTTP_PORT=9090 HTTPS_PORT=9443'
$myargs = $myargs + ' /L*v \"C:\temp\weblient_install.log\""'
Start-process $exe $myargs -Wait
# Stop logging
Stop-Transcript
  1.  Create Vcenter Folder Structure.

Let say you want to create a folder structure from scratch  using PowerCli  , Below example,  is powercli method to folder structure using comma-separated variable (CSV) file.

Figure 1.1- Sample CSV layout
samplecsvlayout

in the example csv file created. There are two colunms, the first colunm, Name, is used to define the name of the folder you wish to create. The second column, Path , isuse dto show the path to where this folder is to be created in Vcenter.

As see in Figure 1.1 in th epath column all entries begin with vm\. This folder will API. Once you have created the CVS folder that contains the layout of the your folder structure, a script can easily read your CSV folder and create the structure using the code shown in listing 1.2.
Using a CSV file to create a Vcenter file structure.

I have two vCenter VC1 and VC2 and many VM’s  with same names are present in both the VC,

1. Export the complete folder structure with VM details of a datacenter from VC1 to a outputfile.

2. Then in VC2 i need to create only a particular folder with all its its sub-folders from the output file generated from VC1. This folder will be a root folder which is directly under datacenter in VC1.

3. After creating the particular folder structure, the script should read the vm’s that was present in that folder & subfolder from output file and then it should search the same VM’s in VC2 and place them into the folders identically.

Export / Import selective Folder structure:

The folder1 structure hs been taken from VC1 and has been created in VC2 with all the subfolders and placing the same VM’s in the folders.

 

VC1

|—–Datacenter1

|————Folder1

|——-SubFolder1

|—-VM1

|—-VM2

|——-Subfolder2

|—–VM3

|—–VM4

|————-Folder2

|——–Subfolder3

|——–Subfolder4

VC2

|—–Datacenter1

|————Folder1

|——-SubFolder1

|—-VM1

|—-VM2

|——-Subfolder2

|—–VM3

|—–VM4

Export the VMs and the folder  structure.

 

New-VIProperty -Name 'BlueFolderPath' -ObjectType 'VirtualMachine' -Value {
    param($vm)

    function Get-ParentName{
        param($object)

        if($object.Folder){
            $blue = Get-ParentName $object.Folder
            $name = $object.Folder.Name
        }
        elseif($object.Parent -and $object.Parent.GetType().Name -like "Folder*"){
            $blue = Get-ParentName $object.Parent
            $name = $object.Parent.Name
        }
        elseif($object.ParentFolder){
            $blue = Get-ParentName $object.ParentFolder
            $name = $object.ParentFolder.Name
        }
        if("vm","Datacenters" -notcontains $name){
            $blue + "/" + $name
        }
        else{
            $blue
        }
    }

    (Get-ParentName $vm).Remove(0,1)
} -Force | Out-Null 
$dcName = "MyDC"

Get-VM -Location (Get-Datacenter -Name $dcName) | 
Select Name,BlueFolderPath |
Export-Csv "C:\vm-folder.csv" -NoTypeInformation -UseCulture

 

The script uses a New-VIProperty to fetch the blue folderpath for a VM.

 

Import the folder structure and move existing VMs.

 

$newDatacenter = "MyNewDC" 
$newFolder = "Folder1" 

$startFolder = New-Folder -Name $newFolder -Location (Get-Folder -Name vm -Location (Get-Datacenter -Name $newDatacenter))

Import-Csv "C:\vm-folder.csv" -UseCulture | %{
    $location = $startFolder
    $_.BlueFolderPath.TrimStart('/').Split('/') | %{
        $tgtFolder = Get-Folder -Name $_ -Location $location -ErrorAction SilentlyContinue
        if(!$tgtFolder){
            $location = New-Folder -Name $_ -Location $location
        }
        else{
            $location = $tgtFolder
        }
    }
    
    $vm = Get-VM -Name $_.Name -ErrorAction SilentlyContinue
    if($vm){
        Move-VM -VM $vm -Destination $location -Confirm:$false 
    }
}

 

The complete folder structure that was exported will now be imported in datacenter MyNewDC under the folder Folder1.

Sample Code -Export/Import  VM  

Export VMs
The script is cluster-based. Use the script multiple times if you have more than one cluster. You have to replace “MyCluster” with your clusters name. The Script will export information about all VMs to the $inventory array, and additionally to a csv file. You can always use the csv file load your inventory back to the $inventory array. The script exports:

  • VM-Name
  • VMX Path
  • Resource Pool
  • VM Folder
$cluster = Get-Cluster MyCluster

$inventory = Get-Cluster $cluster | Get-VM | 
Add-Member -MemberType ScriptProperty -Name 'VMXPath' -Value {$this.extensiondata.config.files.vmpathname} -Passthru -Force | 
Select-Object Name,ResourcePool,Folder,VMXPath

$inventory |Export-Csv inventory.csv

Import VMs
To add virtual machines back to the inventory, connect to the vCenter server where you want to add the VMs. You have to create the cluster and any resource pools or VM-Folders prior to use the script. The structure has to be identical to the vCenter where the VMs have been exported from. You also have to mount the original datastores.

(optional) Load the csv file back to the $inventory array:

$inventory = Import-Csv inventory.csv

Add VMs back to the inventory:

$cluster = Get-Cluster MyCluster

foreach($vm in $inventory){
  $ESXHost = Get-Cluster $cluster | Get-VMHost | select -First 1
  New-VM -VMFilePath $vm.VMXPath -VMHost $ESXHost -Location $vm.Folder -ResourcePool (Get-Cluster $cluster | Get-ResourcePool $vm.ResourcePool)
}
 Define Users and Their Privileges 
The authorization to perform task in your virutal infrastructure is controlled by a role-base access control(RBAC) system. A vCenter Server administrator can specify in great  detail which user or groups can perform which tasks on which objects.

RBAC system are define using 3 key concepts:

Privilege:  a privilege is the ability to perform an action or read a property

Role: a role is collection of privileges. Roles provide a way to add all the individual privilleges that are required.to perform a number of tasks. such as administrator a vsphere host.

Object: an object is an item on which actions can be performed vCenter server objects are datacentes, folders, resource pools, clusters, hosts, and VMs.
 Granting Privileges 

Reference

Reference 
Downloadable  PowerCLI-{powershell) modules and resources.
For example, PowerCLI book series-The  author is Jonathan to provide an introduction to PowerCLI modules – http://www.simple-talk.com/sysadmin/powershell/an-introduction-to-powershell-modules/.

Here is the Download link to the PowerCLIBook PowerShell module here – PowerCLIBook Module

Please note you need can unblock the zip file to prevent a Security Warning when importing the module. (See these two links for more details on why this is necessary, Scott Hansleman and Hey Scripting guy)

  • Extract the zip file and copy the PowerCLIBook folder containing the 4 files that make up the module to your PowerShell modules file location, typically this will be C:\Users\username\Documents\WindowsPowerShell\Modules

 

The module is made up of four files:

  • PowerCLIBook.psd1 – The PowerShell module manifest file, which describes the contents of the module and controls how the module is processed.
  • PowerCLIBook.psm1 – The PowerShell module file containing the functions which make up the PowerCLIBook module.
  • DistributedSwitch.psm1 – The PowerShell module file containing the functions which make up the DistributedSwitch nested module, if you like a sub-module of the PowerCLIBook module.
  • Test-Version.ps1 – A script which is run prior to the PowerCLIBook module being called which checks to see if PowerCLI 4.1 or newer is installed.

 
What is New VMware Vspere PowerCLI 6.5
http://blogs.vmware.com/PowerCLI/2016/11/new-release-powercli-6-5-r1.html

Installing VMware vSphere PowerCLI 6.0
https://www.vmware.com/support/developer/PowerCLI/PowerCLI60R1/powercli60r1-releasenotes.html

VMware vSphere PowerCLI Reference -Automating vSphere Administration
https://www.vmware.com/support/developer/windowstoolkit/wintk40u1/doc/viwin_admg.pdf

PowerCLI Script Sample Download
http://www.powerclibook.com/

VMware vSphere PowerCLI Reference -Automating vSphere Administration
http://www.sybex.com/go/vmwarevspherepowercliref .

Automate Installation of vCenter Server.
http://technodrone.blogspot.com/2013/02/vcenter-automated-install.html