Monday, September 11, 2017

Virtual Machine VMDK report using PowerCLI

Virtual Machine VMDK report using PowerCLI

Script 1

$VMs = Get-VM *
$Data = @()
foreach ($VM in $VMs){
    $VMDKs = $VM | get-HardDisk
    foreach ($VMDK in $VMDKs) {
        if ($VMDK -ne $null){
            $CapacityGB = $VMDK.CapacityKB/1024/1024
            $CapacityGB = [int]$CapacityGB
            $into = New-Object PSObject
            Add-Member -InputObject $into -MemberType NoteProperty -Name VMname $VM.Name
            Add-Member -InputObject $into -MemberType NoteProperty -Name Datastore $VMDK.FileName.Split(']')[0].TrimStart('[')
            Add-Member -InputObject $into -MemberType NoteProperty -Name VMDK $VMDK.FileName.Split(']')[1].TrimStart('[')
            Add-Member -InputObject $into -MemberType NoteProperty -Name StorageFormat $VMDK.StorageFormat
            Add-Member -InputObject $into -MemberType NoteProperty -Name CapacityGB $CapacityGB
            $Data += $into
        }
    }
 }
$Data | Sort-Object VMname,Datastore,VMDK | Export-Csv -Path D:\scripts\VM-VMDKs.csv -NoTypeInformation

Output:


VMname Datastore VMDK StorageFormat CapacityGB
test3 datastore1  test3/test3.vmdk Thick 100
test3 datastore1  test3/test3_1.vmdk Thin 300
test4 datastore1  test4/test4.vmdk Thick 100
test4 datastore1  test4/test4_1.vmdk Thin 300


Script 2

$report = @()
foreach ($vm in Get-VM){
$view = Get-View $vm
if ($view.config.hardware.Device.Backing.ThinProvisioned -eq $true){
$row = '' | select Name, Provisioned, Total, Used, VMDKs, VMDKsize, DiskUsed, Thin
    $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 = [math]::round($vm.UsedSpaceGB , 2)
    $row.VMDKs = $view.config.hardware.Device.Backing.Filename | Out-String
    $row.VMDKsize = $view.config.hardware.Device | where {$_.GetType().name -eq 'VirtualDisk'} | ForEach-Object {($_.capacityinKB)/1048576} | Out-String
    $row.DiskUsed = $vm.Extensiondata.Guest.Disk | ForEach-Object {[math]::round( ($_.Capacity - $_.FreeSpace)/1048576/1024, 2 )} | Out-String
    $row.Thin = $view.config.hardware.Device.Backing.ThinProvisioned | Out-String
$report += $row
}}
$report | Sort Name | Export-Csv -Path "D:\Scripts\test\Thin_Disks.csv"

Output:

Name Provisioned Total Used VMDKs VMDKsize DiskUsed Thin
test3 432.19 400 102.38 [datastore1] test3/test3.vmdk

[datastore1] test3/test3_1.vmdk
100

300
0
False

True
test4 432.19 400 102.38 [datastore1] test4/test4.vmdk

[datastore1] test4/test4_1.vmdk
100

300
0
False

True

1 comment: