Automation

 View Only
Expand all | Collapse all

To list Thick or Thin disk type in following script

  • 1.  To list Thick or Thin disk type in following script

    Posted Aug 30, 2022 03:52 PM

    I have following script which will list capacity, usage and free space for all VM disks. 

    Can you please help me to add one more column, listing what disk type, is this VM disk Thick or Thin? 

     

    Get-VM |
    Select-Object Name,
    @{N = 'Datastores'; E={(Get-Datastore -VM $_).Name -join '|'}},
    @{N = "GuestCapacityGB"; E = { [math]::Round(($_.Guest.Disks | ForEach-Object { $_.Capacity / 1GB } | Measure-Object -Sum | Select-Object -ExpandProperty Sum), 2) } },
    @{N = "GuestUsageGB"; E = { [math]::Round(($_.Guest.Disks | ForEach-Object { $_.CapacityGB - $_.FreeSpaceGB } | Measure-Object -Sum | Select-Object -ExpandProperty Sum), 1) } },
    @{N = "FreeSpaceGB"; E = { [math]::Round(($_.Guest.Disks | ForEach-Object { $_.FreeSpace / 1GB } | Measure-Object -Sum | Select-Object -ExpandProperty Sum), 2) } },
    @{N = "GuestUsage(%)"; E = {
    $capacityGB = ($_.Guest.Disks | Measure-Object -Property CapacityGB -Sum).Sum
    $freeGB = ($_.Guest.Disks | Measure-Object -Property FreeSpaceGB -Sum).Sum
    [math]::Round((($capacityGB - $freeGB) / $capacityGB * 100), 2) }
    } |
    Sort-Object -Property "GuestUsage(%)" |
    Export-Csv -Path c:\temp\report.csv -NoTypeInformation -UseCulture



  • 2.  RE: To list Thick or Thin disk type in following script

    Posted Aug 30, 2022 04:08 PM

    Provided you are complying with the requirements for the Get-VMGuestDisk cmdlet, you could do

    @{N='HDType';E={(Get-VMGuestDisk -VM $_ | Get-HardDisk).StorageFormat -join '|' }} |
    

     



  • 3.  RE: To list Thick or Thin disk type in following script

    Posted Aug 30, 2022 04:16 PM

    Could you please add the new line into the script above?



  • 4.  RE: To list Thick or Thin disk type in following script

    Posted Aug 30, 2022 04:20 PM

    Sure, but it's just a calculated property like the others you have in there.

    Get-VM |
    Select-Object Name,
    @{N = 'Datastores'; E = { (Get-Datastore -VM $_).Name -join '|' } },
    @{N = "GuestCapacityGB"; E = { [math]::Round(($_.Guest.Disks | ForEach-Object { $_.Capacity / 1GB } | Measure-Object -Sum | Select-Object -ExpandProperty Sum), 2) } },
    @{N = "GuestUsageGB"; E = { [math]::Round(($_.Guest.Disks | ForEach-Object { $_.CapacityGB - $_.FreeSpaceGB } | Measure-Object -Sum | Select-Object -ExpandProperty Sum), 1) } },
    @{N = "FreeSpaceGB"; E = { [math]::Round(($_.Guest.Disks | ForEach-Object { $_.FreeSpace / 1GB } | Measure-Object -Sum | Select-Object -ExpandProperty Sum), 2) } },
    @{N = "GuestUsage(%)"; E = {
        $capacityGB = ($_.Guest.Disks | Measure-Object -Property CapacityGB -Sum).Sum
        $freeGB = ($_.Guest.Disks | Measure-Object -Property FreeSpaceGB -Sum).Sum
        [math]::Round((($capacityGB - $freeGB) / $capacityGB * 100), 2) }
    },
    @{N = 'HDType'; E = { (Get-VMGuestDisk -VM $_ | Get-HardDisk).StorageFormat -join '|' }} |
    Sort-Object -Property "GuestUsage(%)" |
    Export-Csv -Path c:\temp\report.csv -NoTypeInformation -UseCulture


  • 5.  RE: To list Thick or Thin disk type in following script

    Posted Aug 30, 2022 07:48 PM

    Would like to ask you for one more help.

    Based on the script, can you please separate VM disks from each VM's, list all individual VM disks including capacity, usage and free space along with disk format on each one of VM? We have just upgraded vSphere to version 7. 

    Thank you for all your help.



  • 6.  RE: To list Thick or Thin disk type in following script

    Posted Aug 30, 2022 08:25 PM

    Just to be sure, that will be the view from the vSphere side, i.e. Harddisks.
    Not partitions inside the Guest OS, as your original script is doing



  • 7.  RE: To list Thick or Thin disk type in following script

    Posted Aug 30, 2022 08:45 PM

    I  honestly don't know the differences between these two. I  thought they are same.

    Anyway, the purpose of the script is to list how much each VM disks has been really used by data. In Thick format, if the capacity is 500GB, for instance, and the real usage only 10GB. So, I have about 450GB over provisioned, in this case, I wanted to cut the provisioned size down. But, I would like to list all VM disks, Thick or Thin



  • 8.  RE: To list Thick or Thin disk type in following script

    Posted Aug 30, 2022 09:13 PM

    That sounds like you want something like the report I posted about in yadr – A vdisk reporter



  • 9.  RE: To list Thick or Thin disk type in following script

    Posted Aug 31, 2022 02:09 PM

    I tried your script, but got the following errors. Is this because API 4.0 missed? Do I need to add extra line in your script in order to invoke API 4.0?
    I have gone to Developer Center and select the API Explorer tab, from there, there are quite a lot of things listed, how do I check if API 4.0 is installed or not?

    Cannot index into a null array.
    At line:38 char:1
    + $vmdkName = $vm.LayoutEx.File[$diskFiles[0]].Name
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray



  • 10.  RE: To list Thick or Thin disk type in following script

    Posted Aug 31, 2022 02:18 PM

    I assume you are running this on vSphere 6.5 or beyond?
    Then API 4.0 should be no problem.

    Is this returning any VMs?

    Get-View -ViewType VirtualMachine

    Are there VMs without Harddisk?



  • 11.  RE: To list Thick or Thin disk type in following script

    Posted Aug 31, 2022 02:24 PM

    We are running vSphere 7.0, one before the latest version.

    Get-View -ViewType VirtualMachine

    the command above works fine, and I got a long list of outputs. please see below as just one of a lots. 

    How would I know if I have a vm without harddisks? There are more than 2000 VM's here.

    ...

    Capability : VMware.Vim.VirtualMachineCapability
    Config : VMware.Vim.VirtualMachineConfigInfo
    Layout : VMware.Vim.VirtualMachineFileLayout
    LayoutEx : VMware.Vim.VirtualMachineFileLayoutEx
    Storage : VMware.Vim.VirtualMachineStorageInfo
    EnvironmentBrowser : EnvironmentBrowser-envbrowser-2083
    ResourcePool : VirtualApp-resgroup-v2646
    ParentVApp : VirtualApp-resgroup-v2646
    ResourceConfig : VMware.Vim.ResourceConfigSpec
    Runtime : VMware.Vim.VirtualMachineRuntimeInfo
    Guest : VMware.Vim.GuestInfo
    Summary : VMware.Vim.VirtualMachineSummary
    Datastore : {Datastore-datastore-180}
    Network : {DistributedVirtualPortgroup-dvportgroup-35}
    Snapshot :
    RootSnapshot : {}
    GuestHeartbeatStatus : gray
    LinkedView :
    Parent :
    CustomValue : {101}
    OverallStatus : green
    ConfigStatus : green
    ConfigIssue : {}
    EffectiveRole : {-1}
    Permission : {}
    Name : vmname.abc.com
    DisabledMethod : {MakePrimaryVM_Task, TerminateFaultTolerantVM_Task, ResetVM_Task, UnmountToolsInstaller...}
    RecentTask : {}
    DeclaredAlarmState : {alarm-10.vm-2083, alarm-1002.vm-2083, alarm-11.vm-2083, alarm-1703.vm-2083...}
    TriggeredAlarmState : {}
    AlarmActionsEnabled : True
    Tag : {}
    Value : {101}
    AvailableField : {Application/System, Availability, Compute, FA.GosAgent...}
    MoRef : VirtualMachine-vm-2083
    Client : VMware.Vim.VimClientImpl

    ....

    Thank you!



  • 12.  RE: To list Thick or Thin disk type in following script

    Posted Aug 31, 2022 03:44 PM

    Can you check if there are any VM with no Harddisks or even no Files (vCenter ghost entries).
    Check if this returns anything

     

     

    Get-View -ViewType VirtualMachine |
    Where{$_.LayoutEx.File.Count -eq 0 -or 
       $_.Config.Hardware.Device.where{$_.DeviceInfo.Label -like "Hard disk *"}.Count -eq 0} |
    Select Name,
     @{N='Files';E={$_.LayoutEx.File.Count}},
     @{N='HD';E={$_.Config.Hardware.Device.where{$_.DeviceInfo.Label -like "Hard disk *"}.Count}}

     

     

     



  • 13.  RE: To list Thick or Thin disk type in following script

    Posted Aug 31, 2022 03:47 PM

    Returned following errors:

     

    At line:2 char:34
    + Where{$_.LayoutEx.Files.Count -eq -or
    + ~
    You must provide a value expression following the '-eq' operator.
    + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ExpectedValueExpression



  • 14.  RE: To list Thick or Thin disk type in following script

    Posted Aug 31, 2022 03:50 PM

    There was a 0 missing.
    I corrected the code above



  • 15.  RE: To list Thick or Thin disk type in following script

    Posted Aug 31, 2022 04:09 PM

    Yes, there are 3 VM's with 0 on HD. 

    Returned 0's on FILES for all VM's. 

    Should we do anything to your original script then?

     

     



  • 16.  RE: To list Thick or Thin disk type in following script

    Posted Aug 31, 2022 04:12 PM

    You could change line 13 to

    $vms = Get-View -ViewType VirtualMachine | where{$_.LayoutEx.Files.Count -ne 0}


  • 17.  RE: To list Thick or Thin disk type in following script

    Posted Aug 31, 2022 04:30 PM

    Great, Thanks!

    I do have a few more questions, if you can please help me out:


    1. For Thin disks, "AllocatedMB" would be really physical space occupied in Datastore. Correct?


    2. For those with FALSE on both Thin and RDM columns, could they then considered to be Thick type of disks?
    If yes, The "AllocataedMB"  and CapacityMB are all the same. Is there anyway I can tell how much space really being used by the data? because if it is too small, I wanted to cut down the provisioned size(CapacityMB). 



  • 18.  RE: To list Thick or Thin disk type in following script

    Posted Aug 31, 2022 04:49 PM

    By the way, after I replaced line 13 with your new line, it returned following errors:

    Add-Type : Cannot add type. The type name 'DiskInfo' already exists.
    At line:19 char:1
    + Add-Type -Language CsharpVersion3 -TypeDefinition $DiskInfoDef
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (DiskInfo:String) [Add-Type], Exception
    + FullyQualifiedErrorId : TYPE_ALREADY_EXISTS,Microsoft.PowerShell.Commands.AddTypeCommand



  • 19.  RE: To list Thick or Thin disk type in following script

    Posted Aug 31, 2022 04:50 PM

    That's a known issue with C# types.
    You can't remove types defined this way.

    You'll have to stop/start your PS session I'm afraid.



  • 20.  RE: To list Thick or Thin disk type in following script

    Posted Aug 31, 2022 05:12 PM

    I closed out and restarted a new POWERSHELL session. This time there are no any errors, but nothing were put in the output file, just a empty file. Any idea?



  • 21.  RE: To list Thick or Thin disk type in following script

    Posted Aug 31, 2022 05:31 PM

    Yes, there was a typo in the Where-clause (Files instead of File)
    It should have been

     

    $vms = Get-View -ViewType VirtualMachine | where{$_.LayoutEx.File.Count -ne 0}


    You'll have to re-run the previous snippet as well, it had the same typo

     

     



  • 22.  RE: To list Thick or Thin disk type in following script

    Posted Aug 31, 2022 06:58 PM

    I got the result and the file, but still got the following errors. That's alright, it is probably due to 0 HD on a few VMs. 
    Would you please let me know questions I post earlier at 9:29am? I really appreciate your time. Those questions are important to me. Thank you!

     

     

     

    Cannot index into a null array.
    At line:38 char:1
    + $vmdkName = $vm.LayoutEx.File[$diskFiles[0]].Name
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray



  • 23.  RE: To list Thick or Thin disk type in following script

    Posted Aug 31, 2022 07:03 PM

    1. Yes, correct

    2. Yes, these are Thick HD
    I'm afraid you will have to go inside the Guest OS to check what is actually used.
    That's where the Get-VMGuestDisk with the Harddisk parameter could be used.
    But be aware that a HD can contain more than 1 partition inside the Guest OS.
    It can even contain hidden partitions.



  • 24.  RE: To list Thick or Thin disk type in following script

    Posted Sep 06, 2022 06:06 PM
    @{N='HDType';E={(Get-VMGuestDisk -VM $_ | Get-HardDisk).StorageFormat -join '|' }} |

     

     just by the line itself seems not working. Would you please complete the script?

     

    Thank you!



  • 25.  RE: To list Thick or Thin disk type in following script

    Posted Sep 06, 2022 06:11 PM

    No, you have to insert that line with the other calculated properties on the Select-Object cmdlet.

    Get-VM |
        Select-Object Name,
        @{N = 'Datastores'; E = { (Get-Datastore -VM $_).Name -join '|' } },
        @{N = 'HDType'; E = { (Get-VMGuestDisk -VM $_ | Get-HardDisk).StorageFormat -join '|' } },
        @{N = "GuestCapacityGB"; E = { [math]::Round(($_.Guest.Disks | ForEach-Object { $_.Capacity / 1GB } | Measure-Object -Sum | Select-Object -ExpandProperty Sum), 2) } },
        @{N = "GuestUsageGB"; E = { [math]::Round(($_.Guest.Disks | ForEach-Object { $_.CapacityGB - $_.FreeSpaceGB } | Measure-Object -Sum | Select-Object -ExpandProperty Sum), 1) } },
        @{N = "FreeSpaceGB"; E = { [math]::Round(($_.Guest.Disks | ForEach-Object { $_.FreeSpace / 1GB } | Measure-Object -Sum | Select-Object -ExpandProperty Sum), 2) } },
        @{N = "GuestUsage(%)"; E = {
                $capacityGB = ($_.Guest.Disks | Measure-Object -Property CapacityGB -Sum).Sum
                $freeGB = ($_.Guest.Disks | Measure-Object -Property FreeSpaceGB -Sum).Sum
                [math]::Round((($capacityGB - $freeGB) / $capacityGB * 100), 2) }
        } |
        Sort-Object -Property "GuestUsage(%)" |
        Export-Csv -Path c:\temp\report.csv -NoTypeInformation -UseCulture