This week I need to create a script to list all VMs. This particular script display all VMs that are inside in some particularly iSCSI LUNs. Since VMware environment could have iSCSI, NFS and also Local Disks, so we need to search by CanonicalName. In this case iSCSI LUN CanonicalName.
Some examples of CanonicalName that we can search with Get-ScsiLun:
mpx.vmhba1:C0:T0:L0 – Local Storage
naa.600508b1001cd496d9b6de3cd17** – Local Disks
eui.0824c2ed87fe91f6 or eui.7d374ff1ab9a3558 – iSCSU LUNs
Note: If you want to exclude the Local Disks and/or Local Storage from the search, just add -and $_.IsLocal -like “False” in the Get-ScsiLun.
So by searching the CanonicalName, we can search and List all VMs inside. We can use CanonicalName for iSCSI, Local Disks, and Local Storage(system). In this case, we are only using for iSCSI LUNs.
This is the script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
######################################################################################### # Created by: Luciano Patrão / Vladimir Sokol # # Date: 26-02-2017 # # List VMs by Datastore and CanonicalName # ######################################################################################### ###### Credentials and vCenter connection ###### $server = "vcenterIP/DNSname" $user = "user" $pwd = "pass" $outFile = "pathforfile\datastores-vms.html" # This section is based on a example found in: http://www.vsysad.com/2013/07/powercli-script-to-get-naa-and-datastore-name-of-all-storage-on-esx-server/ # Create new property New-VIProperty -Name lunDatastoreName -ObjectType ScsiLun -Value { param($lun) $ds = $lun.VMHost.ExtensionData.Datastore | %{Get-View $_} | ` Where {$_.Summary.Type -eq "VMFS" -and ($_.Info.Vmfs.Extent | where {$_.DiskName -eq $lun.CanonicalName})} if($ds){ $ds.Name } } -Force -WarningAction SilentlyContinue | Out-Null ##### cls Connect-VIServer $server -User $user -Password $pwd $allHosts = Get-VMHost | where {($_.ConnectionState -like "Connected")} | Sort # Get all unique datastores sorted by canonical names Write-Host "`n" $allCanonical=@() foreach ($esxHost in $allHosts) { Write-Host "Analyzing host $esxHost" # Here is where we add the CanonicalName unique search (to exclude local disks add -and $_.IsLocal -like "False" ) $allCanonical += Get-ScsiLun -VmHost $esxHost | Where {$_.CanonicalName -like "naa.600*"} | Select CanonicalName, lunDatastoreName } $allCanonical = $allCanonical | Sort-Object -Property lunDatastoreName | Get-Unique -AsString Write-Host -ForegroundColor Green "All hosts analyzed.`n" # get VMs per each datastore $body = @() foreach ($dataStore in $allCanonical) { if ($dataStore.lunDatastoreName ) { Write-Host "Collecting VMs from dataStore=" $dataStore.lunDatastoreName $VMs = Get-VM -Datastore $dataStore.lunDatastoreName | Sort-Object $preText = "<h2>"+$datastore.lunDatastoreName + " - " + $datastore.CanonicalName + "</h2>" if ($VMs) { $report = $VMs | Select Name, @{N="vCPU";E={($_).NumCpu}}, @{N="Memory (GB)";E={($_).MemoryGB}} $body += $report | ConvertTo-Html -PreContent $preText } else { $body += "" | ConvertTo-Html -PreContent ($preText + "<h3>There is no VMs on this datastore</h3>") } } else { Write-Host "Canonical name" $datastore.CanonicalName "has no datastore name" } } Write-Host -ForegroundColor Green "All datastores analyzed.`n" # Create HTML report $Header = @" <style> TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse; font-family: arial, verdana, sans-serif} TR:Hover TD {Background-Color: #C1D5F8;} TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;} TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;} .odd { background-color:#ffffff; } .even { background-color:#dddddd; } .redtext {color: red;} H1,H2,H3,H4,H5,H6 {font-family: arial, verdana, sans-serif} p { font-size: 100%; font-family: arial, verdana, sans-serif } </style> <title> VM report </title> "@ ConvertTo-Html -Head $Header -Body $body | Out-File -FilePath $outFile Write-Host "`nHTML report is saved to $outFile" Disconnect-VIServer -Server $server -Force -Confirm:$false Write-Host -ForegroundColor Green "Done" |
The initial part of this script (the New-VIProperty parameter) was based on a script that we found in www.vsysad.com.
Note: Again many thanks to my colleague Vladimir Sokol for is scripting qualities for the help, improvement and also taking the script to a higher next level.
Hope this script can help and be useful.
Note: Share this article, if you think is worth sharing.
Leave A Comment