PowerCLI

I will describe here how to install/update PowerCLI and also show examples of commands/scripts I often use.

If you want to work with VMware PowerCLI (11.0.0), make sure that the following software is present on your system:

OS Type .NET Version PowerShell Version
Windows .NET Framework 4.5, 4.5.x, 4.6, 4.6.x or 4.7.x Windows PowerShell 3.0, 4.0, 5.0, or 5.1
Ubuntu .NET Core 2.0 PowerShell Core 6.1
macOS .NET Core 2.0 PowerShell Core 6.1

The following Operating Systems are supported:

OS Type 64-Bit
Server
  • Windows Server 2016
  • Windows Server 2012 R2
  • Windows Server 2008 R2 Service Pack 1
Workstation
  • Windows 10
  • Windows 7 Service Pack 1
  • Ubuntu 16.04
  • macOS 10.12

Install PowerCLI:

Install-Module -Name VMware.PowerCLI

Update PowerCLI:

Update-Module -Name VMware.PowerCLI

Check on which version of PowerCLI you are:

Get-Module -Name VMware.PowerCLI

This will give you the following output:

 

Connect to a vCenter server or ESXi host:

Connect-VIServer -Server <server> -User <user> -Password <password> 

Connect to a vCenter using an encrypted password:

New-VICredentialStoreItem -User <user> -Password <user> -Host <server> -File C:\<your location.xml>

The default location for this XML is “C:\Users\<username>\AppData\Roaming\VMware\credstore\vicredentials.xml”

Connect to a vCenter server using this XML file:

$user =New-VICredentialStoreItem -User <user> -Password <user> -Host <server> -File C:\<your location.xml>
Connect-VIServer -Server <server> -user $user.User -password $user.Password 

Configure the Agent VM settings for a Host:

$datastore = <DATASTORE> 
$network = <NETWORK>
$Host = Get-VMHost -Name <HOSTNAME>
$agMgr = Get-View -Id $Host.ExtensionData.ConfigManager.EsxAgentHostManager
$agent = New-Object VMware.Vim.HostEsxAgentHostManagerConfigInfo
$agent.AgentVmDatastore = (Get-Datastore -Name $datastore).ExtensionData.MoRef
$agent.AgentVmNetwork = $Host.ExtensionData.Network | where{(Get-View -Id $_).Name -eq $network}
$agMgr.EsxAgentHostManagerUpdateConfig($agent)

Add datastore(s) to a new Host using a reference Host:

$source = <SOURCE>
$destination = <DESTINATION>
foreach ($datastore in (Get-vmhost $source | Get-Datastore | where {$_.Type -eq "nfs" -and $_.Accessible -eq "true"})){New-Datastore -vmhost $destination -Nfs -Name $datastore.Name -Path $datastore.RemotePath -NfsHost $datastore.RemoteHost}

Example of working with advanced settings using PowerCLI:

$esxi = <HOST>
$localdatastore = <DATASTORE>
### Disables SSH warning ###
Write-Host "Disable SSH Warning" 
Get-AdvancedSetting -Entity $esxi -Name UserVars.SuppressShellWarning | Set-AdvancedSetting -Value 1 -Confirm:$false
### Change MaxQueueDepth setting ###
Write-Host "Set NFS.MaxQueueDepth"
Get-AdvancedSetting -Entity $esxi -Name NFS.MaxQueueDepth | Set-AdvancedSetting -Value 64 -Confirm:$false
### Edit Max NFS Volumes ###
Write-Host "Set NFS.MaxVolumes"
Get-AdvancedSetting -Entity $esxi -Name NFS.MaxVolumes | Set-AdvancedSetting -Value 256 -Confirm:$false
### Change SYSLOG location ###
Write-Host "Set Syslog.global.logDir"
Get-AdvancedSetting -Entity $esxi -Name Syslog.global.logDir | Set-AdvancedSetting  -Value "[$localdatastore]scratch/log" -Confirm:$false
### Change ProductLockerLocation ###
Write-Host "Set UserVars.ProductLockerLocation"
Get-AdvancedSetting -Entity $esxi -Name UserVars.ProductLockerLocation | Set-AdvancedSetting  -Value "/vmfs/volumes/vmtemplates/VMWARETOOLS" -Confirm:$false

Create a csv file containing the VM name and configured OS:

$Filepath = <FilePath>
$Servers = Get-VM | Sort-Object -Property Name | 
Get-View -Property @("Name", "Config.GuestFullName") | 
Select -Property Name, 
@{N="Configured OS";E={$_.Config.GuestFullName}}
$Servers | Export-Csv $Filepath -NoTypeInformation -UseCulture

Enable/Disable SSH on a Host:

# Enable SSH on a Host #
Get-VMhost | Get-VMHostService | Where-Object { $_.Key -eq “TSM-SSH”} | Start-VMHostService -Confirm:$false
# Disable SSH on a Host #
Get-VMhost | Get-VMHostService | Where-Object { $_.Key -eq “TSM-SSH”} | Stop-VMHostService -Confirm:$false