Sorting bits into bytes...

Deleting old vSAN partitions

In my current assignment I have to reuse “old” servers to build a new VMware Cloud Foundation platform. The servers already have a vSAN partition, which wasn’t removed before breaking down the old platform. But I guess nobody ever does delete the old vSAN partition, right?

 

I have to remove the vSAN partition on so many servers, that it doesn’t make sense to do this by hand. So why not write a Powershell script?

First create a csv file, in my example named vsanip.csv. Be sure to start with ip and then your ESXi servers management IPs:

ip
192.168.100.10
192.168.100.11
192.168.100.12
192.168.100.13
192.168.100.14

 

Use this script to clear the old vSAN partition.

<#
Author: Kabir Ali - info@kablog.nl
Scriptname: CVP (Clear vSAN Partition)
Version: 1.0 (Tested)
Date: Nov 29 2021
Why: Because clearing old vSAN partition data is slow and stupid

#>

<#
Example:
.\CVP.ps1 -InputCSV ".\vsanip.csv" -ESXiUser "root" -ESXiPass "VMware1!"

#>

Param (
    
    [Parameter(Mandatory = $true)][string]$InputCSV,
    [Parameter(Mandatory = $true)][string]$ESXiUser,
    [Parameter(Mandatory = $true)][string]$ESXiPass

)

# To SSH into ESXi this module is needed
if (Get-Module -ListAvailable -Name Posh-SSH) {
    Write-Host "Module exists"
} 
else {
    Write-Host "Module does not exist"
    Install-Module Posh-SSH
}



# Read Input CSV file
$vmhosts = import-csv $InputCSV

# Accept invalid SSL certificate from ESXi
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false | Out-Null

# Convert credentials to Secure String
$passwordsec = ConvertTo-SecureString -String $ESXiPass -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($ESXiUser,$passwordsec)

foreach ($vmhost in $vmhosts){
    # Connect to the ESXi host
    write-host "Connecting to ESXi host:" $vmhost.ip
    try {
        connect-viserver -server $vmhost.ip -user $user -password $password | Out-Null
    }
    Catch {
        $_.Exception.ToString()
        $error[0] | Format-List -Force
        Write-Host "Connecting to ESXi host: " $vmhost.ip "failed! Check ESXi host IP and credentials" -ForegroundColor Red
        Write-Error "Stopping!" -ErrorAction Stop
        break
    }
    
    # SSH to the ESXi host and get some vSAN data
    $ssh = new-sshsession -computername $vmhost.ip -credential $cred -acceptkey -keepaliveinterval 5
    $vsanstoragelistcommand = "esxcli vsan storage list"
    $vsanstoragelist = invoke-sshcommand -sessionid $ssh.sessionid -command $vsanstoragelistcommand -timeout 30
    
    # Build an array with the data return from the above command
    [array]$vsandata = @()
        foreach($vsanstoragedata in $vsanstoragelist.Output){
                $vsandata += New-Object PSObject -Property @{
                VSAN_Disk_Group_Name = $vsanstoragedata | Select-String -Pattern "VSAN Disk Group Name"
            }
        }
    Write-host "Found the following unique vSAN Disk Group Names:" -ForegroundColor Yellow
    $vsandata.VSAN_Disk_Group_Name | sort -Unique

    # In order to remove the disks the vSAN disk claim model must be set on manual
    $diskclaim = "esxcli vsan storage automode set --enabled false"
    try {
        invoke-sshcommand -sessionid $ssh.sessionid -command $diskclaim  -timeout 60 | Out-Null
        Write-Host "Changed vSAN disk claim settings to manual" -ForegroundColor Yellow
    }
    Catch {
        $_.Exception.ToString()
        $error[0] | Format-List -Force
        Write-Host "Changing mode failed on ESXi host: " $vmhost.ip -ForegroundColor Red
        Write-Error "Stopping!" -ErrorAction Stop
        break
    }

    # Mount the found disksgroups
    foreach($diskmount in ($vsandata.VSAN_Disk_Group_Name | sort -Unique)){
        $mountdisk = "esxcli vsan storage diskgroup mount -s "+ ($diskmount -replace "VSAN Disk Group Name: ").Split('',[System.StringSplitOptions]::RemoveEmptyEntries)
        try {
            invoke-sshcommand -sessionid $ssh.sessionid -command $mountdisk -timeout 60 | Out-Null
            Write-Host "Mounted VSAN Disk Group Name" $mountdisk -ForegroundColor Yellow
        }
        Catch {
            $_.Exception.ToString()
            $error[0] | Format-List -Force
            Write-Host "Mounting failed on ESXi host: " $vmhost.ip -ForegroundColor Red
            Write-Error "Stopping!" -ErrorAction Stop
            break
        }
    }

    # Remove the vSAN partitions
    foreach($diskuuid in ($vsandata.VSAN_Disk_Group_Name | sort -Unique)){
        $removevsanpartition = "esxcli vsan storage remove -s "+ ($diskuuid -replace "VSAN Disk Group Name: ").Split('',[System.StringSplitOptions]::RemoveEmptyEntries)
        try {
            invoke-sshcommand -sessionid $ssh.sessionid -command $removevsanpartition -timeout 30 | Out-Null
            Write-Host "Cleared VSAN Disk Group Name" $removevsanpartition -ForegroundColor Yellow
        }
        Catch {
            $_.Exception.ToString()
            $error[0] | Format-List -Force
            Write-Host "Clearing the partition failed on ESXi host: " $vmhost.ip -ForegroundColor Red
            Write-Error "Stopping!" -ErrorAction Stop
            break
        }
    }
    
    # Close SSH connection
    Write-Warning "Closing SSH connection"
    remove-sshsession -sessionid $ssh.sessionid

    # Disconnect ESXi host
    Write-Warning "Disconnecting ESXi host"
    disconnect-viserver -Server * -confirm:$false
}

 

I hope this helps!

Leave a Reply