rpmello
Enthusiast
Enthusiast

Adding RDM Size to Custom Fields

Jump to solution

I've been trying out these scripts and found the snapshot size especially helpful as we are having a bit of a storage crisis, trying to reclaim all we can until we can buy more:

http://www.peetersonline.nl/index.php/vmware/add-custom-fields-to-vi-client-with-powershell-samples/

My goal is to add another custom field for RDM size, keeping in mind it must be a sum of all RDMs on the VM since some VMs have 2 or 3 RDMs.

The code I use to get RDM sizes is the following:

$capacity=0
get-datacenter "Systems Datacenter" | get-vm | get-harddisk | %{if($_.DiskType -ne "Flat"){
$capacity = $_.CapacityKB/1048576
$sum = $sum + $capacity
Write-Host $_.Filename $capacity GB
}
}

The issue is that this isn't very easy to plugin to the existing script. It seems like I need to do a select on a Get-View to do it properly, but I don't know of a way to get disk type and disk size from the Get-View. Is there a documented way to do this, or do I need to take an entirely different approach? I have a feeling I'm missing something obvious and just need to take a look at some docs...

Thanks

0 Kudos
1 Solution

Accepted Solutions
hugopeeters
Hot Shot
Hot Shot

rpmello,

It's easier than you think. The trick is to put the Get-View inside the loop, because you want to use $VM for the calculation instead of $VMView.

Here's how to do it:

##############################
# Script created by Hugo Peeters #
# http://www.peetersonline.nl   #
##############################

# Variables
$VCServerName = "MYVCSERVER"
$CustomFieldName = "RDM Size (GB)"
$ManagedObjectType = "VirtualMachine"

# Script
$VC = Connect-VIServer $VCServerName
$SI = Get-View ServiceInstance
$CFM = Get-View $SI.Content.CustomFieldsManager

$myCustomField = $CFM.Field | Where {$_.Name -eq $CustomFieldName}
If (!$myCustomField)
	{
	# Create Custom Field
	$FieldCopy = $CFM.Field[0]
	$CFM.AddCustomFieldDef($CustomFieldName,$ManagedObjectType,$FieldCopy.FieldDefPrivileges,$FieldCopy.FieldInstancePrivileges)
	}
	
# Fill Custom Fields
$VMs = Get-VM
ForEach ($VM in $VMs)
	{
	$RDMSize = [math]::Round((($VM | Get-HardDisk | Where {$_.DiskType -ne "flat"} | Measure-Object CapacityKB -Sum).Sum * 1KB / 1GB),0)
	If ($RDMSize )
		{
		$VMView = $VM | Get-View
		# Compare value to current value
		If ($RDMSize -ne ($VMView.CustomValue | ?{$_.Key -eq $myCustomField.Key}).Value)
			{
			# Set Custom Value
			$VMView.setCustomValue($CustomFieldName,$RDMSize )
			}
		}
	}
Disconnect-VIServer -Confirm:$False

Hug

http://www.peetersonline.nl

View solution in original post

0 Kudos
3 Replies
hugopeeters
Hot Shot
Hot Shot

rpmello,

It's easier than you think. The trick is to put the Get-View inside the loop, because you want to use $VM for the calculation instead of $VMView.

Here's how to do it:

##############################
# Script created by Hugo Peeters #
# http://www.peetersonline.nl   #
##############################

# Variables
$VCServerName = "MYVCSERVER"
$CustomFieldName = "RDM Size (GB)"
$ManagedObjectType = "VirtualMachine"

# Script
$VC = Connect-VIServer $VCServerName
$SI = Get-View ServiceInstance
$CFM = Get-View $SI.Content.CustomFieldsManager

$myCustomField = $CFM.Field | Where {$_.Name -eq $CustomFieldName}
If (!$myCustomField)
	{
	# Create Custom Field
	$FieldCopy = $CFM.Field[0]
	$CFM.AddCustomFieldDef($CustomFieldName,$ManagedObjectType,$FieldCopy.FieldDefPrivileges,$FieldCopy.FieldInstancePrivileges)
	}
	
# Fill Custom Fields
$VMs = Get-VM
ForEach ($VM in $VMs)
	{
	$RDMSize = [math]::Round((($VM | Get-HardDisk | Where {$_.DiskType -ne "flat"} | Measure-Object CapacityKB -Sum).Sum * 1KB / 1GB),0)
	If ($RDMSize )
		{
		$VMView = $VM | Get-View
		# Compare value to current value
		If ($RDMSize -ne ($VMView.CustomValue | ?{$_.Key -eq $myCustomField.Key}).Value)
			{
			# Set Custom Value
			$VMView.setCustomValue($CustomFieldName,$RDMSize )
			}
		}
	}
Disconnect-VIServer -Confirm:$False

Hug

http://www.peetersonline.nl

View solution in original post

0 Kudos
rpmello
Enthusiast
Enthusiast

Wow, very helpful, thanks... I am pretty new to powershell so I still need to learn the intricacies of some of these commands.

0 Kudos
hugopeeters
Hot Shot
Hot Shot

I recommend working through the Powershell Owners Manual to get the hang of the power of the object-oriented way Powershell works: http://www.microsoft.com/technet/scriptcenter/topics/winpsh/manual/default.mspx

That's where I started a year and a half ago.

Hugo

http://www.peetersonline.nl

0 Kudos