Sunday, 14 December 2014

SVM Destroyer

This is an update of Vserver/SVM Dismantling with PowerShell from 12th October (and I couldn’t resist giving the post a different title!) Remember, this script is only intended for lab scenarios where you regularly build up infrastructures for testing purposes, then need to rapidly pull it all down again to start afresh!

Non-Interactive Mode

This version - have called the script Destroy-SVM.ps1 (but you can call it anything you like) - now has a non-interactive mode.

Example:
PS C:\Scripts>
.\Destroy-SVM.ps1 -Cluster NACLU1 -Username ADMINUSER -Password ADMINPASS -SVM NASVM1 -PeerClusterUsername NACLU2 -PeerClusterPassword ADMINPASS -Yes

The script can either be run interactively as before with:
.\Destroy-SVM.ps1

Or you can run it non-interactively using the syntax in the example above. A quick description of the switches:

-Cluster : Cluster name of the cluster that contains the SVM you want to destroy
-Username : An admin user with permission to destroy the SVM
-Password : The password for the admin user
-SVM : The SVM (Vserver) you want to destroy
-PeerClusterUsername : (Optional) An admin user for cluster(s) (if there’s more than one and they have different passwords, the script will prompt) which contain SVMs that are peered with the SVM you want to destroy
-PeerClusterPassword : (Optional) Password for the above peer cluster admin user
-Yes : Include this switch if you don’t want the script to prompt for “do you really want to do this?”

The 16 Steps

The steps have been expanded by 4 to make 16 steps now!

>>>>> STEP 1/16: Online All Volumes that have SnapMirrorDestinations
>>>>> STEP 2/16: Release SnapMirror Destinations
>>>>> STEP 3/16: Delete SnapMirrors
>>>>> STEP 4/16: Remove Vserver Peers
>>>>> STEP 5/16: Remove Vserver Transition Peers
>>>>> STEP 6/16: Unmount all volumes (but rootvol)
>>>>> STEP 7/16: Remove all LUN Maps (SAN)
>>>>> STEP 8/16: Igroups Delete (SAN)
>>>>> STEP 9/16: Remove Portsets (SAN)
>>>>> STEP 10/16: Offline clones
>>>>> STEP 11/16: Delete clones
>>>>> STEP 12/16: Offline volumes (rootvol last)
>>>>> STEP 13/16: Destroy volumes (rootvol last)
>>>>> STEP 14/16: CIFS Server Delete
>>>>> STEP 15/16: Remove SnapMirror Policies owned by vserver_admin
>>>>> STEP 16/16: SVM Delete

The Script

Copy and paste into a text editor (like Notepad++) and save as say Destroy-SVM.ps1.
Note: Blogger’s not really designed for posting code, but copy and pasting from it does seem to work fine!

## TITLE: Destroy-SVM.ps1 for CDOT 8.2.X
## CAVEAT UTILITOR: This program comes with no warranty and no support!
## DESCRIPTION: Dismantles and Deletes an SVM

Param(
       [String]$Cluster,[String]$Username,$Password,[String]$SVM,
       [String]$PeerClusterUsername,$PeerClusterPassword,[Switch]$Yes
)

$PeerClu_Specified = $null
If($PeerClusterUsername -and $PeerClusterPassword){
       $PeerClu_Specified = $true
       $PeerClusterPassword = $PeerClusterPassword | ConvertTo-SecureString -AsPlainText -Force
}

"";"<<<<< Destroy-SVM.ps1 for CDOT 8.2.X >>>>>";""
Import-Module DataONTAP
If(!$Cluster){ $cluster    = Read-Host "Enter Cluster Name"} else {"Cluster Name = $Cluster"}
If(!$Username){$username   = Read-Host "Enter Admin User  "} else {"Admin User = $Username"}
If(!$Password){$password   = Read-Host "Enter Password    " -AsSecureString}
else{          $password = $Password | ConvertTo-SecureString  -AsPlainText -Force}
$credential = New-Object System.Management.Automation.PsCredential($username,$password)
[Void](Connect-NcController $cluster -Credential $credential -ErrorAction SilentlyContinue)
If(!$global:currentnccontroller){"";"Failed to connect - exiting program!";"";EXIT}

"";">>>>> Data SVMs in $cluster";""
$attributes        = Get-NcVserver -Template
$query             = Get-NcVserver -Template
$query.VserverType = "data"
$svms              = Get-NcVserver -Attributes $attributes -Query $query
If(!$svms){"$cluster has no Data SVMs, exiting!";"";EXIT}
$svms | foreach { $_.Vserver };""

If(!$SVM){
       $answered = $false
       while (!$answered){
              $svm = Read-Host "Enter SVM Name"
              $svms | foreach {If($svm -eq $_){$answered = $true}}
       }
} else {
       $match = $false
       $svms | foreach {If($svm -eq $_){$match = $true}}
       If (!$match){"SVM $SVM does not exist, exiting!";"";EXIT}
       "SVM to delete = $SVM"
}
$global:currentnccontroller.Vserver = $svm;""

If(!$Yes){
       $answered = $false
       while (!$answered){
              $yesNo = (Read-Host "Are you sure you want to dismantle and delete the SVM $SVM (Y/N)").ToUpper()
              If($yesNo -eq "Y"){$answered = $true}elseif($yesNo -eq "N"){EXIT}
       }
}

"";">>>>> STEP 1/16: Online All Volumes that have SnapMirrorDestinations"
$SMDestinations = Get-NcSnapMirrorDestination
If($SMDestinations){
       ""
       $SMDestinations.SourceVolume | foreach {
              If ((Get-NcVol $_).state -ne "online"){
                     Set-NcVol $_ -Online
              }
       }
}

"";">>>>> STEP 2/16: Release SnapMirror Destinations"
$SMDestinations | Invoke-NcSnapMirrorRelease -Confirm:$false

"";">>>>> STEP 3/16: Delete SnapMirrors"
<# Remove-NcSnapMirror does not work in Vserver Context for Pre 8.2 relationships (LS Mirrors) #>
$global:currentnccontroller.Vserver = $null
$SnapMirrors = Get-NcSnapMirror -DestinationVserver $SVM
$SnapMirrors | foreach {Remove-NcSnapmirror -SourceCluster $_.SourceCluster -SourceVserver $_.SourceVserver -SourceVolume $_.SourceVolume -DestinationCluster $_.DestinationCluster -DestinationVserver $_.DestinationVserver -DestinationVolume $_.DestinationVolume -Confirm:$false}
$global:currentnccontroller.Vserver = $svm

"";">>>>> STEP 4/16: Remove Vserver Peers"
[Void](get-ncvserverpeer | remove-ncvserverpeer -Confirm:$false -ErrorAction SilentlyContinue)
$VserverPeers = Get-NcVserverPeer

If($VserverPeers){
      
       "";"Unable to remove Vserver Peers.";"Connecting to Vserver Peer(s) Cluster(s) in turn to delete SnapMirrors"
      
       foreach ($VserverPeer in $VserverPeers) {
      
       $PeerCluster = $VserverPeer.PeerCluster
       $PeerSVM     = $VserverPeer.PeerVserver
       $answered    = $false
      
       while(!$answered){
              If(!$PeerClu_Specified){
                     "";"Enter Cluster credentials for the cluster $PeerCluster, so we can release/delete SnapMirrors on the Data SVM $PeerSVM";""
                     $PeerClusterUsername = Read-Host "Enter Admin User for $PeerCluster"
                     $PeerClusterPassword = Read-Host "Enter Password" -AsSecureString
              }
              $PeerCredential = New-Object System.Management.Automation.PsCredential($PeerClusterUsername,$PeerClusterPassword)
              [Void](Connect-NcController $PeerCluster -Credential $PeerCredential)
              If(!$global:currentnccontroller){
                     ""; "Failed to connect to $PeerCluster!"; ""
                     $PeerClusterUsername = $PeerClusterPassword = $PeerClu_Specified = $null
              } else {$answered = $true}
       }
      
       "";">>>>> $PeerSVM STEP 1/3: Online All Volumes that have SnapMirrorDestinations"
       $SMDestinations = Get-NcSnapMirrorDestination -SourceVserver $PeerSVM -DestinationVserver $SVM
       If($SMDestinations){
              ""
              $SMDestinations.SourceVolume | foreach {
                     If ((Get-NcVol $_ -VserverContext $PeerSVM).state -ne "online"){
                           Set-NcVol $_ -VserverContext $PeerSVM -Online
                     }
              }
       }
      
       "";">>>>> $PeerSVM STEP 2/3: Release SnapMirror Destinations"
       $SMDestinations | Invoke-NcSnapMirrorRelease -Confirm:$false
      
       "";">>>>> $PeerSVM STEP 3/3: Delete SnapMirrors"      
       $SnapMirrors = Get-NcSnapMirror -SourceVserver $SVM -DestinationVserver $PeerSVM
       $SnapMirrors | foreach {Remove-NcSnapmirror -SourceCluster $_.SourceCluster -SourceVserver $_.SourceVserver -SourceVolume $_.SourceVolume -DestinationCluster $_.DestinationCluster -DestinationVserver $_.DestinationVserver -DestinationVolume $_.DestinationVolume -Confirm:$false}
      
       }
      
       [Void](Connect-NcController $cluster -Credential $credential)
       $global:currentnccontroller.Vserver = $svm
       [Void](get-ncvserverpeer | remove-ncvserverpeer -Confirm:$false -ErrorAction SilentlyContinue)
      
}

"";">>>>> STEP 5/16: Remove Vserver Transition Peers"
Get-NcVserverPeerTransition -Vserver $svm | Remove-NcVserverPeerTransition -Confirm:$false

"";">>>>> STEP 6/16: Unmount all volumes (but rootvol)"
$attributes = Get-NcVol -Template
$volumes    = Get-NcVol -Attributes $attributes
$rootvol    = (Get-NcVserver).rootvolume
$volumes | foreach { If ($_.Name -ne $rootvol) { Dismount-NcVol $_ } }

"";">>>>> STEP 7/16: Remove all LUN Maps (SAN)"
Get-NcLunMap | Remove-NcLunMap -Confirm:$false

"";">>>>> STEP 8/16: Igroups Delete (SAN)"
Get-NcIgroup | Remove-NcIgroup -Confirm:$false

"";">>>>> STEP 9/16: Remove Portsets (SAN)"
Get-NcPortSet | Remove-NcPortSet

"";">>>>> STEP 10/16: Offline clones"
$clone_attrs       = Get-NcVolClone -Template
$clone_query       = Get-NcVolClone -Template
$clone_query.state = "!offline"
$onlineClones      = (Get-NcVolClone -Attributes $clone_attrs -Query $clone_query).volume
If($onlineClones){ $onlineClones | foreach { Set-NcVol $_ -Offline } }

"";">>>>> STEP 11/16: Delete clones"
$clones = (Get-NcVolClone -Attributes $clone_attrs).volume
If($clones){ $clones | foreach { Remove-NcVol $_ -Confirm:$false } }

"";">>>>> STEP 12/16: Offline volumes (rootvol last)"
$query       = Get-NcVol -Template
$query.state = "!offline"
$onlineVols  = Get-NcVol -Attributes $attributes -Query $query
$onlineVols | foreach { If ($_.Name -ne $rootvol) { Set-NcVol $_ -Offline } }
If($rootvol -and ((Get-NcVol $rootvol).State -ne "offline") ){Set-NcVol $rootvol -Offline}

"";">>>>> STEP 13/16: Destroy volumes (rootvol last)"
$volumes | foreach { If ($_.Name -ne $rootvol) { Remove-NcVol $_ -Confirm:$false } }
If($rootvol){Remove-NcVol $rootvol -Confirm:$false}

"";">>>>> STEP 14/16: CIFS Server Delete"
Get-NcCifsServer | Remove-NcCifsServer -Confirm:$false

"";">>>>> STEP 15/16: Remove SnapMirror Policies owned by vserver_admin"
$query = Get-NcSnapmirrorPolicy -Template
$query.owner = "vserver_admin"
Get-NcSnapmirrorPolicy -Query $query | Remove-NcSnapMirrorPolicy

"";">>>>> STEP 16/16: SVM Delete"
Remove-NcVserver $svm -Confirm:$false
$global:currentnccontroller.Vserver = $null

If(!(Get-NcVserver $svm)){"Successfully removed the data SVM $svm from cluster $cluster!"}

## THE END ##

3 comments:

  1. This script has been great and saved me a ton of time! I have been using it a lot in my lab since it came out! The only issues I have been running into recently are SVMs that are in an SVM-DR relationship, which was not available in the 8.2.x release. Thanks!

    ReplyDelete
  2. I love this script. I use it in my lab all the time after performing a customer repro, tech demo, or learning activity.

    ReplyDelete
    Replies
    1. Thanks Chance. I'm pleased it's been of use.

      Delete