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.
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.
Tags:
harddisks,
powershell