slashji
Contributor
Contributor

Find Thick provisioned disks and shutdown then delete whole VM

Jump to solution

I need to find all VM´s with Thick Provisioned Disk(s) and delete them. 

At the moment i can get the list of VM´s using Thick provisioned disks using command:

Get-Datastore | Get-VM | Get-HardDisk | Where {$_.storageformat -eq "Thick" } | Select Parent, Name, CapacityGB, storageformat 

 

Now i still cant understand how can i pipe the Stop-VM and then Remove-VM -DeletePermanently command into the previous one.

I feel like its really simple oneliner but im missing something out in the basics of PowerShell...

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

You could use a Where-clause.

Get-Datastore | Get-VM | 
Where{Get-HardDisk -VM $_ | where{$_.StorageFormat -eq 'Thick'}} |
Stop-VM -Confirm:$false |
Remove-VM -DeletePermanently -Confirm:$false


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

View solution in original post

0 Kudos
3 Replies
LucD
Leadership
Leadership

You could use a Where-clause.

Get-Datastore | Get-VM | 
Where{Get-HardDisk -VM $_ | where{$_.StorageFormat -eq 'Thick'}} |
Stop-VM -Confirm:$false |
Remove-VM -DeletePermanently -Confirm:$false


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

View solution in original post

0 Kudos
slashji
Contributor
Contributor

Perfect, thanks!
Little note that Stop-VM and Remove-VM have to be run as separate tasks since Remove-VM is using info piped from Stop-VM in your example.

0 Kudos
LucD
Leadership
Leadership

That was the intention.
Stop-VM returns a VirtualMachine object, which is then used by Remove-VM.
Isn't that working?


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

0 Kudos