Tuesday 22 November 2016

How to retrieve Avamar Serial Numbers via SSH


mapall --noerror --user=root --nodes=all+ --quiet 'echo `hostname -i`": "`/usr/bin/ipmitool fru print 0 | grep "Product Asset Tag" | sed "s/^.*:\s\(.*$\)/\1/"`'

Friday 30 October 2015

PowerCLI - iSCSI Configuration


iSCSI storage configuration

Create vdSwitch for iSCSI traffic. Switch will have 2 uplinks, and MTU 9000 (Jumbo frames)
$Datacentre = Get-Datacenter -Name Datacentre_01
New-VDSwitch -Name vdSwitch_iSCSI -Location $Datacentre -NumUplinkPorts 2 -MaxPorts 256 -Version "6.0.0" -Mtu 9000 | Out-Null

The iSCSI vdSwitch will have 2 iSCSI port groups associated with vlan 230. vdPG_iSCSI_1 will only use dvUplink1 and vdPG_iSCSI_2 will only use dvUplink2.
Get-VDSwitch -Name vdSwitch_iSCSI | Get-VDPortgroup | Get-VDUplinkTeamingPolicy | Set-VDUplinkTeamingPolicy -ActiveUplinkPort dvUplink1 -UnusedUplinkPort dvUplink2
Get-VDSwitch -Name vdSwitch_iSCSI | New-VDPortgroup -Name vdPG_iSCSI1 -NumPorts 120 -VLanId 230 -PortBinding Ephemeral
Get-VDSwitch -Name vdSwitch_iSCSI | New-VDPortgroup -Name vdPG_iSCSI2 -NumPorts 120 -VLanId 230 -PortBinding Ephemeral
Get-VDSwitch -Name vdSwitch_iSCSI | Get-VDPortgroup -Name vdPG_iSCSI2 | Get-VDPortgroupOverridePolicy | Set-VDPortgroupOverridePolicy -UplinkTeamingOverrideAllowed $true
Get-VDSwitch -Name vdSwitch_iSCSI | Get-VDPortgroup -Name vdPG_iSCSI2 | Get-VDUplinkTeamingPolicy | Set-VDUplinkTeamingPolicy -ActiveUplinkPort dvUplink2 -UnusedUplinkPort dvUplink1

Add esx host to iSCSI switch
Get-VDSwitch -Name vdSwitch_iSCSI | Add-VDSwitchVMHost -VMHost esx-01.localdomain.local
$nic2 = Get-VMHost -Name esx-01.localdomain.local | Get-VMHostNetworkAdapter -Physical -Name vmnic2
$nic3 = Get-VMHost -Name esx-01.localdomain.local | Get-VMHostNetworkAdapter -Physical -Name vmnic3
Get-VDSwitch -Name $vdSwitch_iSCSI_Name | Add-VDSwitchPhysicalNetworkAdapter -VMHostPhysicalNic $nic2 -Confirm:$false
Get-VDSwitch -Name $vdSwitch_iSCSI_Name | Add-VDSwitchPhysicalNetworkAdapter -VMHostPhysicalNic $nic3 -Confirm:$false

Add to iSCSI vdSwitch 2 vmkernel ports for iSCSI traffic (this has to be done for each esx host)
New-VMHostNetworkAdapter -VMHost esx-01.localdomain.local -VirtualSwitch vdSwitch_iSCSI  -PortGroup vdPG_iSCSI1 -IP 192.168.101.1 -SubnetMask 255.255.255.0 -Mtu 9000
New-VMHostNetworkAdapter -VMHost esx-01.localdomain.local -VirtualSwitch vdSwitch_iSCSI  -PortGroup vdPG_iSCSI2 -IP 192.168.101.101 -SubnetMask 255.255.255.0 -Mtu 9000

Enable software iSCSI storage adapter on esx host
Get-VMHostStorage -VMHost $HostName | Set-VMHostStorage -SoftwareIScsiEnabled $true

Add iSCSI vmkernel ports to iSCSI vdSwitch
$iSCSI1_nic = Get-VMHostNetworkAdapter -VMHost esx-01.localdomain.local | where {$_.IP -like "192.168.101.1"} | select Name
$iSCSI2_nic = Get-VMHostNetworkAdapter -VMHost esx-01.localdomain.local | where {$_.IP -like "192.168.101.101"} | select Name
$HBANumber = Get-VMHostHba -VMHost esx-01.localdomain.local -Type iSCSI | %{$_.Device}
$esxcli = Get-EsxCli -VMHost esx-01.localdomain.local 
$esxcli.iscsi.networkportal.add($HBANumber,$true,$iSCSI1_nic.Name)
$esxcli.iscsi.networkportal.add($HBANumber,$true,$iSCSI2_nic.Name)

Add iSCSI target and rescan for storage
$HBANumber = Get-VMHostHba -VMHost esx-01.localdomain.local -Type iSCSI | %{$_.Device}
New-IScsiHbaTarget -IScsiHba $HBANumber -Address "192.168.101.201"
Get-VMHostStorage -VMHost esx-01.localdomain.local -RescanAllHba
Get-VMHostStorage -VMHost esx-01.localdomain.local -RescanVmfs

Additionally you may want to change iSCSI software adapter Advances Settings. Typically you will disable "DelayedAck" and i.e. for Equallogic array increase "LoginTimeout". I couldn't find any direct command to do this so I have to use Get-View. More about storage manipulation you can find here.
$HostView = Get-VMHost esx-01.localdomain.local | Get-View
$HostStorageSystemID = $HostView.configmanager.StorageSystem
$HBANumber = ($HostView.config.storagedevice.HostBusAdapter | where {$_.Model -match "iSCSI Software"}).device

$options = New-Object VMWare.Vim.HostInternetScsiHbaParamValue[] (2)
$options[0] = New-Object VMware.Vim.HostInternetScsiHbaParamValue  
$options[0].key = "DelayedAck"  
$options[0].value = $false 
$options[1] = New-Object VMware.Vim.HostInternetScsiHbaParamValue  
$options[1].key = "LoginTimeout"  
$options[1].value = 60 

$HostStorageSystem = Get-View -ID $HostStorageSystemID  
$HostStorageSystem.UpdateInternetScsiAdvancedOptions($HBANumber , $null, $options)

And finally I have put all commands together to make easy script to configure iSCSI on a cluster
$VIUser="administrator"
$VIPassword="password"

$DatacentreName="Datacentre_01"
$vdSwitch_iSCSI_Name="vdSwitch_iSCSI"
$vdPG_iSCSI1="vdPG_iSCSI_1"
$vdPG_iSCSI2="vdPG_iSCSI_2"
$iSCSI1_Net="192.168.101."
$iSCSI1_StartIP=1
$iSCSI1_NetMask="255.255.255.0"
$iSCSI2_Net="192.168.101."
$iSCSI2_StartIP=101
$iSCSI2_NetMask="255.255.255.0"
$iSCSI_Targets=@("192.168.101.201","192.168.101.202")
$HostNames=@("esx-01.localdomain.local","esx-02.localdomain.local")

Connect-VIServer vcentre.localdomain.local -User $VIUser -Password $VIPassword | Out-Null

$Datacentre = Get-Datacenter -Name $DatacentreName
New-VDSwitch -Name $vdSwitch_iSCSI_Name -Location $Datacentre -NumUplinkPorts 2 -MaxPorts 256 -Version "6.0.0" -Mtu 9000 | Out-Null

Get-VDSwitch -Name $vdSwitch_iSCSI_Name | Get-VDPortgroup | Get-VDUplinkTeamingPolicy | Set-VDUplinkTeamingPolicy -ActiveUplinkPort dvUplink1 -UnusedUplinkPort dvUplink2
Get-VDSwitch -Name $vdSwitch_iSCSI_Name | New-VDPortgroup -Name $vdPG_iSCSI1 -NumPorts 120 -VLanId 230 -PortBinding Ephemeral
Get-VDSwitch -Name $vdSwitch_iSCSI_Name | New-VDPortgroup -Name $vdPG_iSCSI2 -NumPorts 120 -VLanId 230 -PortBinding Ephemeral
Get-VDSwitch -Name $vdSwitch_iSCSI_Name | Get-VDPortgroup -Name $vdPG_iSCSI2 | Get-VDPortgroupOverridePolicy | Set-VDPortgroupOverridePolicy -UplinkTeamingOverrideAllowed $true
Get-VDSwitch -Name $vdSwitch_iSCSI_Name | Get-VDPortgroup -Name $vdPG_iSCSI2 | Get-VDUplinkTeamingPolicy | Set-VDUplinkTeamingPolicy -ActiveUplinkPort dvUplink2 -UnusedUplinkPort dvUplink1

$options = New-Object VMWare.Vim.HostInternetScsiHbaParamValue[] (2)
$options[0] = New-Object VMware.Vim.HostInternetScsiHbaParamValue  
$options[0].key = "DelayedAck"  
$options[0].value = $false 
$options[1] = New-Object VMware.Vim.HostInternetScsiHbaParamValue  
$options[1].key = "LoginTimeout"  
$options[1].value = 60 

foreach ($HostName in $HostNames){
    $iSCSI1_IP=$iSCSI1_Net+$iSCSI1_StartIP
    $iSCSI2_IP=$iSCSI2_Net+$iSCSI2_StartIP
    Get-VDSwitch -Name $vdSwitch_iSCSI_Name | Add-VDSwitchVMHost -VMHost $HostName
    $nic2 = Get-VMHost -Name $HostName | Get-VMHostNetworkAdapter -Physical -Name vmnic2
    $nic3 = Get-VMHost -Name $HostName | Get-VMHostNetworkAdapter -Physical -Name vmnic3
    Get-VDSwitch -Name $vdSwitch_iSCSI_Name | Add-VDSwitchPhysicalNetworkAdapter -VMHostPhysicalNic $nic2 -Confirm:$false
    Get-VDSwitch -Name $vdSwitch_iSCSI_Name | Add-VDSwitchPhysicalNetworkAdapter -VMHostPhysicalNic $nic3 -Confirm:$false
    New-VMHostNetworkAdapter -VMHost $HostName -VirtualSwitch $vdSwitch_iSCSI_Name  -PortGroup $vdPG_iSCSI1 -IP $iSCSI1_IP -SubnetMask $iSCSI1_NetMask -Mtu 9000 | Out-Null
    $iSCSI1_nic = Get-VMHostNetworkAdapter -VMHost $HostName | where {$_.IP -like $iSCSI1_IP} | select Name
    New-VMHostNetworkAdapter -VMHost $HostName -VirtualSwitch $vdSwitch_iSCSI_Name  -PortGroup $vdPG_iSCSI2 -IP $iSCSI2_IP -SubnetMask $iSCSI2_NetMask -Mtu 9000 | Out-Null
    $iSCSI2_nic = Get-VMHostNetworkAdapter -VMHost $HostName | where {$_.IP -like $iSCSI2_IP} | select Name

    Get-VMHostStorage -VMHost $HostName | Set-VMHostStorage -SoftwareIScsiEnabled $true  | Out-Null

    $HBANumber = Get-VMHostHba -VMHost $HostName -Type iSCSI | %{$_.Device}

    $esxcli = Get-EsxCli -VMHost $HostName
    $esxcli.iscsi.networkportal.add($HBANumber,$true,$iSCSI1_nic.Name)
    $esxcli.iscsi.networkportal.add($HBANumber,$true,$iSCSI2_nic.Name)

    $HostView = Get-VMHost $HostName | Get-View
    $HostStorageSystemID = $HostView.configmanager.StorageSystem
    $HostStorageSystem = Get-View -ID $HostStorageSystemID  
    $HostStorageSystem.UpdateInternetScsiAdvancedOptions($HBANumber , $null, $options)

    foreach ($iSCSI_Target in $iSCSI_Targets){
        New-IScsiHbaTarget -IScsiHba $HBANumber -Address $iSCSI_Target
    }

    Get-VMHostStorage -VMHost $HostName -RescanAllHba
    Get-VMHostStorage -VMHost $HostName -RescanVmfs
}

Disconnect-VIServer -Confirm:$false

Wednesday 28 October 2015

PowerCLI - Cluster Configuration


I've just decided to create some step by step guide how to create VMware cluster via PowerCLI
Please keep in mind that this guide will not show you how to configure all parameters but should give you an idea how to start.

Prerequisite for this guide:
  • vCentre server has to be installed and static IP configured
  • All ESX hosts should have hypervisor installed and static management IP configured
  • There has to be network connectivity between vCentre and ESX hosts

Connect to your vCentre server
 
Connect-VIServer vcentre.localdomain.local


Get default folder list, we will use it to create datacentre
Get-Folder
PowerCLI C:\> Get-Folder

Name                           Type
----                           ----
Datacenters                    Datacenter
vm                             VM
network                        Network
host                           HostAndCluster
datastore                      Datastore
Discovered virtual machine     VM

Create new datacentre "Datacentre_01"
New-Datacenter -Location Datacenters -Name Datacentre_01

Create new cluster in datacentre "Datacentre_01"
New-Cluster -Name Cluster_01 -Location "Datacentre_01"

On "Cluster_01" enable HA, virtual machines in the cluster should not start if they violate availability constraints, cluster should tolerate 1 host failure, VM swap file should be stored wit VM files, VM restart priority in case of host failure should be "Low", in case of host isolation all running VMs should remain online
Get-Cluster -Name Cluster_01 | Set-Cluster -Name Cluster_01 -HAEnabled:$true -HAAdmissionControlEnabled:$true -HAFailover 1 -VMSwapfilePolicy "WithVM" -HARestartPriority "Low" -HAIsolationResponse "DoNothing" -Confirm:$false

On "Cluster_01" enable DRS and set to be fully automated
Set-Cluster -Cluster Cluster_01 -DRSEnabled:$true -DRSAutomationLevel "FullyAutomated" -Confirm:$false

On "Cluster_01" enable EVC for intel-nehalem
Set-Cluster -Cluster Cluster_01 -EVCMode "intel-nehalem" -Confirm:$false

If you want to disable EVC run:
Set-Cluster -Cluster Cluster_01 -EVCMode $null -Confirm:$false

Add host esx-01 to "Cluster_01"
Add-VMHost -Name esx-01.localdomain.local -Location Cluster_01 -User root -Password password

If you want to remove host from cluster run:
Set-VMHost -VMHost esx-01.localdomain.local -State Disconnected
Remove-VMHost esx-01.localdomain.local -Confirm:$false

Put your host in maintenance mode
Set-VMHost -VMHost esx-01.localdomain.local -State Maintenance

Get esx host physical nic list
Get-VMHost -Name esx-01.localdomain.local | Get-VMHostNetworkAdapter | where {$_.DeviceName -like "*nic*"} | select Name
PowerCLI C:\>Get-VMHost -Name esx-01.localdomain.local | Get-VMHostNetworkAdapter | where {$_.DeviceName -like "*nic*"} | select Name
Name
----
vmnic0
vmnic1
vmnic2
vmnic3

Confirm that there is only one nic attached to default vSwitch0
Get-VMHost -Name esx-01.localdomain.local | Get-VirtualSwitch -Name vSwitch0 | select Nic
PowerCLI C:\>Get-VMHost -Name esx-01.localdomain.local | Get-VirtualSwitch -Name vSwitch0 | select Nic
Nic
---
{vmnic0}

Add 2nd physical nic "vmnic1" to vSwitch0
$nic = Get-VMHost -Name esx-01.localdomain.local | Get-VMHostNetworkAdapter -Physical -Name vmnic1
Get-VMHost -Name esx-01.localdomain.local | Get-VirtualSwitch -Name vSwitch0 | Add-VirtualSwitchPhysicalNetworkAdapter -VMHostPhysicalNic $nic -Confirm:$false

Create new VM port group on vSwitch0
Get-VMHost -Name esx-01.localdomain.local | Get-VirtualSwitch -Name vSwitch0 | New-VirtualPortGroup -Name vPortgroup

Add vlan id 120 to port group "vPortgroup"
Get-VMHost -Name esx-01.localdomain.local | Get-VirtualSwitch -Name vSwitch0 | Get-VirtualPortGroup -Name vPortgroup | Set-VirtualPortGroup -name test -VLanId 120

Create new VM port group on vSwitch0 with vlan id 120
Get-VMHost -Name esx-01.localdomain.local | Get-VirtualSwitch -Name vSwitch0 | New-VirtualPortGroup -Name vPortgroup -VLanId 120

Create new vdSwitch with 2 uplinks
$Datacentre = Get-Datacenter -Name Datacentre_01
New-VDSwitch -Name vdSwitch0 -Location $Datacentre -NumUplinkPorts 2 -LinkDiscoveryProtocol "LLDP" -LinkDiscoveryProtocolOperation "Listen" -MaxPorts 256 -Version "6.0.0"

Set uplink teaming policy for vdSwitch0: dvUplink1 as active and dvUplink2 as standby
Get-VDSwitch -Name vdSwitch0 | Get-VDPortgroup | Get-VDUplinkTeamingPolicy | Set-VDUplinkTeamingPolicy -ActiveUplinkPort dvUplink1 -StandbyUplinkPort dvUplink2

Create new port group Production on vdSwitch0 with 16 ports, vlan id 200 and port binding Ephemeral
Get-VDSwitch -Name vdSwitch0 | New-VDPortgroup -Name Production -NumPorts 16 -VLanId 200 -PortBinding Ephemeral

Create new port group vMotion on vdSwitch0 with 16 ports, vlan id 110 and port binding Ephemeral
Get-VDSwitch -Name vdSwitch0 | New-VDPortgroup -Name vMotion -NumPorts 16 -VLanId 110 -PortBinding Ephemeral

Overwrite uplink teaming policy for vMotion port group: dvUplink2 as active and dvUplink1 as standby
Get-VDSwitch -Name vdSwitch0 | Get-VDPortgroup -Name vMotion | Get-VDPortgroupOverridePolicy | Set-VDPortgroupOverridePolicy -UplinkTeamingOverrideAllowed $true
Get-VDSwitch -Name vdSwitch0 | Get-VDPortgroup -Name vMotion | Get-VDUplinkTeamingPolicy | Set-VDUplinkTeamingPolicy -ActiveUplinkPort dvUplink2 -StandbyUplinkPort dvUplink1

Add esx-01 host to vdSwitch0
Get-VDSwitch -Name vdSwitch0 | Add-VDSwitchVMHost -VMHost esx-01.localdomain.local

Add vmnic2 and vmnic3 of esx-01 host to vdSwitch0 uplinks
$nic2 = Get-VMHost -Name esx-01.localdomain.local | Get-VMHostNetworkAdapter -Physical -Name vmnic2
$nic3 = Get-VMHost -Name esx-01.localdomain.local | Get-VMHostNetworkAdapter -Physical -Name vmnic3
Get-VDSwitch -Name vdSwitch0 | Add-VDSwitchPhysicalNetworkAdapter -VMHostPhysicalNic $nic2 -Confirm:$false
Get-VDSwitch -Name vdSwitch0 | Add-VDSwitchPhysicalNetworkAdapter -VMHostPhysicalNic $nic3 -Confirm:$false

Add vmkernel port to vdSwitch0 to vMotion port group and configure it for vMotion traffic with static IP address
New-VMHostNetworkAdapter -VMHost esx-01.localdomain.local -VirtualSwitch vdSwitch0 -PortGroup vMotion -IP 192.168.0.1 -SubnetMask 255.255.255.0 -VMotionEnabled:$true

Remove your host from maintenance mode
Set-VMHost -VMHost esx-01.localdomain.local -State Connected


And finally I have put all commands together to make easy script to configure a cluster
 
$VIUser="administrator"
$VIPassword="password"
$HostUser="root"
$HostPassword="hostpassword"

$DatacentreName="Datacentre_01"
$ClusterName="Cluster_01"
$vSwitchName="vSwitch0"
$vdSwitchName="vdSwitch0"
$vMotionNet="192.168.0."
$VMotionStartIP=1
$VMotionNetMask="255.255.255.0"$HostNames=@("esx-01.localdomain.local","esx-02.localdomain.local")



Connect-VIServer vcentre.appv4u.com -User $VIUser -Password $VIPassword



New-Datacenter -Location Datacenters -Name $DatacentreName
New-Cluster -Name $ClusterName -Location $DatacentreName
Get-Cluster -Name $ClusterName | Set-Cluster -Name $ClusterName -HAEnabled:$true -HAAdmissionControlEnabled:$true -HAFailover 1 -VMSwapfilePolicy "WithVM" -HARestartPriority "Low" -HAIsolationResponse "DoNothing" -Confirm:$false
Set-Cluster -Cluster $ClusterName -DRSEnabled:$true -DRSAutomationLevel "FullyAutomated" -Confirm:$false
Set-Cluster -Cluster $ClusterName -EVCMode "intel-nehalem" -Confirm:$false

$Datacentre = Get-Datacenter -Name $DatacentreName
New-VDSwitch -Name $vdSwitchName -Location $Datacentre -NumUplinkPorts 2 -LinkDiscoveryProtocol "LLDP" -LinkDiscoveryProtocolOperation "Listen" -MaxPorts 256 -Version "6.0.0"

Get-VDSwitch -Name $vdSwitchName | Get-VDPortgroup | Get-VDUplinkTeamingPolicy | Set-VDUplinkTeamingPolicy -ActiveUplinkPort dvUplink1 -StandbyUplinkPort dvUplink2
Get-VDSwitch -Name $vdSwitchName | New-VDPortgroup -Name Production -NumPorts 16 -VLanId 200 -PortBinding Ephemeral
Get-VDSwitch -Name $vdSwitchName | New-VDPortgroup -Name vMotion -NumPorts 16 -VLanId 110 -PortBinding Ephemeral
Get-VDSwitch -Name $vdSwitchName | Get-VDPortgroup -Name vMotion | Get-VDPortgroupOverridePolicy | Set-VDPortgroupOverridePolicy -UplinkTeamingOverrideAllowed $true
Get-VDSwitch -Name $vdSwitchName | Get-VDPortgroup -Name vMotion | Get-VDUplinkTeamingPolicy | Set-VDUplinkTeamingPolicy -ActiveUplinkPort dvUplink2 -StandbyUplinkPort dvUplink1



foreach ($HostName in $HostNames){

 $VMotionIP=$vMotionNet+$VMotionStartIP
 Add-VMHost -Name $HostName -Location $ClusterName -User $HostUser -Password $HostPassword | Out-Null

 Set-VMHost -VMHost $HostName -State Maintenance | Out-Null

 $nic1 = Get-VMHost -Name $HostName | Get-VMHostNetworkAdapter -Physical -Name vmnic1
 Get-VMHost -Name $HostName | Get-VirtualSwitch -Name vSwitch0 | Add-VirtualSwitchPhysicalNetworkAdapter -VMHostPhysicalNic $nic1 -Confirm:$false

 Get-VDSwitch -Name $vdSwitchName | Add-VDSwitchVMHost -VMHost $HostName 
 $nic2 = Get-VMHost -Name $HostName | Get-VMHostNetworkAdapter -Physical -Name vmnic2
 $nic3 = Get-VMHost -Name $HostName | Get-VMHostNetworkAdapter -Physical -Name vmnic3
 Get-VDSwitch -Name $vdSwitchName | Add-VDSwitchPhysicalNetworkAdapter -VMHostPhysicalNic $nic2 -Confirm:$false
 Get-VDSwitch -Name $vdSwitchName | Add-VDSwitchPhysicalNetworkAdapter -VMHostPhysicalNic $nic3 -Confirm:$false
 New-VMHostNetworkAdapter -VMHost $HostName -VirtualSwitch $vdSwitchName  -PortGroup vMotion -IP $VMotionIP -SubnetMask $VMotionNetMask -VMotionEnabled:$true

 Set-VMHost -VMHost $HostName -State Connected
 $VMotionStartIP++
 $VMotionIP=""
}

Disconnect-VIServer -Confirm:$false

Friday 7 August 2015

PowerCLI - VM manipulation


How to get vCPU count of all powered on VMs of particular ESX host

Get-VMHost -Name HostName | get-vm | Where {$_.PowerState -eq "PoweredOn"} | Measure-Object -Property NumCpu -Sum | select -ExpandProperty Sum

How to get count of all powered on VMs of particular ESX host

$a = Get-VMHost -Name HostName | get-vm | Where {$_.PowerState -eq "PoweredOn"} | select NumCpu; $a.count

How to get summary of VM snapshot size

$vms = get-vm; foreach ($vm in $vms) {$snap = get-vm -Name $vm.Name | Get-Snapshot | Measure-Object -Property SizeGB -Sum | Select Name -ExpandProperty Sum; if ($snap -gt 0) {$vm.Name, $snap}}

Get Canonical Name and Capacity of all Raw Devices (RDM) attached to each VM in the cluster

foreach ($v in $(get-cluster -Name Cluster Name | Get-VM)) {$v.Name; Get-VM -Name $v.Name | Get-HardDisk -DiskType RawPhysical | select ScsiCanonicalName,CapacityGB}

Thursday 6 August 2015

PowerCLI - Datastore manipulation


Get all LUNs connected to host

Get-Cluster -Name ClusterName | Get-VMHost -Name HostName | Get-ScsiLun

Get all "disk" LUNs connected to host

Get-Cluster -Name ClusterName | Get-VMHost -Name HostName | Get-ScsiLun -LunType disk

Get alldatastores name, free space, capacity

Get-View -ViewType Datastore -Property Name, Info | Get-VIObjectByVIView

Set MultiPath Policy to "RoundRobin" on all LUNs which have not "RoundRobin" already

Get-Cluster -Name ClusterName | Get-VMHost -Name HostName | Get-ScsiLun -LunType disk | Where {$_.MultiPathPolicy -ne "RoundRobin"} | Set-ScsiLun -MultiPathPolicy "RoundRobin"

Get full information of all LUNs connected to host (you can find here SAN device Model which will be used in next command)

Get-Cluster -Name ClusterName | Get-VMHost -Name HostName | Get-ScsiLun | fl

Set MultiPath Policy to "RoundRobin" on all LUNs where SAN model is "VRAID"

Get-Cluster -Name ClusterName | Get-VMHost -Name HostName | Get-ScsiLun -LunType disk | Where {$_.MultiPathPolicy -ne "RoundRobin" -and $_.Model -eq "VRAID"} | Set-ScsiLun -MultiPathPolicy "RoundRobin"

Get Canonical Name ans Capacity of all LUNs attached to each host in the cluster

foreach ($v in $(get-cluster -Name ClusterName | Get-VMHost)) {$v.name; Get-VMHost -name $v.Name | Get-ScsiLun | select CanonicalName,CapacityGB}

Get Canonical Name and Prefered Path of all LUNs connected to host where Path Policy is set to "Fixed" and SAN model is not "VRAID"

Get-Cluster -Name ClusterName | Get-VMHost -Name HostName | Get-ScsiLun -LunType disk | Where {$_.MultiPathPolicy -eq "Fixed" -and $_.Model -ne "VRAID"} | Get-ScsiLunPath | select ScsiCanonicalName, Name | Export-Csv -NoTypeInformation -UseCulture -Path c:\test\zone1_path.csv

Get all Equallogic datastore path policy and iop to switch path

Get-VMHost | Get-ScsiLun | Where {$_.Vendor -eq "EQLOGIC"} | select VMHost, MultipathPolicy, CommandsToSwitchPath

Get all Equallogic datastore path policy to "RoundRobin" and iop to switch path to "3"

Get-VMHost | Get-ScsiLun | Where {$_.Vendor -eq "EQLOGIC"} | Set-ScsiLun -MultipathPolicy RoundRobin -CommandsToSwitchPath 3

Display all datastores attached to ESX hosts with their Canonical Names

get-vmhost -Name HostName | Get-Datastore | Where-Object {$_.ExtensionData.Info.GetType().Name -eq "VmfsDatastoreInfo"} | ForEach-Object { if ($_) {$Datastore = $_; $Datastore.ExtensionData.Info.Vmfs.Extent | Select-Object -Property @{Name="Name";Expression={$Datastore.Name}},DiskName}}

Tuesday 14 July 2015

PowerCLI - How to get cluster resource information


Get cluster available resources and usage

Get-Cluster -Name ClusterName | get-vmhost | select Name, NumCpu, CpuUsageMhz, CPUTotalMhz, MemoryUsageGB, MemoryTotalGB | Export-Csv -NoTypeInformation -UseCulture -Path c:\test\vent1.csv

Get all datastores per cluster

foreach ($cluster in Get-Cluster){Add-content fi $cluster.Name; Get-cluster -name $cluster.Name | Get-Datastore | select Name, FreeSpaceGB, CapacityGB}

Get all datastores per cluster and export to txt file

foreach ($cluster in Get-Cluster){Add-content "C:\test\test.txt" "$cluster"; Add-Content "C:\test\test.txt" "Name, FreeSpaceGB, CapacityGB"; $d=Get-cluster -name $cluster.Name | Get-Datastore | select Name, FreeSpaceGB, CapacityGB; $d | % {add-content "c:\test\test.txt" "$($_.Name), $($_.FreeSpaceGB), $($_.CapacityGB)"}}

Get count of all hosts cpu cores

Get-Cluster -Name SharedEnterprisePlus01 | Get-VMHost | Measure-Object -Property NumCpu -Sum | select -ExpandProperty Sum

Get list of all NOT empty resource pools in the cluster based on pool's VM's cpu count

$ResourcePools = Get-Cluster -Name ClusterName | Get-ResourcePool
foreach ($Pool in $ResourcePools){$a = Get-ResourcePool -Name $Pool.Name | Get-VM | Measure-Object -Property NumCpu -Sum | select -ExpandProperty Sum; if($a -gt 0) {$Pool.Name}}

Get list of all VMs which have "prod" in their name and summary information about disk size

$a = get-vm | where{$_.Name -like "*prod*"}; foreach ($v in $a) {Write-Host $v.Name; Get-HardDisk -vm $v.name | Measure-Object -Property CapacityGB -Sum | select -ExpandProperty Sum}

Get list of all VMs which have "prod" in their name and summary information about disk size and write to txt file. I've used Write-Host commands to display output on the screen too.

$a = get-vm; foreach ($v in $a) {$s = Get-HardDisk -vm $v.name | Measure-Object -Property CapacityGB -Sum | select -ExpandProperty Sum; $s | %{Add-Content "d:\iso\vm.txt" "$($v.Name), $($_)"}; Write-Host $v.Name; Write-Host $s}

Friday 10 July 2015

PowerCLI – How to get Vmware tools version


Get vmtools version for all VMs


get-vm | % {get-view $_.id} | select Name,@{Name="Tools Version"; Expression={$_.Config.tools.toolsversion}}, @{ Name="Tool Status"; Expression={$_.Guest.ToolsVersionStatus}}

Get vmtools version for all VMs from a cluster

get-cluster -Name ClusterName | get-vm | % {get-view $_.id} | select Name, @{Name="Tools Version"; Expression={$_.Config.tools.toolsversion}}, @{ Name="Tool Status"; Expression={$_.Guest.ToolsVersionStatus}}

Get vmtools version for all powered on VMs

get-vm | where {$_.powerstate -eq "PoweredOn"} | % {get-view $_.id} | select Name, @{Name="Tools Version"; Expression={$_.Config.tools.toolsversion}}, @{ Name="Tool Status"; Expression={$_.Guest.ToolsVersionStatus}}

List all VMs with out of date vmtools

get-vm | where {$_.Guest.ToolsVersionStatus -ne "guestToolsCurrent"} | % {get-view $_.id} | select Name, @{Name="Tools Version"; Expression={$_.Config.tools.toolsversion}}, @{ Name="Tool Status"; Expression={$_.Guest.ToolsVersionStatus}}

Get vmtools for all VMs with Microsoft operating system

get-cluster -Name ClusterName | get-vm | % {get-view $_.id} | where {$_.config.guestfullname -like "*Microsoft*"} | select Name, @{Name="OS Version"; Expression={$_.config.guestfullname}}, @{Name="Tools Version"; Expression={$_.Config.tools.toolsversion}}, @{Name="Tools Status"; Expression={$_.Guest.ToolsVersionStatus2}}

Get vmtools version for all VMs exported to csv file

get-vm | % {get-view $_.id} | select Name, @{Name="Tools Version"; Expression={$_.Config.tools.toolsversion}}, @{ Name="Tool Status"; Expression={$_.Guest.ToolsVersionStatus}} | Export-Csv -NoTypeInformation -UseCulture -Path C:\Tools\ToolsVersion.csv