VMware Cloud Community
RR9
Enthusiast
Enthusiast

How to find Base/original disk of VM which is running from snapshot?

in our backup solution..

  1. client VM's snapshot will be taken and VM's base disk will be attached to backup proxy nodes.

After backup completes VM's base disk will be detached from backup proxy nodes..but sometime base disk remains in backup proxy nodes.

so i am looking for script where i want to find base/original disks of $vm and compare it with $backupnode and remove that disk from virtual machine $backupnode.

But problem here is $vm has snapshot and i am not able to get the base/original hard disk name... could someone help me on this?

$vm="abc"

$backupnode="xyz"

regards,

Ranjit

13 Replies
LucD
Leadership
Leadership

This should list the harddisks that do not belong to the backup VM.

$backupVM = "backupVM"

$vm = Get-VM -Name $backupVM
$vm.ExtensionData.Layout.Disk | %{
 
$_.DiskFile | where {$_ -notmatch $backupVM}
}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

RR9
Enthusiast
Enthusiast

LucD ..thanks a lot for your reply..i was struggling for this to work....

$bkpserver="xyz"------------this is my backup proxy node

$backupclient = "abc" ----- VM whose backup is completed. It is running from snapshot and its base disk is still attached in "xyz" ---NOW --I want to remove these base disks of "abc" from "xyz"

----------------------------------------------------------------------------------

$bkpserver="xyz"

$backupclient = "abc"

$vm = Get-VM -Name $bkpserver

$a = $vm.ExtensionData.Layout.Disk | %{

  $_.DiskFile | where {$_ -match $backupclient}

}

----------------------------------------------------------------------------------

with above code i am getting list of disks need to be removed from "xyz" but if i use  "$a | Remove-HardDisk -Confirm:$false" in above code it returns below error...

Remove-HardDisk : The input object cannot be bound to any parameters for the co

mmand either because the command does not take pipeline input or the input and

its properties do not match any of the parameters that take pipeline input.

Reply
0 Kudos
LucD
Leadership
Leadership

The Remove-Harddisk cmdlet requires a Harddisk object as input.

Try it like this

$bkpserver="xyz"
$backupclient = "abc"


Get-VM -Name $bkpserver | Get-HardDisk |
where {$_.Filename -match $backupclient } |
Remove-HardDisk -Confirm:$false


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

RR9
Enthusiast
Enthusiast

thanks for your help... worked... thanks again...

current code..

$bkpserver="xyz"

$backupclient = Get-VM | where {$_.ExtensionData.Runtime.consolidationNeeded}

$vm = Get-VM -Name $bkpserver

Get-VM -Name $bkpserver | Get-HardDisk |

where {$_.Filename -match $backupclient } |

Remove-HardDisk -Confirm:$false

Get-VM |

Where-Object {$_.Extensiondata.Runtime.ConsolidationNeeded} |

ForEach-Object {

  $_.ExtensionData.ConsolidateVMDisks()

}

Reply
0 Kudos
RR9
Enthusiast
Enthusiast

Hi LucD,

i am following resolution in KB... http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=203353...

current code..

---------------------- ------------------------------

$bkpserver=get-vm Test**

$backupclient = Get-VM | where {$_.ExtensionData.Runtime.consolidationNeeded}

Get-VM -Name $bkpserver | Get-HardDisk |

where {$_.Filename -match $backupclient } |

Remove-HardDisk -Confirm:$false

Get-VM |

Where-Object {$_.Extensiondata.Runtime.ConsolidationNeeded} |

ForEach-Object {

  $_.ExtensionData.ConsolidateVMDisks()

}

---------------------- ------------------------------

Note:--

$bkpserver will be multiple VMs and base/original disks of $backupclient VMs could be attached to any of the $bkpserver

$backupclient can be multiple VMs and these VMs are running from snapshot

IN my current code

Get-VM -Name $bkpserver | Get-HardDisk |

where {$_.Filename -match $backupclient }

this doesn't return any list of disk...

Reply
0 Kudos
LucD
Leadership
Leadership

Isn't that because you only look at VMs that need consolidation ?

A VM that has a harddisk mounted on the backup server will not automatically need consolidation afaik.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
RR9
Enthusiast
Enthusiast

I had checked manually ... $backupclient = i was having 4 VMs which needed consolidation.These 4 VMs were running from snapshot disks (-0000**.vmdk)  and these 4 VMs base disks were mounted on VMs in $bkpserver .

Note:-

If i run $backupclient | get-harddisk ..then it returns -0000**.vmdk disks

In script ..i want check base disks of VMs in $backupclient which are attached to $bkpserver and then remove these base disks

Reply
0 Kudos
LucD
Leadership
Leadership

On the client you will indeed see the snapshot for its harddisks.

You should check on the $bkpserver if any of these is mounted.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
RR9
Enthusiast
Enthusiast

backup nodes remove client snapshots..but $backupclient | get-snapshots is null as snapshots are removed but $backupclient vms continue to run from snapshot disks (-000001.vmdk)

base/original disks of $backupclient remain mounted on $bkpserver vms

so we want to compare disks of $backclient to $bkpserver

$backupclient disks are like datastore/test-000001.vmdk

$bkpserver disks are like datastore/test.vmdk

here how to compare,list and remove these base disks from $bkpserver using powercli

we want to compare actual filenames so that there should not be any errors... please suggest if have any solution or workaround to this issue.

Reply
0 Kudos
RR9
Enthusiast
Enthusiast

script is completed to perform steps described in KB ...  kb.vmware.com/kb/2033537

...Luc thanks for your help.

if anyone find any error ,please highlight

Script does following tasks:-

1. Connect to vCenter server

2. get list of VMs which need consolidation

3. Find datastore folder path of ncvm

4. Find datastore name of ncvm

5. Find VMDK filenames of ncvm from /datastore/folder

6. Find leftover disks on backup proxy nodes

7. Remove leftover disks from backup proxy nodes

8. Consolidate VMs

9. Disconnect VIserver

10. send mail

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

#################################################### Start ########################################

add-pssnapin VMware.VimAutomation.Core

$vcenter1 = ""

#######################################backup server proxy nodes VM names are starting from "test" e.g. test01 , test02################

$bkpserver = get-vm test*

Connect-VIServer -Server $vcenter1

#############

# Variables #

#############

$date=(get-date).ToString('dd-MM-yyyy')

$MH = @"

<style>

TABLE {border-width: 1px;border-style: solid;border-color: black;}

TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: Gold;}

TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}

</style>

"@

##################

# Mail variables #

##################

$enablemail="yes"

$smtpServer = ""

$mailfrom = ""

$mailto = ""

##############################################script start##########################################

#######################get list of VMs which need consolidation#####################################

$ncvms = get-vm | where {$_.ExtensionData.Runtime.consolidationNeeded}

$ncvmsnames= $ncvms | select Name

$ncvmsnameshtml= $ncvmsnames | ConvertTo-Html –title "$vcenter1 VMs needed consolidation" –body "<H2>$vcenter1 VMs needed consolidation</H2>" -head $MH

if ($ncvms -ne $null)

{

#######################################Find datastore folder path of ncvms##########################

foreach ($ncvm in ($ncvms))

{

$ncvmdisks = get-vm $ncvm | get-harddisk

$ncvmdisksoutstr = $ncvmdisks | select Filename |Out-String

$c= $ncvmdisksoutstr.Substring($ncvmdisksoutstr.LastIndexOf('--------')+8)

$pos = $c.IndexOf("/")

$leftPart = $c.Substring(0, $pos+1)

$rightPart = $c.Substring($pos+1)

$dspath=$leftPart.trimstart()

#######################################Find datastore name path of ncvms##########################

$vmdisk = Get-VM $ncvm | Get-harddisk

$vmdiskDatastores=($vmdisk | %{$_.Filename.Split('[]')[1]})

#$vmdiskfilename=($vmdisk | %{$_.Filename.Split('[]')[2]})

#######################################Find VMDK filenames of ncvms###############################

$ncvmdsdiskfiles=foreach ($vmdiskDatastore in ($vmdiskDatastores)) {Get-HardDisk -Datastore $vmdiskDatastore -DatastorePath $dspath}

#######################################Find leftover disks on backup proxy nodes##################

$leftoverdisks += foreach ($ncvmdsdiskfile in ($ncvmdsdiskfiles))

{

$bkpserver| get-harddisk |sort |where{ ForEach-Object{$_.Filename -eq $ncvmdsdiskfile.Filename} }

}

}

}

#######################################Remove leftover disks from backup proxy nodes##############

$leftoverdisktable = $leftoverdisks | select Parent,Filename,Name

$leftoverdisktablehtml = $leftoverdisktable | ConvertTo-Html –title "$vcenter1 VADP leftover disks details" –body "<H2>$vcenter1 VADP leftover disks details.</H2>" -head $MH

$leftoverdisks | remove-harddisk -Confirm:$false

#################################################### Consolidate VMs  ##########################

Get-VM $ncvms |

ForEach-Object {

  $_.ExtensionData.ConsolidateVMDisks()

}

#################################################### Disconnect VIserver##########################

disconnect-viserver $vcenter1 -force -confirm:$false

######################

# E-mail HTML output #

######################

if ($enablemail -match "yes")

{

$msg = new-object Net.Mail.MailMessage

#$att = new-object Net.Mail.Attachment($filelocation)

$smtp = new-object Net.Mail.SmtpClient($smtpServer)

$msg.From = $mailfrom

$msg.To.Add($mailto)

$msg.Subject = “VADP leftover Disk removal & Consolidation Report”

$msg.IsBodyHTML = $true

$msg.Body = $ncvmsnameshtml

$msg.Body +=$leftoverdisktablehtml

#$msg.Attachments.Add($att)

#$msg.Attachments.Add($att1)

$smtp.Send($msg)

}

#################################################### End ########################################

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Reply
0 Kudos
Bunty11
Hot Shot
Hot Shot

We get this error

Remove-HardDisk : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.

Reply
0 Kudos
Bunty11
Hot Shot
Hot Shot

Remove-HardDisk : Cannot bind parameter 'HardDisk'. Cannot convert the "[XXX] XX.XX.XX/XX.XX.XX.vmdk" value of type

"System.String" to type "VMware.VimAutomation.ViCore.Types.V1.VirtualDevice.HardDisk".

Reply
0 Kudos
LucD
Leadership
Leadership

Not sure what you trying to do, but the filenames that are returned are just the VMDK files that are not attached to a VM.

Meaning the Get-HardDisk cmdlet will not be able to find them this way.

Same goes for the Remove-HardDisk.

If you want to remove orphaned disk files, you might want to take a look at my Orphaned Files And Folders – Spring Cleaning post.

That function also has the option to remove the orphaned files.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos