PowerCLI

You need to execute the below command if you get the below error while trying to run PowerCLI script for the first time.

$CurrentValue = [Environment]::GetEnvironmentVariable("PSModulePath", "Machine")

[Environment]::SetEnvironmentVariable("PSModulePath", $CurrentValue + ";C:\Program Files (x86)\VMware\Infrastructure\vSphere

powercli error 1

After executing the command

powercli error 2

————

Below excellent PowerCLI commands were taken from :

http://blog.vmpros.nl/2011/07/29/vmware-update-vmware-tools-without-a-reboot-by-powercli/

To execute the below commands, open PowerCLI and connect to the Vcenter server. Then execute the commands you require.

Upgrade VMware Tools, all virtual machines in cluster:

1
Get-Cluster "clustername" | Get-VM | Update-Tools –NoReboot

Upgrade VMware Tools, located in folder:

1
Get-Cluster "clustername" | Get-Folder "Beheer servers" | Get-VM | Update-Tools –NoReboot

Upgrade VMware Tools, selected virtual machine:

1
Get-Cluster "clustername" | Get-VM "BHR-SVR11" | Update-Tools –NoReboot

Get VMware-Tools versions:

1
2
Get-View -ViewType VirtualMachine | select Name, @{ Name=”ToolsVersion”;
Expression={$_.config.tools.toolsVersion}}

Script to get VMware Datastore disk space. Note: This is not my original code yet I have made some changes to it.

AddPsSnapin VMware.VimAutomation.Core
Connect-VIServer VCENTERNAME
Function Percentcal { 
    param(
    [parameter(Mandatory = $true)]
    [int]$InputNum1,
    [parameter(Mandatory = $true)]
    [int]$InputNum2)
    $InputNum1 / $InputNum2*100
}

$datastores = Get-Datastore | Sort Name 

ForEach ($ds in $datastores) 
{    
    if (($ds.Name -match “Shared”) -or ($ds.Name -match “”))
    {
        $PercentFree = Percentcal $ds.FreeSpaceMB $ds.CapacityMB 
        $PercentFree = “{0:N2}” -f $PercentFree
        $ds | Add-Member -type NoteProperty -name PercentFree -value $PercentFree
    }
}
$datastores | Select Name,@{N=”TotalSpaceGB”;E={[Math]::Round(($_.ExtensionData.Summary.Capacity)/1GB,0)}},@{N=”UsedSpaceGB”;E={[Math]::Round(($_.ExtensionData.Summary.Capacity – $_.ExtensionData.Summary.FreeSpace)/1GB,0)}},@{N=”FreeSpaceGB”;E={[Math]::Round(($_.ExtensionData.Summary.FreeSpace)/1GB,0)}},PercentFree | Export-Csv c:\DATASTOREREPORT.csv -NoTypeInformation

PowerCLI script to power OFF VMs in Vcenter

https://communities.vmware.com/blogs/amitrajit/2013/10/22/power-off-a-list-of-vms

Note: Make sure you have VM in the csv file with name at the top as mentioned in the above link



$vcenter="<vcenter name>"  
  
#Connect to vcenter server  
connect-viserver $vcenter  
  
#Import vm name from csv file  
Import-Csv E:\Scripts\power\poweredonvms.csv |  
foreach {  
    $strNewVMName = $_.name  
  
    #Generate a view for each vm to determine power state  
    $vm = Get-View -ViewType VirtualMachine -Filter @{"Name" = $strNewVMName}  
  
    #If vm is powered on then VMware Tools status is checked  
           if ($vm.Runtime.PowerState -ne "PoweredOff") {  
               if ($vm.config.Tools.ToolsVersion -ne 0) {  
  
                   Write-Host "VMware tools installed. Graceful OS shutdown ++++++++ $strNewVMName ----"  
                   Shutdown-VMGuest $strNewVMName -Confirm:$false  
                  
                   #For generating email  
                   $Report += $strNewVMName + " --- VMware tools installed. Graceful OS shutdown `r`n"  
               }  
               else {  
  
                      Write-Host "VMware tools not installed. Force VM shutdown ++++++++ $strNewVMName ----"  
                      Stop-VM $strNewVMName -Confirm:$false  
                              
                   #For generating email  
                   $Report += $strNewVMName + " --- VMware tools not installed. Force VM shutdown `r`n"  
               }  
           }  
}  
  
write-host "Sleeping ..."  
Sleep 300