MOCs
- Overview
- Installation
- Connecting to vSphere
- Virtual Machine Management
- Host and Cluster Management
- Networking Management
- Storage Management
- vCenter Server Appliance Management
- Working with VM Templates
- Snapshot Management
- PowerCLI Scripts
- Tips and Tricks
Overview
VMware PowerCLI is a command-line interface tool for managing VMware vSphere environments, leveraging the VMware vSphere API. It’s built on Windows PowerShell and provides more than 700 cmdlets for managing VMware products and services.
Documentation
Installation
Warning
Make sure you run
Set-ExecutionPolicy RemoteSigned
before running the following
- PowerCLI can be installed directly from the PowerShell Gallery:
Install-Module -Name VMware.PowerCLI
Connecting to vSphere
- Connect to a vCenter Server or ESXi host:
Connect-VIServer -Server <server_name> -User <username> -Password <password>
Virtual Machine Management
- Creating a New VM:
New-VM -Name "VMName" -VMHost "HostName" -Datastore "DatastoreName" -OS "OperatingSystem" -MemoryGB 4 -DiskGB 40
- Starting a VM:
Start-VM -VM "VMName"
- Stopping a VM:
Stop-VM -VM "VMName" -Confirm:$false
Host and Cluster Management
- Listing all ESXi Hosts:
Get-VMHost
- Adding a Host to a Cluster:
Add-VMHost -Name "HostName" -Location "ClusterName" -User "root" -Password "password"
Networking Management
- Creating a Virtual Switch:
New-VirtualSwitch -VMHost "VMHost" -Name "NewSwitch"
- Creating a Port Group:
New-VirtualPortGroup -VirtualSwitch "VirtualSwitch" -Name "NewPortGroup" -VLanId 10
Storage Management
- Listing Datastores:
Get-Datastore
- Creating a New Datastore:
New-Datastore -VMHost "VMHost" -Name "NewDatastore" -Path "/vmfs/volumes/new_datastore" -Type VMFS
vCenter Server Appliance Management
- Restarting vCenter Services:
Restart-Service -Name vmware-vpxd
Working with VM Templates
- Creating a VM Template:
New-Template -VM "VMName" -Name "TemplateName" -Location "Datastore"
- Deploying a VM from a Template:
New-VM -Name "NewVM" -Template "TemplateName" -VMHost "HostName"
Snapshot Management
- Creating a Snapshot:
New-Snapshot -VM "VMName" -Name "SnapshotName"
- Removing a Snapshot:
Remove-Snapshot -VM "VMName" -Name "SnapshotName" -Confirm:$false
PowerCLI Scripts
- PowerCLI can be used to script complex tasks, combining multiple cmdlets into scripts to automate repetitive tasks or to schedule operations.
Tips and Tricks
- Updating PowerCLI:
Update-Module -Name VMware.PowerCLI
- Automating Tasks: Leverage PowerCLI to automate tasks such as VM deployment, configuration changes, and performance monitoring.
- Security Best Practices: Use secure methods for handling credentials, such as PowerShell’s secure string or credential store.