POWERCLI

Here some examples of PowerCLI commands I frequently use during my work.

Install PowerCLI 11.0 on Windows

  • Remove old versions of PowerCLI (6.5)
  • Check PowerShell Version

$PSversionTable.PSVersion

  • Make sure Windows Management Framework (WMF) 5.1 is installed. If not install WMF 5.1, link
  • Run the  following command in PowerShell:
  • Install-Module -Name VMware.PowerCLI –Scope CurrentUser
    

    Bypass PowerCLI security warnings “Run only scripts that you trust. While scripts from the Internet can be useful, this script can potentially harm your computer. Do you want to run –.ps1? [D] Do not run [R] Run once

    Set-ExecutionPolicy bypass
    

    Update the PowerCLI modules

    Update-Module -Name VMware.PowerCLI
    

    Check if there is a new version of PowerCLi available

     Update-Module VMware.PowerCLI -whatif 

    Import all the PowerCLI VMware modules

    Get-module -ListAvailable VMware* | Import-Module
    

    Connect to vCenter or ESX host by using an encrypted password

    New-VICredentialStoreItem -Host <servername> -Password PASSWORD -User <username> -file C:\"PowerCLI\vicredentials.xml"
    

    Connect to vCenter by using the XML file

    $creds = Get-VICredentialStoreItem -file C:\"PowerCLI\vicredentials.xml"
    Connect-VIServer -Server $creds.Host -user $creds.User -password $creds.Password
    
    

    Put an ESXi host in maintenance mode

    Connect-Viserver <server>
    Get-VMHost -Name <host> | set-vmhost -State Maintenance
    

    Enable SSH for all ESXi hosts

    Get-Cluster | Get-VMhost | Get-VMHostService | Where-Object { $_.Key -eq “TSM-SSH”} | Start-VMHostService -Confirm:$false
    

    Disable SSH for all ESXi hosts

    Get-Cluster | Get-VMhost | Get-VMHostService | Where-Object { $_.Key -eq “TSM-SSH”} | Stop-VMHostService -Confirm:$false
    

    Running a schedule task

    C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe "&amp; 'C:\PowerCLI\snapshot.ps1'"
    

    Change root password VMware ESX(i) host

    Connect-VIserver servername
    Set-VMHostAccount -UserAccount root -Password Password
    

    Change multipath policy to RoundRobin for alle the datastores that matches the

    Get-vmhost servername | Get-ScsiLun -CanonicalName 'naa.6000eb*' | Set-ScsiLun -MultipathPolicy "RoundRobin"
    

    HP LeftHand = naa.6000*
    HP 3PAR = naa.60002ac*
    HP EVA = naa.6001*
    NetApp = naa.60a9*

    Update VMware tools VM

    get-vm VNNAME | update-tools
    

    Get the total amount of vRAM used by the powered on VMs

    Get-VM | where {$_.PowerState -eq "PoweredOn"} | Measure-Object -Property MemoryMB -Sum
    

    vSAN

    Test-VsanClusterHealth
    Test-VsanNetworkPerformance
    Test-VsanStoragePerformance

    Test vSAN Performance

    Test-VsanStoragePerformance -Cluster $cluster -TestDurationsSeconds 120 -StoragePolicy $policy -WorkLoad BasicSanityTest