VMware Cloud Community
CFeggestad
Contributor
Contributor
Jump to solution

Remove-HardDisk, cant pass a string to it, is there another way?

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

  }

}

0 Kudos
1 Solution

Accepted Solutions
snoopj
Enthusiast
Enthusiast
Jump to solution

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

  }

}

View solution in original post

0 Kudos
5 Replies
NealeC
Hot Shot
Hot Shot
Jump to solution

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

-------------- If you found this or any other answer useful please consider the use of the Helpful or Correct buttons to award points. Chris Neale VCIX6-NV;vExpert2014-17;VCP6-NV;VCP5-DCV;VCP4;VCA-NV;VCA-DCV;VTSP2015;VTSP5;VTSP4 http://www.chrisneale.org http://www.twitter.com/mrcneale
0 Kudos
CFeggestad
Contributor
Contributor
Jump to solution

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

0 Kudos
snoopj
Enthusiast
Enthusiast
Jump to solution

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.

snoopj
Enthusiast
Enthusiast
Jump to solution

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

  }

}

0 Kudos
CFeggestad
Contributor
Contributor
Jump to solution

Thanks Jon for your help! Both options worked.

0 Kudos