My script is running slow

This topic contains 1 reply, has 2 voices, and was last updated by  Darwin Reiswig 3 months ago.

  • Author
    Posts
  • #93693

    raghavendra
    Participant

    Hi
    The below script is running slow. Please help me.

    [CmdletBinding()]
    Param(
    [Parameter(Position =0,
    Mandatory=$true,
    ValueFromPipeLine=$true,
    ValueFromPipeLineByPropertyName=$true,
    HelpMessage = 'VCenter server to query',
    ParameterSetName = 'vCenterName')]
    [Alias('vCenter')]
    [String[]]$vCenterName,

    [Switch]$ShowProgress
    )

    foreach($vc in $vCenterName) {
    Connect-VIServer $vc
    }

    #Install-Module -Name ImportExcel -Credential -Force

    $fileName = "C::\script\VM-report1-new.xlsx"

    foreach($vc in $global:DefaultVIServers){

    $table = @()

    ForEach($VM in (Get-Cluster XXXX-Cluster | Get-VM)){

    $Snapshots = $VM | Get-Snapshot

    $Report = "" | Select-Object VMname,vmCreatedByUser,vmCreatedDate,ESXname,ClusterName,MemoryGB,vCPUcount,Folder,VMXname,VmdkSizeGB,DatastoreName,SnapshotCount,Owner,Pod_vmcount

    $Report.VMName = $VM.name

    $Report.vmCreatedByUser = Get-VIEvent $VM | sort createdTime | select -first 1 | select UserName | % {$_.UserName.split("\")[1]}

    $Report.vmCreatedDate = Get-VIEvent $VM | sort createdTime | select -first 1 | select CreatedTime

    $Report.ESXname = $VM.VMHost

    $Report.ClusterName = ($VM | Get-Cluster).Name

    $Report.MemoryGB = $VM.MemoryMB/1024

    $Report.vCPUcount = $VM.NumCpu | %{$_ -gt 4}

    $Report.Folder = $VM.Folder

    $Report.VMXname = $VM.ExtensionData.Config.Files.VmPathName.Split("/")[1]

    $Report.VmdkSizeGB = $VM.UsedSpaceGB

    $Report.DatastoreName = $VM.ExtensionDataConfig.DatastoreUrl

    $Report.SnapshotCount = ($VM | Get-Snapshot).Count

    $Report.Owner = $VM | Get-VIPermission | Where-Object {$_.Role -like "Lab Specific VM Folder*"} | select Principal | % {$_.Principal.split("\")[1]}

    $Report.Pod_vmcount = get-resourcePool -location *XXX* | select Name,@{N=“VMcount“;E={($_ |Get-VM).Count}} | Where-Object VMcount -eq 0

    $table += $Report

    }

    $table | Export-Excel -Path $fileName -WorkSheetname "$($vc.Name)"

    Thanks
    Ragahv

  • #93726

    Darwin Reiswig
    Participant

    It could be because you are connected to all of your VI Servers at the same time. One idea would be to Connect-VIServer for one server at a time, get the results for that server, then Disconnect-VIServer and move on to the next server.

    You might also look at Measure-Command to figure out which step in your process is slow, but if you have a large number of VMs on multiple clusters, it will probably take quite a while regardless.

You must be logged in to reply to this topic.