-
Notifications
You must be signed in to change notification settings - Fork 1
PowerCLI
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
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
All the reference examples show below are also available in the Reference-Examples.ps1 file.
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
}
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
}
}
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
}
function Create-VM([string]$vmName, [string]$datastore) {
$datastore = Get-Datastore -server $server -Name $datastore
New-VM -Name $vmName -Datastore $datastore -DiskGB 20 -MemoryGB 2
}
The following examples cannot be executed against the ESXi host directly and requires vCenter to be deployed.
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
function Change-VMDatastore([string]$vmName, [string]$datastore) {
Get-VM -Server $server -Name $vmName | Move-VM -Server $server -Datastore $datastore
}
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
}
- How to clone VMs if VMware vCenter is unavailable - https://www.vmwareblog.org/clone-vms-vmware-vcenter-unavailable/
- Verifying time synchronization across an ESX/ESXi host environment - https://kb.vmware.com/s/article/1003736
- How to Deploy Virtual Machines in vSphere Using PowerCLI - https://www.businessnewsdaily.com/11038-use-powercli-vsphere-deployment.html
- How to Install PowerCLI on Ubuntu Linux 18.04 LTS - https://www.altaro.com/vmware/install-powercli-ubuntu-linux-18-04-lts/
- How to Deploy a VM using PowerCLI and customized settings using a PowerCLI CustomizationSpec - http://powershelldistrict.com/powercli-oscustomizationspec/
- vSphere PowerCLI Reference: New-VM - https://pubs.vmware.com/vsphere-51/index.jsp?topic=%2Fcom.vmware.powercli.cmdletref.doc%2FNew-VM.html
- Move a Virtual Machine to a Different Datastore Using VMware vSphere Storage vMotion -https://pubs.vmware.com/vsphere-51/index.jsp?topic=%2Fcom.vmware.powercli.ug.doc%2FGUID-4A17E665-C8E8-4C8C-9CFA-3F10585FB833.html
- How to Deploy Virtual Machines in vSphere Using PowerCLI - https://www.businessnewsdaily.com/11038-use-powercli-vsphere-deployment.html
- Deploy VMware VMs with PowerCLI and MDT - https://4sysops.com/archives/deploy-vmware-vms-with-powercli-and-mdt/
- VMware PowerCLI Blog - https://blogs.vmware.com/PowerCLI/2014/05/working-customization-specifications-powercli-part-1.html
- Guest operating system identifier -https://vdc-download.vmware.com/vmwb-repository/dcr-public/da47f910-60ac-438b-8b9b-6122f4d14524/16b7274a-bf8b-4b4c-a05e-746f2aa93c8c/doc/vim.vm.GuestOsDescriptor.GuestOsIdentifier.html