Automation

 View Only
Expand all | Collapse all

Get total number of disks configured on virtual VM

  • 1.  Get total number of disks configured on virtual VM

    Posted Feb 02, 2023 09:41 AM

    Hi

    I'm unable to find a command to collect the total number of disks and the controller configured on VM?

    Can you help me at least with some resources  



  • 2.  RE: Get total number of disks configured on virtual VM
    Best Answer

    Posted Feb 02, 2023 09:49 AM

    You mean something like this?

    Get-VM |
    Select Name,
      @{N='vDisk#';E={(Get-HardDisk -VM $_).Count}},
      @{N='Controller#';E={(Get-ScsiController -VM $_).Count}}
    


  • 3.  RE: Get total number of disks configured on virtual VM

    Posted Feb 02, 2023 12:55 PM

    I tried to add it to this script but I'm getting errors

    https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/vSAN-policy-in-different-datastores/m-p/2951782#M110494

    for sure you can help me but I would like to understand how I can find such information, because I'm checking this 2 references

    https://developer.vmware.com/docs/powercli/latest/products/vmwarevsphereandvsan/
    https://developer.vmware.com/apis/1355/vsphere

    For sure I'm doing something in wrong way

     

     



  • 4.  RE: Get total number of disks configured on virtual VM
    Best Answer

    Posted Feb 02, 2023 01:07 PM

    Did you try something like this?

    $report = @()
    foreach ($vm in Get-VM) {
      $view = Get-View $vm
      $hd = Get-HardDisk -VM $vm
      if ($view.config.hardware.Device.Backing.ThinProvisioned -eq $true) {
        $row = '' | Select-Object Name, Provisioned, Total, Used, VMDKs, VMDKsize, DiskUsed, Thin, StoragePolicy, 'vDisk#', 'Controller#'
        $row.Name = $vm.Name
        $row.Provisioned = [math]::round($vm.ProvisionedSpaceGB , 2)
        $row.Total = [math]::round(($view.config.hardware.Device | Measure-Object CapacityInKB -Sum).sum / 1048576 , 2)
        $row.Used = "{0:N2}GB" -f $vm.UsedSpaceGB
        $row.VMDKs = $view.config.hardware.Device.Backing.Filename | Out-String
        $row.VMDKsize = $view.config.hardware.Device | Where-Object { $_.GetType().name -eq 'VirtualDisk' } | ForEach-Object { "{0:N2}GB" -f (($_.capacityinKB) / 1MB) } | Out-String
        $row.DiskUsed = $vm.Extensiondata.Guest.Disk | ForEach-Object { "{0:N2}GB" -f (($_.Capacity - $_.FreeSpace) / 1GB) } | Out-String
        $row.Thin = $view.config.hardware.Device.Backing.ThinProvisioned | Out-String
        $row.StoragePolicy = (Get-SpbmEntityConfiguration -HardDisk $hd).StoragePolicy.Name | %{if($_ -eq $null){'Datastore Default'}else{$_}} | Out-String
        $row.'vDisk#' = $hd.Count
        $row.'Controller#' = (Get-ScsiController -VM $vm).Count
        $report += $row
      }
    }
    
    $report | Sort-Object Name | Export-Csv -Path "D:Thin_Disks.csv"
    

     



  • 5.  RE: Get total number of disks configured on virtual VM

    Posted Feb 02, 2023 02:39 PM

    As simple as that   Thank you

    Question : to have the output in separate line this , would you please help me with some clue, I would like to give a try

    lElOUCHE_79_0-1675348691281.png

     

     



  • 6.  RE: Get total number of disks configured on virtual VM

    Posted Feb 02, 2023 02:57 PM

    Instead of looking at al the Harddisks at once, I would loop over the Harddisks.
    Using a loop like this for example

    Get-VM -PipelineVariable vm | 
    Get-Harddisk -PipelineVariable hd |
    Foreach-Object -Process {
    
    # Create and populate the object for each Harddisk
    
    }


    The PipelineVariable parameter allows you to refer to the objects returned by Get-VM, via $vm, and Get-Harddisk, via $hd.

    Also, note that the Get-View is not really needed, you can access the same properties via for example $vm.ExtensionData and $hd.ExtensionData



  • 7.  RE: Get total number of disks configured on virtual VM

    Posted Feb 02, 2023 03:21 PM

    I will give atry and keep you posted

    I'm still  beginner 



  • 8.  RE: Get total number of disks configured on virtual VM

    Posted Feb 02, 2023 05:11 PM

    We were all beginners at one point in time 
    The best way to learn is by doing



  • 9.  RE: Get total number of disks configured on virtual VM

    Posted Feb 02, 2023 06:41 PM

    would you please assist me to find reference where I can find ExtensionData?

    I checked the link below

    https://developer.vmware.com/docs/powercli/latest/products/vmwarevsphereandvsan/



  • 10.  RE: Get total number of disks configured on virtual VM

    Posted Feb 02, 2023 07:02 PM

    Sure, everything that is available under ExtensionData are in fact the objects listed in the vSphere Web Services API.

    For example the content you get when you do

    $vm = Get-VM -Name MyVM
    $vm.ExtensionData

    is documented under the VirtualMachine entry.
    Similar for $esx.ExtensionData, $datastore.ExtensionData, $datacenter.ExtensionData, ...

    Reading the API Reference might take a while to get used to, but everything is in there.

    The objects returned by cmdlets that use a REST API method are documented in the vSphere Automation Reference



  • 11.  RE: Get total number of disks configured on virtual VM

    Posted Feb 02, 2023 07:45 PM

    I tried something like this, the good point is that I didn't received any error message but the result is not what's expected

          #################################
          #   vCheck Targeting Variables  # 
          ################################# 
          # Total number of hosts
          $TotalVMHosts = Get-VMHost
          $TotalVMHostsCount = $TotalVMHosts.count
          $CountHosts = $CountHosts + $TotalVMHostsCount
          Write-Host "There are $TotalVMHostsCount Hosts in $DefaultVIServer" -Foregroundcolor "Cyan"
          
          # Total number of VMs
          $TotalVMs = Get-VM
          $TotalVMsCount = $TotalVMs.count
          $CountVMs=$CountVMs + $TotalVMsCount
          Write-Host "There are $TotalVMsCount Virtual Machines in $DefaultVIServer" -Foregroundcolor "Cyan"
    
          ##############################
          # Gathering ESXi information #
          ##############################
          Write-Host "Gathering Disk Partition Storage Information"
          $Report = @() 
         
          $i=0
          ForEach ($VM in $TotalVMs){ 
            $i++
            Write-Progress -Activity "vCollecting VMs" -Status ("VM: {0}" -f $VM.Name) -PercentComplete ($i/$TotalVMsCount*100) -Id 1  -ParentId 0
            #Datacenter info  
            $datacenter = $vm | Get-Datacenter | Select-Object -ExpandProperty name 
            #vCenter Server  
            $vCenter = $vm.ExtensionData.Client.ServiceUrl.Split('/')[2].trimend(":443") 
            #Cluster info  
            $cluster = $vm | Get-Cluster | Select-Object -ExpandProperty name 
            
            # Total Drives
            $TotalDrives = $VM.ExtensionData.Guest.Disk
            $TotalDrivesCount = $TotalDrives.count
            $j=0
            ForEach ($Drive in $VM.ExtensionData.Guest.Disk){
                $j++
                Write-Progress -Activity "vCollecting Drives" -Status ("Drive: {0}" -f $Drive.DiskPath) -PercentComplete ($j/$TotalDrivesCount*100) -Id 2  -ParentId 1
                $Path = $Drive.DiskPath
                $Capacity   =   [math]::Round($Drive.Capacity/ 1GB)
                $Freespace  =   [math]::Round($Drive.FreeSpace / 1GB)
                $PercentFree=   [math]::Round(((100* ($Drive.FreeSpace))/ ($Drive.Capacity)),0)
    
                $Vmresult = New-Object PSObject   
                $Vmresult | add-member -MemberType NoteProperty -Name "datacenter"      -Value $datacenter  
                $Vmresult | add-member -MemberType NoteProperty -Name "vCenter Server"  -Value $vCenter  
                $Vmresult | add-member -MemberType NoteProperty -Name "Cluster"         -Value $cluster 
                $Vmresult | add-member -MemberType NoteProperty -Name "Host"            -Value $VM.VMHost 
                $Vmresult | add-member -MemberType NoteProperty -Name "VM Name"         -Value $VM.Name
                $Vmresult | add-member -MemberType NoteProperty -Name "Disk PATH"       -Value $Path
                $Vmresult | add-member -MemberType NoteProperty -Name "Capacity(GB)"    -Value $Capacity
                $Vmresult | add-member -MemberType NoteProperty -Name "Free Space(GB)"  -Value $Freespace
                $Vmresult | add-member -MemberType NoteProperty -Name "Free Space %"    -Value $PercentFree
                $report += $Vmresult       
             }
          } 

     

    The output for path for example it shot something like this */storage/autodeploy* and I was wondering that I will have the path for the datastore



  • 12.  RE: Get total number of disks configured on virtual VM

    Posted Feb 02, 2023 08:13 PM

    Everything under $vm.ExtensionData.Guest is information obtained by the VMware Tools from inside the Guest OS.
    The Guest OS has no knowledge whatsoever about the virtual environment, including datastores.

    That is one reason I suggested earlier to have a loop with Get-Harddisk.
    That will return vDisk information, including the Datastore



  • 13.  RE: Get total number of disks configured on virtual VM

    Posted Feb 02, 2023 08:22 PM

    Thanks I will try again