Skip to content

PowerCLI

Johnny Foulds edited this page Aug 14, 2019 · 30 revisions

To install VMWare PowerCLI with PowerShellGet, run the PowerShell terminal as administrator and execute the following command:

Install-Module -Name VMware.PowerCLI

If NuGet is not installed you will be prompted to do so before the installation continues.

Please be aware that many of the commands will not work unless you have deployed a VMware vCenter Server Appliance

Test PowerCLI

You can test if PowerCLI can connect to your vSphere server by executing the following script:

$EsxiHostName = '192.168.3.100'
$EsxiUser = 'root'
$EsxiPassword = '[your_password]'

Connect-VIServer -Verbose:$true -Server $EsxiHostName -User $EsxiUser -Password $EsxiPassword

If you get a warning that server certificate is invalid (when using a self signed certificate) the flowing command will allow you to accept this state:

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false

Reference Examples

All the reference examples show below are also available in the Reference-Examples.ps1 file.

Get a list of all VMs

Once connected to the server a complete lists of VMs can be obtained using the following function:

function show-vms
{
    $reportPath = $env:TEMP + '\VMReport.html'

    # get all the virtual machines and write the output to a html file
    $vms = Get-VM -Name *
    $vms | ConvertTo-Html | Out-File $reportPath

    # display the html file
    Start-Process $reportPath
}

Start a Virtual Machine that is Powered Off

function Run-VM([string]$vmName)
{
    # get the vm to start
    $vm = Get-VM -Server $server -Name $vmName
    #$vm | Get-View

    $powerState = $vm.PowerState
    if ($powerState -ne 'PoweredOff') {
        Write-Host("** The current state of the VM is ""$powerState"" and will not be started." )
    } 
    else {
        Write-Host("Starting: $vmName")
        Start-VM -VM $vm -Confirm
    }   
}

Creating Snapshots

function Create-Snapshot([string]$vmName, [string]$name, [string]$description) {
    $vm = Get-VM -Server $server -Name $vmName

    New-Snapshot -Server $server -VM $vm -Name $name -Description $description -Quiesce
}

Create a new VM

function Create-VM([string]$vmName, [string]$datastore) {
    $datastore = Get-Datastore -server $server -Name $datastore
    New-VM -Name $vmName -Datastore $datastore -DiskGB 20 -MemoryGB 2    
}

Reference Examples for vCenter

The following examples cannot be executed against the ESXi host directly and requires vCenter to be deployed.

Create a new Linux VM

function Create-LinuxVM([string]$vmName, [string]$datastore, [string]$vmHost, [string]$isopath) {
    # create the new virtual machine
    $vm = New-VM -Server $server -Name $vmName -GuestId "centos7_64Guest" -Datastore $datastore -VMHost $vmHost -DiskGB 20 -MemoryGB 2 -CD
  
    # mount the installation iso
    Get-CDDrive -VM $vm | Set-CDDrive -IsoPath $isopath -StartConnected $true -Confirm:$false

    # start the virtual machine
    Start-VM -VM $vm
}

The GuestId can be looked up in in the SDK under Enum - VirtualMachineGuestOsIdentifier

Move a VM to a different datastore

function Change-VMDatastore([string]$vmName, [string]$datastore) {
    Get-VM -Server $server -Name $vmName | Move-VM -Server $server -Datastore $datastore
}

Clone VM

function Clone-VM([string]$vmName, [string]$datastore, [string]$sourceVM, [string]$referenceSnapshot, [string]$vmHost) {
    # get the source vm
    $vmSource = Get-VM -Server $server -Name $sourceVM

    # set up the creation parameters
    $cloneParams = @{
        'Name' = $vmName
        'Datastore' = $datastore 
        'VM' = $vmSource 
        'DiskStorageFormat' = 'thin'
        'VMHost' = $vmHost
    }

    # add the reference snapshot and LinkedClone parameters if a snapshot was specified
    if ($referenceSnapshot -ne "") {
        $snapshot = Get-Snapshot -VM $vmSource -Name $referenceSnapshot

        $cloneParams.Add("LinkedClone", $null)
        $cloneParams.Add("ReferenceSnapshot", $snapshot)
    }
    
    # clone the vm
    New-VM @cloneParams
}

Web References