I was thinking of using Get-Harddisk and then piping it to Remove-HardDisk, but I'm not sure of a way to get VMDKs only matching the text *snap*. The below script used to work a few months ago, I'm not sure what changed, no upgrades were done.
Code in question:
$DisksToRemove = (Get-VM VM1, VM2, VM3 | Get-Harddisk)
foreach($disk in $DisksToRemove)
{
if($disk.Filename -like "*snap*")
{
Remove-HardDisk -HardDisk $disk.Filename -Confirm:$false
Write-Host "Removed: " -foreground "green"
Write-Host $disk.Filename
}
}
Additionally, you could add this to your existing code and it should work
$DisksToRemove = (Get-VM VM1, VM2, VM3 | Get-Harddisk)
foreach($disk in $DisksToRemove)
{
if($disk.Filename -like "*snap*")
{
$disk | Remove-HardDisk -Confirm:$false <I removed the -HardDisk section>
Write-Host "Removed: " -foreground "green"
Write-Host $disk.Filename
}
}
Hi
What version of powercli are you using, what version of vcenter/esxi?
What is the output if you run it now? Do you get a fail or does it just run/process but not action the removal?
Have you got an example output or a -whatif ?
Regards
Chris
Forgot to put the error:
Remove-HardDisk : Cannot bind parameter 'HardDisk'. Cannot convert the "[DatastoreName] VMName/VMName_1.vmdk" value of type "System.String" to type "VMware.VimAutomation.ViCore.Types.V1.VirtualDevic
e.HardDisk". At D:\Git\FailbackToPROD\8-RemoveDisksDR.ps1:41 char:17 + Remove-HardDisk <<<< $currentdisk -Confirm:$false + CategoryInfo : InvalidArgument: (:) [Remove-HardDisk], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomation.ViCore.Cmdlets.Commands.RemoveHardDisk
vcenter/esxi: 5.1
powercli: 5.5 r1
thanks for your response, NealeC
Try this out:
Get-VM VM1, VM2, VM3 | Get-HardDisk | Where-Object {$_.Filename -match "snap"} | Remove-HardDisk -Confirm:$false
I think the problem is your object types. The object returned by the Get-HardDisk cmdlet is what is expected to be handed to the Remove-HardDisk cmdlet. You can't replicate that by sending it the filename.
You might want to run that without the -Confirm:$false on it to make sure it's grabbing the right disks, or remove that last part to see the listing of all the disks that have been gathered up to that point.
Additionally, you could add this to your existing code and it should work
$DisksToRemove = (Get-VM VM1, VM2, VM3 | Get-Harddisk)
foreach($disk in $DisksToRemove)
{
if($disk.Filename -like "*snap*")
{
$disk | Remove-HardDisk -Confirm:$false <I removed the -HardDisk section>
Write-Host "Removed: " -foreground "green"
Write-Host $disk.Filename
}
}
Thanks Jon for your help! Both options worked.