Automation

 View Only
  • 1.  script to check and delete snapshots

    Posted Dec 01, 2023 05:19 PM

    hi all, i am trying to use a powercli script to check and delete snapshots from a file. Needed help with error handling; specifically to display if the vm is not reachable when reading from the text file and further if there is no snapshot present, then to display that. Any help would be great.

    $VM = Get-VM -Name (get-content servers.txt)
    $VM | Get-Snapshot | Remove-Snapshot -RunAsync -Confirm:$false



  • 2.  RE: script to check and delete snapshots
    Best Answer

    Posted Dec 01, 2023 06:15 PM

    Try something like this

     Get-Content servers.txt -PipelineVariable vmName |
     ForEach-Object -Process {
        try{
            Get-VM -Name $vmName -ErrorAction Stop
            $snap = Get-VM -Name $vmName | Get-Snapshot
            if ($snap) {
                $snap | Remove-Snapshot -RunAsync -Confirm:$false
            } else {
                Write-Host "No snapshot(s) for VM $vmName"
            }
        }
        catch{
            write-host "VM $vmName not found"
        }
     }


  • 3.  RE: script to check and delete snapshots

    Posted Dec 02, 2023 02:27 AM

      thank you, as always works perfectly.