Report Guest Disk Sizes | PowerCLI

I already explained in my previous post how to grow/extend Guest OS disks with PowerCLI.

Now before we can increase harddrives we also need to identify them. I therefor created this script.

$MyCollection = @()
$AllVMs = Import-Csv -Header Desktop "hdd-check-input.csv" | foreach-object { Get-View -ViewType VirtualMachine -Filter @{"Name" = $_.Desktop}}
$SortedVMs = $AllVMs | Select *, @{N="NumDisks";E={@($_.Guest.Disk.Length)}} | Sort-Object -Descending NumDisks
ForEach ($VM in $SortedVMs){
 $Details = New-object PSObject
 $Details | Add-Member -Name Name -Value $VM.name -Membertype NoteProperty
 $DiskNum = 0
 Foreach ($disk in $VM.Guest.Disk){
 $Details | Add-Member -Name "Disk$($DiskNum)path" -MemberType NoteProperty -Value $Disk.DiskPath
 $Details | Add-Member -Name "Disk$($DiskNum)Capacity(MB)" -MemberType NoteProperty -Value ([math]::Round($disk.Capacity/ 1MB))
 $Details | Add-Member -Name "Disk$($DiskNum)FreeSpace(MB)" -MemberType NoteProperty -Value ([math]::Round($disk.FreeSpace / 1MB))
 $DiskNum++
 }
 $MyCollection += $Details
}
$MyCollection | export-csv hdd-check-output.csv
#Out-GridView
# Export-Csv, ConvertTo-Html or ConvertTo-Xml can be used above instead of Out-Gridview

The advantage using get-view is the nice performance if you run this script.

Try it out. You cant break things 🙂 It will give you a nice .csv as output.

View | Unassign User PowerCLI

If you dont want to unassign all users by hand simply provide this script a .csv file with desktop names and it will do it for you.

$filepath = "desktops.csv"
import-csv $filepath -Header Desktop | Foreach-object {
remove-userownership -machine_id (Get-DesktopVM -name $_.Desktop).machine_id
} 

 

Change & Grow VM Hard Disk

We are right now in the need of resizing all of our Hard Disks on the Virtual Desktop VMs from 40GB to 60GB.
I was looking quite some time to find the appropriate command and this is how you do it:

Get-HardDisk -vm "VMName" | where {$_.Name -eq "Hard Disk 1"} | Set-HardDisk –CapacityKB 62914560 -ResizeGuestPartition -Confirm:$false

A little bit of extra information (we ran into some issues performing this):

The second part of the script (Set-Harddisk) actually calls a library function that is using the “Invoke-VMScript” command.

You need to have following privileges on the vCenter System to execute it.
VirtualMachine.GuestOperations.Modify and VirtualMachine.GuestOperations.Execute.

vmware

Update:

This script makes it even more comfortable. You provide a list of desktops in the input file and it will do all of them in one run. After that you will see a report of the changes in the output file.

Import-Csv -Header Desktop "hdd-increase-input.csv" | foreach-object { Get-HardDisk -vm $_.Desktop | where {$_.Name -eq "Hard disk 1"} | Set-HardDisk –CapacityKB 62914560 -ResizeGuestPartition -Confirm:$false
Write-host "-----------------------------------------------------------------------------------------" -foregroundColor White -backgroundColor DarkGreen
} | export-csv "hdd-increase-output.csv"

New Job

Since the 1st of july i finally got into a very interesting position in my company.
I will now be responsible for support, reporting and analysis of a VMWare View virtual desktop infrastructure.
Starting off with a 90day training plan and a View 5.2 class room training i am looking forward to be on a good level pretty soon.