Trying to automate a PS Script

Hey Folks!

We’re having issues with orphaned snapshots being left behind on failed Hyper-V backups. The script I run to delete the old snaps is fairly straightforward:

Get-VMSnapshot -VMName VMNAME | Remove-VMSnapshot

Doing this is fairly quick, but I figured I could automate this if I put a little more effort in. Here’s what I have so far.

If I run the following script on the Hyper-V Host I get a nice little CSV file with the names that would be the “VMNAME” above:

Get-VM | Select-Object Name | Export-CSV PATH -NoTypeInformation

It’s here that I’m struggling to read the CSV file and populate the “VMNAME” portion from the CSV file? Any help would be greatly appreciated!

James

Something like this maybe (caveat I don’t know all the hyper v cmdlets or what your csv looks like), assuming your csv has name at the top and all the vm name’s below it

import-csv C:\servers.csv | % {Get-VMSnapshot -VMName $_.name | Remove-VMSnapshot}

or this might work (see above caveat)

Get-vm | get-vmsnapshot | remove-vmsnapshot

Hi Jon,

The first script worked perfectly, thanks!

James