VDP fix (POWERCLI): Virtual machine disks consolidation is needed - Unable to Access file since it is locked

Version 2

    Hi everyone!..

     

    Saturday at morning, I woke up... I see the email in my phone.. and I have a lot of alarms from vcenter server such as "Virtual machine disks consolidation is needed y Unable to Access file <unspecified filename> since it is locked".

    Seriously I dont know why VMWARE does not fix this kind of problems xD

    I made a simple script to create a easy workarround this Vsphere data protection very common problem.

     

    This script need a few imput data to work well.

     

    - At first place, download the code, paste into Powershell ISE and complete the variable declaration data like snmp/from/to email address.

    - Then, you need to run this script from powershell prompt, when script starts you need to provide a vcenter server IP address and the vsphere data protection appliance name.

    This step its very important because the vsphere data protection appliance name must be the SAME appliance name thats figure in vcenter server. (IE: "vsphere data protection 6.1")

    - Finally, the script will ask a virtual machine name thats have the locked file in data protection appliance.

     

    Running script:

    STEP 1: Verifying existence of virtual machine in your provided vcenter server. (if not exists... continue)

    STEP 2: Verifying existence of virtual machine snapshots. (if not exists... continue)

    STEP 3:Verifying existence of locked disk in vmware sphere data protection appliance. (if not exists... continue)

    STEP 4: Consolidate virtual machine. (Mandatory)

    If any of these steps fail, the script send you a email with error report.

     

    thank you all, I hope it is useful.

     

    Import-Module VMware.VimAutomation.Core -InformationAction SilentlyContinue
    clear
    #///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////PASO 1
    Write-Host "This is a script to fix locks on VDP appliance" -ForegroundColor Yellow -BackgroundColor Black
    sleep 3
    
    
    $smtp = "your SMTP server"
    $from = "vdp_fix@yourdomain.com"
    $to = "user@yourdomain.com"
    
    
    
    
    $Error.Clear()
    $vcenterserver = Read-Host "VCenter Server IP Address"
    $testconn = Test-Connection $vcenterserver -Count 1 -ErrorAction SilentlyContinue
    if($testconn){Write-Host "OK: Task ended without errors." -ForegroundColor DarkCyan
    Connect-VIServer $vcenterserver -InformationAction SilentlyContinue
    }else{Write-Host "ERROR: No VM found" -ForegroundColor Red -BackgroundColor DarkRed; exit}
    
    
    sleep 1
    
    
    $VDPname = Read-Host "vSphere Data Protection appliance name"
    Write-Host "Verifying existence of appliance..." -ForegroundColor DarkCyan
    if(get-vm $VDPname -ErrorAction SilentlyContinue){Write-Host "OK: Task ended without errors." -ForegroundColor DarkCyan}else{Write-Host "ERROR: No VM found, exiting." -ForegroundColor Red -BackgroundColor DarkRed ; sleep 3 ; exit}
    
    
    
    
    if($Error.Count -eq "0"){Write-Host "Starting." -ForegroundColor DarkCyan}else{
    Write-Host "ERROR: Process finalized with errors, sending email report." -ForegroundColor Red -BackgroundColor DarkRed
    $subject = "[ERROR] | Fix VDPLOCK | Input errors"
    send-MailMessage -SmtpServer $smtp -From $from -To $to -Subject $subject -Body "$Error" -BodyAsHtml
    exit}
    
    
    #///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////STEP 1
    #VM NAME INPUT
    write-host "STEP 1"  -ForegroundColor Cyan
    $vmname = Read-Host "Write VM name"
    Write-Host "Verifying existence of Virtual Machine..." -ForegroundColor DarkCyan
    if(get-vm $vmname -ErrorAction SilentlyContinue){Write-Host "OK: VM exist." -ForegroundColor DarkCyan}else{Write-Host "ERROR: VM does not exist, exiting." -ForegroundColor Red -BackgroundColor DarkRed ; sleep 3 ; exit}
    
    
    
    
    #///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////STEP 2
    #VERIFYING EXISTENCE OF SNAPSHOTS
    write-host "STEP 2"  -ForegroundColor Cyan
    Write-Host "Verifying existence of Snapshot..." -ForegroundColor DarkCyan
    $Error.Clear()
    if(get-vm $vmname | Get-Snapshot | Select-Object name -ExpandProperty name -ErrorAction SilentlyContinue)
    {Write-Host "OK: Snapshot exist." -ForegroundColor DarkCyan
    $ans = Read-Host "Do you want to delete this Snapshot? (Y/N)"
    if($ans -ne "Y" -and $ans -ne "N"){Write-Host "ERROR: Invalid option, exiting." -ForegroundColor red -BackgroundColor DarkRed; sleep 3 ;exit}else{
    if($ans -eq "N"){Write-Host "Exiting." -ForegroundColor Red -BackgroundColor DarkRed; sleep 3; exit}else{Write-Host "OK: Your selection is 'Y'." -ForegroundColor DarkCyan}}
    Write-Host "Deleting Snapshot." -ForegroundColor Yellow
    
    
    #DELETE SNAPSHOT
    $Error.Clear()
    $vmsnapshots = Get-Snapshot -VM $vmname | Remove-Snapshot -Confirm:$false -ErrorAction SilentlyContinue
    if($Error.Count -eq "0"){Write-Host "OK: Task ended without errors." -ForegroundColor DarkCyan}else{
    Write-Host "ERROR: Process finalized with errors, sending email report." -ForegroundColor Red -BackgroundColor DarkRed
    $subject = "[ERROR] | Fix VDPLOCK | Error removing snapshot"
    send-MailMessage -SmtpServer $smtp -From $from -To $to -Subject $subject -Body "$Error" -BodyAsHtml}
    $Error.Clear()}
    else{Write-Host "No snapshot found." -ForegroundColor Red -BackgroundColor DarkRed ; sleep 3}
    
    
    
    
    #///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////STEP 3
    #VERIFYING DISKS BLOCKS ON VDP 6.1
    write-host "STEP 3"  -ForegroundColor Cyan
    write-host "Verifying existence of locked disk..."  -ForegroundColor DarkCyan
    $Error.Clear()
    if($disk = Get-VM $VDPname | Get-HardDisk | where {($_.Filename -notmatch $VDPname) -and ($_.Filename -match "$vmname")})
    {Write-Host "OK: External disk found in VDP" -ForegroundColor DarkCyan
    $ans = Read-Host "Do you want to detach disk? (Y/N)"
    if($ans -ne "Y" -and $ans -ne "N"){Write-Host "ERROR: Invalid option." -ForegroundColor red -BackgroundColor DarkRed; exit}else{
    if($ans -eq "N"){Write-Host "Exiting." -ForegroundColor Red -BackgroundColor DarkRed; sleep 3; exit}else{Write-Host "OK: Your selection is 'Y'." -ForegroundColor DarkCyan}}
    
    
    #UNLOCKING DISK
    Write-Host "Unlocking disk." -ForegroundColor Yellow
    $Error.Clear()
    Get-VM $VDPname | Get-HardDisk | where {($_.Filename -notmatch $VDPname) -and ($_.Filename -match "$vmname")} | Remove-HardDisk -Confirm:$false -ErrorAction SilentlyContinue
    if($Error.Count -eq "0"){Write-Host "OK: Task ended without errors." -ForegroundColor DarkCyan}else{
    Write-Host "ERROR: Process finalized with errors, sending email report." -ForegroundColor Red -BackgroundColor DarkRed
    $subject = "[ERROR] | Fix VDPLOCK | Error removing disk locked on VDP 6.1"
    send-MailMessage -SmtpServer $smtp -From $from -To $to -Subject $subject -Body "$Error" -BodyAsHtml}
    $Error.Clear()}
    else{Write-Host "No disk found." -ForegroundColor Red -BackgroundColor DarkRed; sleep 3}
    
    
    
    
    #///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////STEP 4
    #VM CONSOLIDATION
    write-host "STEP 4"  -ForegroundColor Cyan
    write-host "Consolidating VM..."  -ForegroundColor DarkCyan
    sleep 6
    $Error.Clear()
    (Get-VM $vmname).ExtensionData.ConsolidateVMDisks()
    if($Error.Count -eq "0"){Write-Host "OK: Task ended without errors." -ForegroundColor Green}else{
    Write-Host "ERROR: Process finalized with errors, sending email report." -ForegroundColor Red -BackgroundColor DarkRed
    $subject = "[ERROR] | Fix VDPLOCK | Error consolidating VM"
    send-MailMessage -SmtpServer $smtp -From $from -To $to -Subject $subject -Body "$Error" -BodyAsHtml}
    $Error.Clear()
    write-host "Exiting."  -ForegroundColor yellow
    
    
    Disconnect-VIServer -Force -Confirm:$false -ErrorAction SilentlyContinue
    exit