VMware

This Question is Not Answered

1 "correct" answer available (10 pts) 2 "helpful" answers available (6 pts)
3 Replies Last post: Nov 5, 2009 2:46 PM by Matthewq  

Looping through two arrays to find a match posted: Nov 4, 2009 12:26 PM

Click to view Matthewq's profile Lurker 3 posts since
Nov 23, 2005
HI, I am working on a migration at the moment. Almost every disk has to be resized, so I started writing some script.

I am in my second month of powershell'ing so still learning. I am having trouble matching two arrays.

Basically I collect disk metrics using an SQL statement from VC database. This array has the VM path, space used, total capacity.
From this I can create the first array ($DiskNewSizes) which contains, Existing HD size, new HD size based on a target free space percentage.
The second array ($vmDisks) is simply get-harddisk from vm.

Because this is no direct link (or I cant find it) between the logical view and physical view I and trying to match the disk based on existing capacity.
I am round the hard disk capacity and looking for a match. If the is one match, use set-harddisk to resize. If 0 or more than 1 you need to do it manually.

This partly work, but with more disk has issues. I realize my issue with this syntax is that I am incrementing (+=) the array before knowing if ($DiskNewSizes) has a match but I am running myself in circles trying to figure out how to do it?

Any suggestions would be great.

.... Write-Host 'Attempting Hard disk resize'
$DiskMatch = @()
foreach ($resize in $vmDisks) {
$DiskMatch = @()
$DiskMatch += $DiskNewSizes | where {Math::round($_.CurrentCapacity /100, 0) -eq Math::round(($resize.capacitykb /1024 ) /100, 0)}

if ($DiskMatch.count -eq 1) {
$NewSizeKB = $DiskMatch.NewCapacity * 1024
Write-Host 'Match found resizing' $resize.Name $diskmatch.path 'from' $resize.CapacityKB 'KB to' $NewSizeKB 'KB'
Set-HardDisk -HardDisk $resize -CapacityKB $NewSizeKB -whatif
}
elseif ($DiskMatch.count -gt 1) {
Write-Host "Can't match disks manual resize required"
}
}

Thanks
Matt.

Re: Looping through two arrays to find a match

1. Nov 4, 2009 11:43 PM in response to: Matthewq
Click to view harkamal's profile Hot Shot 221 posts since
Mar 19, 2009

You can use hashtable instead of array
$a = New-Object system.collections.hashtable
$a.Add("Disk1","12-3-21-")
$a.Add("Disk2","12-3-21-")
$a.Add("Disk3","12-3-21-")

$a.Contains("Disk2")
True


$a.Contains("Disk4")
False

VMware Developer

SDKs, APIs, Videos, Learn and much more in the Developer community.

Learn More

Developer Sample Code

Increase your developer productivity with VMware API sample code.

Learn More

VMworld Sessions & Labs

Online access to the latest VMworld Sessions & Labs and online services.

Learn more

Purchase PSO Credits Online

Purchase credits to redeem training and consulting services online.

Buy Now

Community Hardware Software

View reported configurations or report your own.

Learn More

VMware vSphere

Come witness the next giant leap in virtualization.

Register Today

Communities