1

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

Dh3va
  • 41
  • 4
  • Where is this "Hole Punching: ..% done." coming from? Not seeing it in your code – Santiago Squarzon Feb 23 '23 at 13:11
  • 1
    The whole punching is probably coming from the call to `ssh $ESXiUser@$ESXiServer "sh ./tmp/$_"` Why don't you change this script and have it conform to your desired output? Alternatively - doing it this way would annoy me, redirect the call to ssh to a variable and print out the information that you need after the clean-up script has finished executing. – albvar Feb 23 '23 at 15:59
  • @albvar How could I change it? The var solution might work in effect. – Dh3va Feb 27 '23 at 10:58

0 Answers0