Powershell script to expand/contract volume on SM and non-SM volumes

by on ‎2013-03-08 11:37 AM

Hi Guys,

I just wrote a script to expand/contract a volume.  I needed to write this specifically for volumes in snapmirror relationships because someone would expand source volume and then forget to expand the destination and then snapmirror would start failing.

I am pretty new to powershell and this is probably my most complicated script so all in all I am looking for any inputs on how I can make this better.  It works perfectly for me but your inputs will be appreciated.  Feel free to use it for yourself.  There is nothing to modify for your specific environment.  This is totally interactive script.  Also, you will only have to login once, once you login it stores your credentials and in domain environment, then it will login to source and destination filers.

Logic is pretty simple and it is as follows:

Ask for filer name and then ask for prompt to login.

Ask for volume on the source filer that you wan to expand.

At this point, if the volume is in Snapmirror relationship, it will enter the "IF" loop and depending upon the size you enter, it will expand/contract both source and destination volume.

If the volume is not in snapmirror relationship, it will enter "ELSE" and expand the source volume.

#############################################

#                                              #

#    Volume Expansion/Contraction Script        #

#                 Author:                      #

#                Nimish Naik                      #

#                                              #

#############################################

import-module .\DataONTAP.dll

$sfiler = ""

$svol = ""

$saggr = ""

$saggrfrespace = ""

$dispfreesaggrspace = ""

$sm = ""

$dfiler = ""

$dvol = ""

$daggr = ""

$daggrfreespace = ""

$dispfreedaggrspace = ""

$dvol = ""

$size = ""

while ($sfiler -eq ""){

   Clear-Host 

   $sfiler = read-host "Enter the name of filer"

}

clear-host

connect-nacontroller $sfiler -Credential (Get-Credential)

add-nacredential

while ($svol -eq ""){

   Clear-Host 

   $svol = read-host "Enter name of volume you'd like to expand/contract"

}

$sm = Get-NaSnapmirrorDestination $svol

if ($sm.destinationlocation -gt '0') {

$saggr = get-navolcontainer -controller $sfiler -name $svol

$saggrfreespace = $saggr.sizeavailable /1024 /1024/1024

$dispfreesaggrspace = "Free space on aggregate $saggr on $sfiler = $saggrfreespace GB"

$split = $sm.destinationlocation.split(":")

$dfiler = $split[0]

$dvol = $split[1]

connect-nacontroller $dfiler -Credential (Get-NaCredential $sfiler -SystemScope $false).Credential

$daggr = get-navolcontainer -controller $dfiler -name $dvol

$daggrfreespace = $daggr.sizeavailable /1024 /1024 /1024

$dispfreedaggrspace = "Free space on aggregate $daggr on $dfiler = $daggrfreespace GB"

while ($size -eq ""){

   Clear-Host 

   write-host "If you are expanding the volume, the format MUST be in (+)(size)(value) format"

   write-Host "For example, if you want to expand the volume by 100 MB, the format to enter on next screen MUST be +100m"

   write-host ""

   write-host "If you are contracting the volume the format MUST be in (-)(size)(value) format"

   write-Host "For example, if you want to contract the volume by 100 MB, the format to enter on next screen MUST be -100m"

   write-host ""

   read-host "valid values are k, m, g, t "

   write-host ""

   $dispfreesaggrspace

   $dispfreedaggrspace

   write-host ""

   $size = read-host "How much would you like volume $svol and $dvol to expand(+)/contract(-) by?"

   clear-host

   write-host "Resize the volumes $svol on $sfiler & $dvol on $dfiler by $size ?"

   read-host "You must press the Any key on top right of the keyboard to continue. Press Ctrl-C to cancel"

}

Connect-NaController $sfiler -Credential (Get-NaCredential $sfiler -SystemScope $false).Credential

clear-host

set-navolsize $svol $size

clear-host

Connect-NaController $dfiler -Credential (Get-NaCredential $sfiler -SystemScope $false).credential

clear-host

set-navolsize $dvol $size

clear-host

remove-nacredential $sfiler

}

else {

$saggr = get-navolcontainer -controller $sfiler -name $svol

$saggrfreespace = $saggr.sizeavailable /1024 /1024/1024

$dispfreesaggrspace = "Free space on aggregate $saggr on $sfiler = $saggrfreespace GB"

while ($size -eq ""){

   Clear-Host

   write-host "If you are expanding the volume, the format MUST be in (+)(size)(value) format"

   write-Host "For example, if you want to expand the volume by 100 MB, the format to enter on next screen MUST be +100m"

   write-host ""

   write-host "If you are contracting the volume the format MUST be in (-)(size)(value) format"

   write-Host "For example, if you want to contract the volume by 100 MB, the format to enter on next screen MUST be -100m"

   write-host

   read-host "valid values are k, m, g, t - Press any key to continue"

   clear-host

   $dispfreesaggrspace

   $size = read-host "How much would you like volume $svol to expand(+)/contract(-) by?"

   clear-host

   write-host "Resize the volumes $svol on $sfiler by $size ?"

   read-host "You must press the Any key on top right of the keyboard to continue. Press Ctrl-C to cancel"

}

set-navolsize $svol $size

clear-host

remove-nacredential $sfiler

}

Comments
vinith Former NetApp Employee

Good. Try using "Write-Host" to minimum and use powershell objects instead.

Also you can use custom hash tables to append all values to one place rather than using Add-Content to create reusable objects, please have a look at the linkhttp://blogs.msdn.com/b/powershell/archive/2009/12/05/new-object-psobject-property-hashtable.aspx

Thanks Vinith,

Much appreciated.  I will surely look in to hash tables.

Warning!

This NetApp Community is public and open website that is indexed by search engines such as Google. Participation in the NetApp Community is voluntary. All content posted on the NetApp Community is publicly viewable and available. This includes the rich text editor which is not encrypted for https.

In accordance to our Code of Conduct and Community Terms of Use DO NOT post or attach the following:

  • Software files (compressed or uncompressed)
  • Files that require an End User License Agreement (EULA)
  • Confidential information
  • Personal data you do not want publicly available
  • Another’s personally identifiable information
  • Copyrighted materials without the permission of the copyright owner

Files and content that do not abide by the Community Terms of Use or Code of Conduct will be removed. Continued non-compliance may result in NetApp Community account restrictions or termination.