Microsoft Virtualization Discussions

Script for creating vols/qtrees from hostname

tyrone_owen_1
5,288 Views

Hello,

I'd like to build a script which can create FlexVols and qtrees from a list of hostnames. The names of the vols and the qtrees will be based on the hostnames of the servers which will connect to the LUNs contained within the qtrees.

Are you able to give me some pointers on where I should start please?

Thanks

1 ACCEPTED SOLUTION

timothyn
5,288 Views

Yeah, there is some PowerShell magic there. 

  • "$hosts | foreach" pipes the list of hostnames to a loop of commands, each time that loop executes it stores the current item (hostname) in the "$_" variable. 
    • "$_" is pretty important in PowerShell, you an learn more about it by running help about_Automatic_Variables
  • Inside the foreach loop (really a block), the commands are always executed in order
  • PowerShell automatically does string substitution on doubly quoted strings, see help about_quoting_rules for details
    • "$_ boot" would automatically work, but PowerShell gets confused by the extra underscore in "$__boot" and doesn't know where the variable ends.
    • "$()" will evaluate any expression inside it without worrying about the text around it
    • "$($_)" evaluates the $_ variable, in this case the current item from the list of hostnames
    • "$($_)_boot" evaluates to something like "foo_boot"

Hope that helps!

View solution in original post

8 REPLIES 8

tyrone_owen_1
5,288 Views

Would something like this work:

    # Define Hostnames

     $host1 = computer1
     $host2 = computer2
     $host3 = computer3
     $hosts = $host1, $host2, $host3

     # Load the DataONTAP Module

     Import-Module DataONTAP

     # Connect to the filer

     Connect-NaController filer1

     # Create FlexVol


     $hosts : ForEach-Object < New-NaVol -Name "$hosts_BOOT" -Aggregate aggr_test -SpaceReserve none  -Size 100g >

     # Create Qtree

     $hosts : ForEach-Object < New-NaQtree "/vol/$hosts_BOOT/$hosts_boot.q"

timothyn
5,288 Views

Certainly!  You're pretty good on the structure too.  In case you need some help with the syntax:

Import-Module dataontap

$hosts = "foo","bar","baz"

Connect-NaController 10.61.169.28

$hosts | foreach {

    New-NaVol "$($_)_boot" -Aggregate aggr0 -SpaceReserve none -Size 10g

    New-NaQtree "/vol/$($_)_boot/$($_)_boot.q"

}

Another tip, if you aren't sure what a cmdlet will do, most cmdlets (in the toolkit or otherwise) support the "-WhatIf" switch which will show you the operation it will perform, but won't actually do anything.

Cheers,

Eric

tyrone_owen_1
5,288 Views

Thanks Eric, really appreciate the reply and pleased I wasn't too wide of the mark.

1. Will the commands be run in order? For example, after the 'foo_boot' volume has been created, will the 'foo_boot.q' qtree be created inside it?

2. How does the '$($_)' part of the script work? I was thinking I'd have to put in the '$hosts' variable?

Thanks for the '-WhatIf' tip.

Cheers

timothyn
5,289 Views

Yeah, there is some PowerShell magic there. 

  • "$hosts | foreach" pipes the list of hostnames to a loop of commands, each time that loop executes it stores the current item (hostname) in the "$_" variable. 
    • "$_" is pretty important in PowerShell, you an learn more about it by running help about_Automatic_Variables
  • Inside the foreach loop (really a block), the commands are always executed in order
  • PowerShell automatically does string substitution on doubly quoted strings, see help about_quoting_rules for details
    • "$_ boot" would automatically work, but PowerShell gets confused by the extra underscore in "$__boot" and doesn't know where the variable ends.
    • "$()" will evaluate any expression inside it without worrying about the text around it
    • "$($_)" evaluates the $_ variable, in this case the current item from the list of hostnames
    • "$($_)_boot" evaluates to something like "foo_boot"

Hope that helps!

tyrone_owen_1
5,288 Views

Eric,

That's brilliant, thanks very much for your help and I appreciate the time you've given to answer.

Thanks, Ty

tyrone_owen_1
5,288 Views

......another thought, if I wanted to read in the list of hosts from a text file as opposed to defining a list within the script, can I do that?

I've got the hosts listed in an Excel spreadsheet I'm thinking if I can export this out to a text file and read it into the script it would save my typing.

Thanks

timothyn
5,288 Views

Of course!

Get-Content works great for a simple file with one item per line:

PS C:\> $hosts = get-content C:\temp\hosts.txt

And if you have more sophisticated data, Import-Csv can read CSV files (easily exported from Excel).  "help Get-Content" or "help Import-Csv" will tell you more.

tyrone_owen_1
5,288 Views

Thanks once again Eric

Public