VMware Cloud Community
Soji42
Contributor
Contributor

Any idea how to optimize this script/function please ?

Hello,

I wrote this function to get the inventory below on all VMs from VSAN.

pastedImage_0.png

The thing is ... it takes like 3 or 4 hours to complete a list of 1000 VM.

Do you know how I can optimize my script so it will proceed faster ?


cmdlet Get-VsanObject is only available in PowerCLI 11.

Function        vmStoragePolicy {

param([PSObject]$vmlist)

        $list = foreach ($vm in $vmlist){

                $vsan = Get-VsanObject -VM $vm | Select VM, Cluster, StoragePolicy, ComplianceStatus

                $da = Get-VM $vm | Get-Datastore

                [PSCustomObject]@{

                        "VM" = $vm

                        "Datastore" = $da.Name

                        "Cluster" = ($vsan.Cluster.Name | Out-String).Trim()

                        "Storage Policy" = ($vsan.StoragePolicy.Name | Out-String).Trim()

                }

        }

        return $list

}

$vmlist = Get-Content "/root/vmvsan.txt"

vmStoragePolicy -vm $vmlist | Export-CSV -Delimiter ";" -Path $file


Thanks for your help guys.

4 Replies
Soji42
Contributor
Contributor

Up Smiley Happy

Reply
0 Kudos
vijayku
VMware Employee
VMware Employee

Hi,

i think the issue here is two fold.

1. Get-VsanObject -VM $vm

     you are looking for the vsanobject associated with the supplied vm name, as no cluster is mentioned the search is performed at the vCenter level every time, which is time consuming.

2.  Get-VM $vm

     Again the same problem here.

i think using Get-View instead of using get-vm will reduce the time significantly. Will test the shared function and will post the results.

Soji42
Contributor
Contributor

vijayku​ Thank you for the idea but the request took 10 sec each Smiley Sad
I tried and got this:

FYI: $cl return a cluster name and $vm return a vm name

$> date;Get-VsanObject -cluster $cl -VM $vm;date

Fri Feb 22 12:51:21 UTC 2019

ComplianceStatus     HealthStatus    TimeOfCheck               StoragePolicy

----------------     ------------    -----------               -------------

compliant            healthy         2/22/19 12:51:29 PM       vSAN Storage Policy PF1 SF1...

compliant            healthy         2/22/19 12:51:29 PM       vSAN Storage Policy PF1 SF1...

compliant            healthy         2/22/19 12:51:29 PM       vSAN Storage Policy PF1 SF1...

compliant            healthy         2/22/19 12:51:29 PM       vSAN Storage Policy PF1 SF1...

compliant            healthy         2/22/19 12:51:29 PM       vSAN Storage Policy PF1 SF1...

compliant            healthy         2/22/19 12:51:29 PM       vSAN Storage Policy PF1 SF1...

compliant            healthy         2/22/19 12:51:29 PM       vSAN Storage Policy PF1 SF1...

Fri Feb 22 12:51:31 UTC 2019

-------------------------------------------------------------------------------------------------------------------

$> date;Get-VsanObject -VM $vm;date

Fri Feb 22 12:59:20 UTC 2019

ComplianceStatus     HealthStatus    TimeOfCheck               StoragePolicy

----------------     ------------    -----------               -------------

compliant            healthy         2/22/19 12:59:27 PM       vSAN Storage Policy PF1 SF1...

compliant            healthy         2/22/19 12:59:27 PM       vSAN Storage Policy PF1 SF1...

compliant            healthy         2/22/19 12:59:27 PM       vSAN Storage Policy PF1 SF1...

compliant            healthy         2/22/19 12:59:27 PM       vSAN Storage Policy PF1 SF1...

compliant            healthy         2/22/19 12:59:27 PM       vSAN Storage Policy PF1 SF1...

compliant            healthy         2/22/19 12:59:27 PM       vSAN Storage Policy PF1 SF1...

compliant            healthy         2/22/19 12:59:27 PM       vSAN Storage Policy PF1 SF1...

Fri Feb 22 12:59:30 UTC 2019

Reply
0 Kudos
Jasemccarty
Immortal
Immortal

@Soji42 a suggestion.

If you query vSAN for all of the objects first, it will place the entire list into the variable you designate:

(add this line)

$VsanObjects = Get-VsanObject <- If you're using multiple clusters

or

$VsanObjects = Get-VsanObject -Cluster (Get-Cluster -Name "ClusterName") <- If you're using a specific cluster

Then, instead of querying vSAN (again) for objects as they pertain to the current VM, just query against the variable:

(change this line)

$vsan = Get-VsanObject -VM $vm | Select VM, Cluster, StoragePolicy, ComplianceStatus

(to this line)

$vsan = $VsanObjects | Where-Object {$_.VM -eq $vm) | Select-Object VM, Cluster, StoragePolicy, ComplianceStatus

It could speed things up a bit.

More PowerCLI examples for vSAN can be found in my PowerCLI Cookbook for vSAN:
https://storagehub.vmware.com/section-assets/powercli-cookbook-for-vsan

Jase McCarty - @jasemccarty
Reply
0 Kudos