Trying to automate a PS Script

This topic contains 2 replies, has 2 voices, and was last updated by  James Hunter 2 weeks, 1 day ago.

  • Author
    Posts
  • #85184

    James Hunter
    Participant

    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

  • #85187

    Jon
    Participant

    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
    • #85190

      James Hunter
      Participant

      Hi Jon,

      The first script worked perfectly, thanks!

      James

You must be logged in to reply to this topic.