This is an old revision of the document!
http://www.virtu-al.net/script-list/
For Windows you can use PowerShell: powercli For Linux, use: vSphere Perl SDK
If you have an error message when starting PowerCLI, open a normal PowerShell as administrator:
Set-ExecutionPolicy RemoteSigned Measure-Command { Get-VMHost } | fl TotalSeconds
PS> Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -WarningAction SilentlyContinue -confirm:$false PS> Connect-VIServer -Server vc01.domain.local -User administrator@vsphere.local -Password ThisIsNotSecure
If you want to use usernames and passwords in text files it is better to use the standard PowerShell stuff for this, the PSCredential object. You can construct a PSCredential object with:
$credentials=Get-Credential
Again, if you don’t want the prompt you can generate it completely on the command line:
$credentials=Get-Credential -UserName administrator@vsphere.local -Message "Enter your vCenter password"
You will get prompted for a password. Now if you want to connect to vCenter you can use:
Connect-VIServer -Server vc01.domain.local -Credential $credentials
If you want to connect to more vCenter servers at the same time, or just don’t like generic connecting there is good news. You can assign the connection object to a variable. With that variable you can do other stuff as shown in the next example:
$vc1 = Connect-VIServer -Server vc01.domain.local -Credential $credentials Get-VM -Server $vc1
How to implement CBT, what do you need ?
VM version 7 at least, No snapshot on your VM Enable CBT, Finally the VM must go through a stun-unstun cycle (power on, resume after suspend, migrate, or snapshot create/delete/revert) before the reconfiguration takes effect.
Connect-Viserver <ip>
login…
passwd….
get-cbt.ps1
#Add-PSSnapin vmware.vimautomation.core #Connect-Viserver #vcenterserver ##################################### ## http://kunaludapi.blogspot.com ## Version: 1 ## Tested this script on ## 1) Powershell v3 ## 2) Powercli v5.5 ## 3) Vsphere 5.x #################################### function Get-VMinventory { } foreach ($vm in (get-vm)) { $props = @{'VMName'=$vm.Name; 'PowerState'= $vm.PowerState; 'CBTtracking' = (Get-VM -Name $vm).ExtensionData.Config.ChangeTrackingEnabled; } $obj = New-Object -TypeName PSObject -Property $Props Write-Output $obj | select-object -Property 'VMName','PowerState','CBTtracking' } Get-VMinventory
tck_status.ps1
# Find VMs where CBT (Change Block Tracking) is Enabled write-output "****** CTK Enabled" Get-VM|Where-Object{$_.ExtensionData.config.ChangeTrackingEnabled -eq $true} # Find VMs where CBT (Change Block Tracking) is Disabled write-output "****** No CTK Enabled" Get-VM|Where-Object{$_.ExtensionData.config.ChangeTrackingEnabled -eq $false}
Connect-Viserver <ip>
login…
passwd….
change-cbt.ps1
#Here is aRunning the script on VM to enable CBT $vmtest = Get-vm TDPVEDM01 | get-view $vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec $vmConfigSpec.changeTrackingEnabled = $true $vmtest.reconfigVM($vmConfigSpec)
enable-cbt-all.ps1
Get-VM | Get-View | foreach { $spec = New-Object VMware.Vim.VirtualMachineConfigSpec $spec.changeTrackingEnabled = $true $taskMoRef = $_.ReconfigVM_Task($spec) }
Connect to the VCenter
PS> Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -WarningAction SilentlyContinue -confirm:$false PS> Connect-VIServer -Server vc01.domain.local -User administrator@vsphere.local -Password ThisIsNotSecure
Rescan all hosts
PS> Get-VMHost | Get-VMHostStorage -RescanAllHba
Rescan all hosts in a cluster:
PS> Get-Cluster -Name “ProVMware” | Get-VMHost | Get-VMHostStorage -RescanAllHba
Show the actual count of the active paths per datastore.
# Start Get all Connected Hosts $myHosts = Get-VMHost | where {$_.ConnectionState -eq "Connected" -and $_.PowerState -eq "PoweredOn"} foreach($esx in $myHosts){ $hss = Get-View $esx.Extensiondata.ConfigManager.StorageSystem $lunTab = @{} $hss.StorageDeviceInfo.ScsiLun | %{ $lunTab.Add($_.Key,$_.CanonicalName) } $pathTab = @{} $hss.StorageDeviceInfo.MultipathInfo.Lun | %{ $pathState = @($_.Path | Group-Object -Property PathState | where {$_.Name -eq "active"} | Select -ExpandProperty Group) if($pathTab.ContainsKey($_.Lun)){ $pathTab[$_.Lun] += $pathState.Count } else{ $pathTab.Add($lunTab[$_.Lun],$pathState.Count) } } foreach($mount in ($hss.FileSystemVolumeInfo.MountInfo | where {$_.Volume.Type -eq "VMFS"})){ $mount.Volume.Extent | Select @{N="VMHost";E={$esx.Name}}, @{N="Datastore";E={$mount.Volume.Name}}, @{N="LUN";E={$_.DiskName}}, @{N="Active Paths";E={$pathTab[$_.DiskName]}} } }
Output:
VMHost Datastore LUN Active Paths ------ --------- --- ------------ esxa100.labo.lu SVC_DATASTORE_06 naa.60050768025186646800000000000386 2 esxa100.labo.lu Local-ESXA100 naa.600508b3301c5424e056c2fd39ab577b 1 esxa100.labo.lu SVC_DATASTORE_04 naa.600507680251866468000000000003af 2
Show the actual count of the paths per HBA.
# Start Get all Connected Hosts $myHosts = Get-VMHost | where {$_.ConnectionState -eq "Connected" -and $_.PowerState -eq "PoweredOn"} foreach($esx in $myHosts){ foreach($hba in (Get-VMHostHba -VMHost $esx -Type "FibreChannel")){ $target = ((Get-View $hba.VMhost).Config.StorageDevice.ScsiTopology.Adapter | where {$_.Adapter -eq $hba.Key}).Target $luns = Get-ScsiLun -Hba $hba -LunType "disk" -ErrorAction SilentlyContinue $nrPaths = ($target | %{$_.Lun.Count} | Measure-Object -Sum).Sum $props = [ordered]@{ VMHost = $esx.name HBA = $hba.Name Targets = $target.Count Devices = $luns.Count Paths = $nrPaths } New-Object PSObject -Property $props | ft -AutoSize } }
Output:
VMHost HBA Targets Devices Paths ------ --- ------- ------- ----- esxa111.labo.lu vmhba0 6 89 178
Or more compact
# Start Get all Connected Hosts $myHosts = Get-VMHost | where {$_.ConnectionState -eq "Connected" -and $_.PowerState -eq "PoweredOn"} foreach($esx in $myHosts){ foreach($hba in (Get-VMHostHba -VMHost $esx -Type "FibreChannel")){ $target = ((Get-View $hba.VMhost).Config.StorageDevice.ScsiTopology.Adapter | where {$_.Adapter -eq $hba.Key}).Target $luns = Get-ScsiLun -Hba $hba -LunType "disk" $nrPaths = ($target | %{$_.Lun.Count} | Measure-Object -Sum).Sum Write-Host $esx $hba.Device "Targets:" $target.Count "Devices:" $luns.Count "Paths:" $nrPaths } }
Output:
esxa100.labo.lu vmhba2 Targets: 2 Devices: 9 Paths: 18 esxa100.labo.lu vmhba3 Targets: 2 Devices: 9 Paths: 18 esxb101.labo.lu vmhba2 Targets: 2 Devices: 9 Paths: 18 esxb101.labo.lu vmhba3 Targets: 2 Devices: 9 Paths: 18
$Array = @() $Clusters = Get-Cluster | Sort Name ForEach ($Cluster in $Clusters){ $VmHosts = $Cluster | Get-VmHost | Where {$_.ConnectionState -eq “Connected”} | Sort Name ForEach ($VmHost in $VmHosts){ $Array += Get-VMHostNetworkAdapter -VMHost $VmHost.Name -VMKernel | Where {$_.VMotionEnabled -eq “True”} | select VmHost,IP } } $Array | Out-GridView
Script to remove 30 days old powered off vm guest from vCenter automatically and email user about it
$vms = Get-VM | where {$_.PowerState -eq "PoweredOff"} $vmPoweredOff = $vms | %{$_.Name} $events = Get-VIEvent -Start (Get-Date).AddDays(-30) -Entity $vms | where{$_.FullFormattedMessage -like "*is powered off"} $lastMonthVM = $events | %{$_.Vm.Name} $moreThan1Month = $vmPoweredOff | where {!($lastMonthVM -contains $_)} $moreThan1Month | Remove-VM -DeletePermanently -Confirm:$false $vmNames = $moreThan1Month | Select -ExpandProperty Name Send-MailMessage -From report@domain.com -To me@domain.com -SmtpServer mail@domain.com -Subject "Removed $($moreThan1Month.Count) VM" -Body ($vmNames | Out-String)