Redgate - SQL Clone, Delete Clone

Octopus.Script exported 2019-05-16 by alex-yates belongs to ‘Redgate’ category.

Deletes a database clone with Redgate SQL Clone.

Requires SQL Clone.

Version date: 16th May 2019

Parameters

When steps based on the template are included in a project’s deployment process, the parameters below can be set.

SQL Clone Server (required)

cloneServer =

The URL for your SQL Clone server (e.g. http://sql-clone.example.com:14145)

SQL Clone User (optional)

cloneUser =

User account to access SQL Clone. (If left blank Octopus tentacle account will be used.)

SQL Clone Password (optional)

clonePassword =

User account to access SQL Clone. (If left blank Octopus tentacle account will be used.)

SQL Server Instance (required)

cloneSqlServer =

The SQL Server instance the clone exists on. This SQL Server instance must have already been added to the SQL Clone Server specified above.

Clone Name (required)

cloneName =

The name of the clone to delete.

Script body

Steps based on this template will execute the following PowerShell script.

$ErrorActionPreference = 'Stop'

Write-Debug "Entering script SQLCloneDeleteTask.ps1"

# The code for this step template is largely a copy/paste job from the
# Azure DevOps Services step template which is maintained by Redgate:
# https://github.com/red-gate/SqlCloneVSTSExtension/blob/master/ImageTask/SQLCloneImageTask.ps1
# The code was copied and adapted on 16th May 2019.

Write-Verbose "cloneServer is $cloneServer"
Write-Verbose "cloneUser is $cloneUser"
Write-Verbose "clonePassword is $clonePassword"
Write-Verbose "cloneSqlServer is $cloneSqlServer"
Write-Verbose "cloneName is $cloneName"

# This line is broken: Import-Module "$PSScriptRoot\Modules\RedGate.SQLClone.PowerShell.dll"

if($cloneUser){
    $password = ConvertTo-SecureString -String $clonePassword -AsPlainText -Force
    $credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $cloneUser,$password

}
Connect-SqlClone -ServerUrl $cloneServer -Credential $credential
Write-Output "Connected to SQL Clone server"

        $sqlServerParts = $cloneSqlServer.Split('\', [System.StringSplitOptions]::RemoveEmptyEntries)
        if ($sqlServerParts.Count -ge 3)
        {
            write-error 'SQL Server instance "' + $cloneSqlServer + '" has not been recognised, if specifying a named instance please use "machine\instance"'
            exit 1
        }
        $cloneSqlServerHost = $sqlServerParts[0]
        $instanceName = ''
        if ($sqlServerParts.Count -ge 2)
        {
            $instanceName = $sqlServerParts[1]
        }
        try
        {
            $instance = Get-SqlCloneSqlServerInstance -MachineName $cloneSqlServerHost -InstanceName $instanceName
            Write-Output "Found SQL Server instance"
        }
        catch
        {
            $instances = Get-SqlCloneSqlServerInstance
            $instanceNames = "`n"
            Foreach ($cInstance in $instances)
            {
                $instanceNames += $cInstance.Name + "`n"
            }
            $message = 'SQL Server instance "' + $cloneSqlServer + '"  has not been added to SQL Clone, available instances:' + $instanceNames
            write-error $message
            exit 1
        }
        
        try
        {
            $clone = Get-SqlClone -Name $cloneName -Location $instance
            Write-Output "Found clone"
        }
        catch
        {
            $clones = Get-SqlClone -Location $instance
            $cloneNames = "`n"
            Foreach ($cClone in $clones)
            {
                $cloneNames += $cClone.Name + "`n"
            }
            $message = 'SQL Clone clone ' + $cloneName + ' does not exist, available clones on SQL instance "' + $cloneSqlServer + '":' + $cloneNames
            write-error $message
            exit 1
        }
        
        Write-Output "Deleting clone"
        Remove-SqlClone -Clone $clone | Wait-SqlCloneOperation
        Write-Output "Finished deleting clone"     

Write-Debug "Leaving script SQLCloneDeleteTask.ps1"

Provided under the Apache License version 2.0.

Report an issue

To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.

{
  "Id": "b13ba90b-3e67-4175-aad4-9531783c4c11",
  "Name": "Redgate - SQL Clone, Delete Clone",
  "Description": "Deletes a database clone with [Redgate SQL Clone](https://www.red-gate.com/products/dba/sql-clone/index).\n\nRequires SQL Clone.\n\n*Version date: 16th May 2019*",
  "Version": 1,
  "ExportedAt": "2019-05-16T11:37:47.360Z",
  "ActionType": "Octopus.Script",
  "Author": "alex-yates",
  "Packages": [],
  "Parameters": [
    {
      "Id": "8c140a4c-65a2-4341-a604-73d14775b3a0",
      "Name": "cloneServer",
      "Label": "SQL Clone Server (required)",
      "HelpText": "The URL for your SQL Clone server (e.g. http://sql-clone.example.com:14145)",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "e5ac1d04-b8a5-440e-ba69-a5d66a53abba",
      "Name": "cloneUser",
      "Label": "SQL Clone User (optional)",
      "HelpText": "User account to access SQL Clone. (If left blank Octopus tentacle account will be used.)",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "5f6288f2-57c9-4a11-91f2-b0c2e3cb9ccd",
      "Name": "clonePassword",
      "Label": "SQL Clone Password (optional)",
      "HelpText": "User account to access SQL Clone. (If left blank Octopus tentacle account will be used.)",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "Sensitive"
      }
    },
    {
      "Id": "ab892024-c4b5-46c2-9434-2ad150e3e014",
      "Name": "cloneSqlServer",
      "Label": "SQL Server Instance (required)",
      "HelpText": "The SQL Server instance the clone exists on. This SQL Server instance must have already been added to the SQL Clone Server specified above.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "a758e9de-05c6-4879-8541-d7f73215fe87",
      "Name": "cloneName",
      "Label": "Clone Name (required)",
      "HelpText": "The name of the clone to delete.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptSource": "Inline",
    "Octopus.Action.Script.Syntax": "PowerShell",
    "Octopus.Action.Script.ScriptBody": "$ErrorActionPreference = 'Stop'\n\nWrite-Debug \"Entering script SQLCloneDeleteTask.ps1\"\n\n# The code for this step template is largely a copy/paste job from the\n# Azure DevOps Services step template which is maintained by Redgate:\n# https://github.com/red-gate/SqlCloneVSTSExtension/blob/master/ImageTask/SQLCloneImageTask.ps1\n# The code was copied and adapted on 16th May 2019.\n\nWrite-Verbose \"cloneServer is $cloneServer\"\nWrite-Verbose \"cloneUser is $cloneUser\"\nWrite-Verbose \"clonePassword is $clonePassword\"\nWrite-Verbose \"cloneSqlServer is $cloneSqlServer\"\nWrite-Verbose \"cloneName is $cloneName\"\n\n# This line is broken: Import-Module \"$PSScriptRoot\\Modules\\RedGate.SQLClone.PowerShell.dll\"\n\nif($cloneUser){\n    $password = ConvertTo-SecureString -String $clonePassword -AsPlainText -Force\n    $credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $cloneUser,$password\n\n}\nConnect-SqlClone -ServerUrl $cloneServer -Credential $credential\nWrite-Output \"Connected to SQL Clone server\"\n\n        $sqlServerParts = $cloneSqlServer.Split('\\', [System.StringSplitOptions]::RemoveEmptyEntries)\n        if ($sqlServerParts.Count -ge 3)\n        {\n            write-error 'SQL Server instance \"' + $cloneSqlServer + '\" has not been recognised, if specifying a named instance please use \"machine\\instance\"'\n            exit 1\n        }\n        $cloneSqlServerHost = $sqlServerParts[0]\n        $instanceName = ''\n        if ($sqlServerParts.Count -ge 2)\n        {\n            $instanceName = $sqlServerParts[1]\n        }\n        try\n        {\n            $instance = Get-SqlCloneSqlServerInstance -MachineName $cloneSqlServerHost -InstanceName $instanceName\n            Write-Output \"Found SQL Server instance\"\n        }\n        catch\n        {\n            $instances = Get-SqlCloneSqlServerInstance\n            $instanceNames = \"`n\"\n            Foreach ($cInstance in $instances)\n            {\n                $instanceNames += $cInstance.Name + \"`n\"\n            }\n            $message = 'SQL Server instance \"' + $cloneSqlServer + '\"  has not been added to SQL Clone, available instances:' + $instanceNames\n            write-error $message\n            exit 1\n        }\n        \n        try\n        {\n            $clone = Get-SqlClone -Name $cloneName -Location $instance\n            Write-Output \"Found clone\"\n        }\n        catch\n        {\n            $clones = Get-SqlClone -Location $instance\n            $cloneNames = \"`n\"\n            Foreach ($cClone in $clones)\n            {\n                $cloneNames += $cClone.Name + \"`n\"\n            }\n            $message = 'SQL Clone clone ' + $cloneName + ' does not exist, available clones on SQL instance \"' + $cloneSqlServer + '\":' + $cloneNames\n            write-error $message\n            exit 1\n        }\n        \n        Write-Output \"Deleting clone\"\n        Remove-SqlClone -Clone $clone | Wait-SqlCloneOperation\n        Write-Output \"Finished deleting clone\"     \n\nWrite-Debug \"Leaving script SQLCloneDeleteTask.ps1\""
  },
  "Category": "Redgate",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/redgate-sql-clone-delete-clone.json",
  "Website": "/step-templates/b13ba90b-3e67-4175-aad4-9531783c4c11",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAD9QTFRF9s3N7Z6e3U1N87+/+NjY0RIS2Tc3zgAA/O7u425u1SQk5Xp66Y2N/fPz76ys+eHh4F5e0AkJ/vj4/vz8////8g2GDwAABlRJREFUeNrsne2SpCoMhkEQ+RJQ+v6v9YDau+JO97a2IuxJdn7MVk3V+EzSQN6QiB7/iCEAARAAARAAARAAARAAARAAARAAARAAARAAAZAvrOeWUkSIwVprt7bwf2wIQYha3vuCQXpKjFbCNUzKoXtlQysZc05pQygvDmSkWMXnb9vuUwtAkjUKkzJAPKcEizcO+IBIOhycM94I4q1Rjn0D8bRWOmWsvwekx461Z1A8YViDbW6QnmJ3IsPvMGMacZ8NhBshu6usddrmAeGYtd2VNjBFLwcZqbqWYnGLQuOlIFSxLo9JRf1lIKNhQ5fLwuf+KhDbdHmtodeAkMwcXYsBBEAABEAABEAA5DqQdvg3QLjRyjWsrR4kyg29pYhg1ci2bpAFZzoLIywmKWuoF2QlQlgyxZuUlYNs4m2oG2SmmeONxHhjJ8XbLSAbMRufsb7dDbKIwhahyT+Vg/yON2+JVofWt3JA/iie7FvfigR5xtu8vn0mK5ULspb6Poi3KkDmDegZb0PeQ6O9AuYxhngzMusxnm1+eDyvNGtl1sSKpT4hSmFDkB1PIMFD1gwxVWOp69pYm2WNC0Toq2IzlXlT3TS6uNvUApgT2ixXAnaWj1zmnJ0lPrHs5xpt8JFQGBNiP60IZgf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4qKAuGxVyTOLT9riFh+kDGOBdFxhP+pg6lygng+DZ5hlww+ywDS8z2jc8oEsWje0eT1sw4vKytMn2S2500QZYLACFAAARAAARAAARAAKRDEujYrx+Auem1HfDtPRg6pd4mau47xHqlcThn2vqVnbz7CRYZEpJNi93DL3YmVR/rqN/VIceBNdgdSXW+Nuw5lYPjQhfWD4gM37oIQG2Sj7EHJ/6iK4h9Iu1NjbJBOk+PNA9/IQZwSdZIaNzBlKP+m+vKVrjUFAQmeOS5rxTdvBk98P9f1FKWxR0QLx3Yq7sM8+hSd04tykvbrH2MsIJipkPb39VXG2xyEWj4+zrIz1fhlufF+nCYf6blvaepimr/VU/dSf2I5OF/Fyj8yGbzpGEAABEAABEAABEAABEAABEAABEAABEAA5H8F8p8AAwD2WseknbBRCwAAAABJRU5ErkJggg==",
  "$Meta": {
    "Type": "ActionTemplate"
  }
}

History

Page updated on Thursday, May 16, 2019