cmdlet : How to remove all clones for a specific machine (agent)

Hello,
in a similar fashion to the "Remove All Clone for an Image", can I remove all clones for a machine? I'm not certain that using Get-SqlClone how to filter all clones for a machine name.

Thank you for your help and best regards to the dev team!

(Remove All Clone for an Image)
https://documentation.red-gate.com/display/CLONE2/Remove+all+clones+for+an+image
Tagged:

Best Answer

  • owen.hallowen.hall Posts: 57 Silver 4
    edited October 16, 2017 12:25PM Answer ✓
    Hi Benethor, how about something like this?
    Connect-SqlClone -ServerUrl 'http://sql-clone.example.com:14145'
    
    $environmentName = $null # replace with environment name string if desired
    $machineName = 'myMachineName'
    
    $instancesOnMachine = Get-SqlCloneSqlServerInstance -MachineName $machineName | where {$_.Environment -eq $environmentName}
    
    foreach ($instance in $instancesOnMachine) {
        $clonesToDelete = Get-SqlClone -Location $instance
        foreach ($clone in $clonesToDelete) {
            Remove-SqlClone $clone | Wait-SqlCloneOperation
        }
    }
    

    This will find all SQL Server instances on a named machine, then find all clones based on those instances, and delete them.
    Software Engineer
    Redgate Software

Answers

  • BenethorBenethor Posts: 22 Bronze 1
    Nice! Does exactly what I need.

    Thanks a lot!
  • owen.hallowen.hall Posts: 57 Silver 4
    edited October 16, 2017 12:26PM
    No problem - bear in mind I've spotted a bug in the case where $environmentName is null (intended to specify 'no environment'), in that if there are machines of the given name with an environment set, it'll delete clones from them as well. I'll fix that today and update the post.

    Edit: updated!
    Software Engineer
    Redgate Software
Sign In or Register to comment.