I currently have a script that executes in parallel the space reclaim of a VM disks, it basically generates the reclaim-script.sh for each disk:
foreach ($vm in $VMs) {
# Gets the disks of the VMs
$Disks = (Get-VM -Name $vm).ExtensionData.LayoutEx.File | Where-Object { $_.name -like '*-flat.vmdk' } | Select-Object Name | Split-Path -LeafBase | ForEach-Object { $_.split("-")[0] }
# Creates a sh script for the space reclaim of each disk of each VMs
$CurrentDS = Get-VM -Name $vm | Get-Datastore
$CurrentFolder = Get-VM -Name $vm | Select-Object -ExpandProperty Name
Write-Output "#! /bin/sh
# Alessandro PRANZO / Thick2Thin migration
cd /vmfs/volumes/$CurrentDS/$CurrentFolder" | Out-File src/reclaim/reclaim-$vm.sh
foreach ($disk in $Disks) {
Write-Output "vmkfstools -K $disk.vmdk" | Out-File src/reclaim/reclaim-$vm.sh -Append
}
}
Once the script that will execute the space reclaim is created, it will be copied via SCP to the remote ESXi host and executed remotely:
# Saves the content of /src/reclaim/ to a variable
$ReclaimScripts = (Get-ChildItem -Path "src/reclaim/*.sh").Name
$ReclaimScripts | ForEach-Object -Parallel {
$ESXiUser = $using:ESXiUser
$ESXiServer = $using:ESXiServer
# Copy the script reclaim-script.sh to ESXi host
scp src/reclaim/$_ $ESXiUser@${ESXiServer}:/tmp/
sleep 1
Get-Error
# Execute the script on the ESXi host
ssh $ESXiUser@$ESXiServer "sh ./tmp/$_"
}
This works just fine, the probles is that I receive back the output of all the 4 space reclaim at the same time, and not organized, so it looks like this:
Hole Punching: 73% done.
Hole Punching: 74% done.
Hole Punching: 75% done.
Hole Punching: 62% done.
Hole Punching: 76% done.
Hole Punching: 54% done.
Hole Punching: 77% done.
Hole Punching: 78% done.
Hole Punching: 79% done.
Hole Punching: 80% done.
Hole Punching: 63% done.
Hole Punching: 81% done.
Hole Punching: 82% done.
Hole Punching: 55% done.
Hole Punching: 83% done.
Hole Punching: 84% done.
Hole Punching: 85% done.
Hole Punching: 86% done.
Hole Punching: 64% done.
Hole Punching: 87% done.
Hole Punching: 56% done.
Hole Punching: 88% done.
Hole Punching: 89% done.
Hole Punching: 90% done.
Hole Punching: 91% done.
Hole Punching: 65% done.
Hole Punching: 92% done.
Hole Punching: 57% done.
Hole Punching: 93% done.
Hole Punching: 94% done.
Hole Punching: 95% done.
Hole Punching: 96% done.
Hole Punching: 66% done.
Hole Punching: 97% done.
Hole Punching: 58% done.
Hole Punching: 98% done.
Hole Punching: 99% done.
Hole Punching: 100% done.
After the try
Hole Punching: 67% done.
Hole Punching: 59% done.
Hole Punching: 68% done.
Pretty hard to tell on which disk it is executing the reclaim and if you check carefully the outputs are mixed for the 4, I wouldl ike to organize them like:
Space reclaim Disk1: 2% Space reclaim Disk3: 34% Space reclaim Disk2: 90%
Or at least have the advancment status on the same line.
I coudln't find any solution to try