Microsoft Virtualization Discussions

Creating a new volume / share with Powershell

BoogieDior
4,718 Views

Hello -

 

I am trying to create a simple powershell script that will walk the steps of create a volume and a share otuside of the UI and force to go through each step along the way.

 

At first I was trying to make it so you would just fill in the $variables at the top of the script for what you want and then have it run the various new-ncvol commands- but this kept erroring.

 

Then when i just input New-NcVol it behaved exactly as i wanted to too by then prompting me for voume name, aggragate, juntion path, and size  <-- this is exactly what i want- somthing that a co worker could run and just type in the information / details of the share they want to create - But i always get the same error right after inputting the size?

 

PS C:\WINDOWS\system32> New-NcVol
cmdlet New-NcVol at command pipeline position 1
Supply values for the following parameters:
(Type !? for Help.)
Name: test99
Aggregate: aggr4
JunctionPath: /test99
Size: 10
New-NcVol : Value in $global:CurrentNcController is not of type NetApp.Ontapi.Filer.C.NcController
At line:1 char:1
+ New-NcVol
+ ~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-NcVol], ArgumentException
+ FullyQualifiedErrorId : ControllerNotSpecified,DataONTAP.C.PowerShell.SDK.Cmdlets.Volume.NewNcVol

PS C:\WINDOWS\system32>

 

I am not looking to do bulk deployments or mass automation - just a way for a simple CLI script to walk through the vol / share creattion process so that anyone with some details could enter them in and it would get made - e.g. I send a mail to Joe saying hey man, can you run ( import ontap module etc ) new-ncvol with the following details ( name, size , aggr, svm ) and he would be able to do so for me?

 

Any help would be most appreciated!

 

Thanks

jordan

 

1 ACCEPTED SOLUTION

mbeattie
4,661 Views

Hi Jordan,

 

I thought it might also be helpful to provide an example for you on an alternative method to achieve the result via Ansible. Rather than writting a script,  you might consider writting a playbook. EG:

 

---
- hosts: localhost
  gather_facts: false
  name: Create Volume CIFS Share
  vars:
    login: &login
      hostname: cluster1.testlab.local
      username: admin
      password: N0tMyP@ssw0rd!
      https: true
      validate_certs: false
    vserver: vserver1
    volume: volume1
    aggregate: node1_aggr1
    size: 10
  tasks:
  - name: Create Volume
    na_ontap_volume:
      state: present
      is_online: yes
      name: "{{ volume }}"
      aggregate_name: "{{ aggregate }}"
      size: "{{ size }}"
      size_unit: gb
      space_guarantee: none
      vserver: "{{ vserver }}"
      volume_security_style: ntfs
      wait_for_completion: true
      junction_path: "{{ '/' + volume }}"
      <<: *login
  - name: Create CIFS Share
    na_ontap_cifs:
      state: present
      share_name: "{{ volume + '$' }}"
      path: "{{ '/' + volume }}"
      vserver: "{{ vserver }}"
      share_properties: browsable,oplocks,changenotify,show-previous-versions
      <<: *login

The playbook is just a configuration file in .yml format (no coding required), you then invoke the playbook and Ansible will deploy the configuration for you. EG i saved the above playbook as "create_volume_cifs_share.yml" and then execute it

 

[root@testux01 playbooks]# ansible-playbook create_volume_cifs_share.yml

PLAY [Create Volume CIFS Share] *********************************************************************************************************************************************************************

TASK [Create Volume] ********************************************************************************************************************************************************************************
changed: [localhost]

TASK [Create CIFS Share] ****************************************************************************************************************************************************************************
changed: [localhost]

PLAY RECAP ******************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Example CLI result:

 

cluster1::*> vol show -vserver vserver1 -volume volume1 -fields volume, junction-path, aggregate, size, space-guarantee, security-style
vserver  volume  aggregate         size security-style junction-path space-guarantee
-------- ------- ----------------- ---- -------------- ------------- ---------------
vserver1 volume1 node1_aggr1 10GB ntfs           /volume1      none

cluster1::*> cifs share show -vserver vserver1 -share-name volume1$ -fields volume, share-name, share-properties
vserver  share-name share-properties                                      volume
-------- ---------- ----------------------------------------------------- -------
vserver1 volume1$   browsable,oplocks,changenotify,show-previous-versions volume1

The advantage to this method is that you don't need to develop scripts or write any code. Just define the configuration. Perhaps this is an option for you to consider and explore? A good place to start to learn more if you have never used ansible before is this blog:

 

https://netapp.io/2018/10/11/getting-started-with-netapp-and-ansible-first-playbook-example/

 

Hope that helps? Please let me know if you have any questions?

 

/Matt

If this post resolved your issue, help others by selecting ACCEPT AS SOLUTION or adding a KUDO.

View solution in original post

2 REPLIES 2

mbeattie
4,669 Views

Hi Jordon,

 

I think the error you are getting might be because you haven't connected to a cluster before attempting to use the New-NcVol cmdlet. You could try something like this basic example, if the user doesn't enter the input parameters when invoking the script they will be prompted for the input.

 

Param(
   [Parameter(Mandatory=$True, HelpMessage="The Cluster name or IP Address")]
   [String]$Cluster,
   [Parameter(Mandatory=$True, HelpMessage="The vserver name")]
   [String]$VserverName,
   [Parameter(Mandatory=$True, HelpMessage="The volume name")]
   [String]$VolumeName,
   [Parameter(Mandatory=$True, HelpMessage="The volume size in GB")]
   [Int]$SizeGB,
   [Parameter(Mandatory=$False, HelpMessage="The aggregate name")]
   [String]$JunctionPath,
   [Parameter(Mandatory=$True, HelpMessage="The aggregate name")]
   [String]$AggregateName,
   [Parameter(Mandatory=$False, HelpMessage="The space reserve")]
   [ValidateSet("none","file","volume")]
   [String]$SpaceReserve="none",
   [Parameter(Mandatory=$False, HelpMessage="The security style")]
   [ValidateSet("mixed","ntfs","unix")]
   [String]$SecurityStyle,
   [Parameter(Mandatory=$True, HelpMessage="The Credential to connect to the cluster")]
   [System.Management.Automation.PSCredential]$Credential   
)
#'------------------------------------------------------------------------------
#'Import the PSTK.
#'------------------------------------------------------------------------------
[String]$moduleName = "DataONTAP"
Try{
   Import-Module -Name $moduleName
   Write-Host "Imported module ""$moduleName"""
}Catch{
   Write-Warning -Message $("Failed importing module ""$moduleName"". Error " + $_.Exception.Message)
   Break;
}
#'------------------------------------------------------------------------------
#'Connect to the cluster.
#'------------------------------------------------------------------------------
Try{
   Connect-NcController -Name $Cluster -HTTPS -Credential $Credential -ErrorAction Stop | Out-Null
   Write-Host "Connected to cluster ""$Cluster"""
}Catch{
   Write-Warning -Message $("Failed connecting to cluster ""$Cluster"". Error " + $_.Exception.Message)
   Break;
}
#'------------------------------------------------------------------------------
#'Set the junction path if not provided.
#'------------------------------------------------------------------------------
If([String]::IsNullOrEmpty($JunctionPath)){
   [String]$JunctionPath = "/$VolumeName"
}
#'------------------------------------------------------------------------------
#'Create the volume.
#'------------------------------------------------------------------------------
[String]$command = $("New-NcVol -Name $VolumeName -Aggregate $AggregateName -JunctionPath $JunctionPath -Size " + $SizeGB + "g -SpaceReserve $SpaceReserve ")
If($SecurityStyle){
   [String]$command += "-SecurityStyle $SecurityStyle "    
}
[String]$command += "-VserverContext $VserverName -ErrorAction Stop"
Try{
   Invoke-Expression -Command $Command -ErrorAction Stop | Out-Null
   Write-Host "Executed Command`: $command"
   Write-Host $("Created Volume ""$VolumeName"" of size " + $SizeGB + "G on vserver ""$VserverName""")
}Catch{
   Write-Warning -Message $("Failed Executing Command`: $command. Error " + $_.Exception.Message)
   Write-Warning -Message $("Failed creating Volume ""$VolumeName"" of size " + $SizeGB + " on vserver ""$VserverName""")
}
#'------------------------------------------------------------------------------

As an example of running the script:

 

PS D:\Scripts\PowerShell\Projects\CreateVolume> $credential = Get-Credential -Credential admin
PS D:\Scripts\PowerShell\Projects\CreateVolume> .\CreateVolume.ps1 -Cluster cluster1.testlab.local -VserverName vserver1
 -VolumeName volume1 -SizeGB 10 -AggregateName  node1_aggr1 -Credential $credential
Imported module "DataONTAP"
Connected to cluster "cluster1.testlab.local"
Executed Command: New-NcVol -Name volume1 -Aggregate node1_aggr1 -JunctionPath /volume1 -Size 10g -SpaceReserve none -VserverContext vserver1 -ErrorAction Stop
Created Volume "volume1" of size 10G on vserver "vserver1"

EG cli result:

 

cluster1::*> vol show -vserver vserver1 -volume volume1 -fields volume, junction-path, aggregate, size, space-guarantee, security-style, state
vserver  volume  aggregate         size state  security-style junction-path space-guarantee
-------- ------- ----------------- ---- ------ -------------- ------------- ---------------
vserver1 volume1 node1_aggr1 10GB online ntfs           /volume1      none

Note: if you don't specifiy the volume security style the new volume will automatically inherit the security style of the vservers root volume.

 

There are many more options you might want to consider when creating volumes, EG dedupe, export policies etc. This is just an example to get you started, modify to meet your requirements. Hope this helps

 

/Matt

If this post resolved your issue, help others by selecting ACCEPT AS SOLUTION or adding a KUDO.

mbeattie
4,662 Views

Hi Jordan,

 

I thought it might also be helpful to provide an example for you on an alternative method to achieve the result via Ansible. Rather than writting a script,  you might consider writting a playbook. EG:

 

---
- hosts: localhost
  gather_facts: false
  name: Create Volume CIFS Share
  vars:
    login: &login
      hostname: cluster1.testlab.local
      username: admin
      password: N0tMyP@ssw0rd!
      https: true
      validate_certs: false
    vserver: vserver1
    volume: volume1
    aggregate: node1_aggr1
    size: 10
  tasks:
  - name: Create Volume
    na_ontap_volume:
      state: present
      is_online: yes
      name: "{{ volume }}"
      aggregate_name: "{{ aggregate }}"
      size: "{{ size }}"
      size_unit: gb
      space_guarantee: none
      vserver: "{{ vserver }}"
      volume_security_style: ntfs
      wait_for_completion: true
      junction_path: "{{ '/' + volume }}"
      <<: *login
  - name: Create CIFS Share
    na_ontap_cifs:
      state: present
      share_name: "{{ volume + '$' }}"
      path: "{{ '/' + volume }}"
      vserver: "{{ vserver }}"
      share_properties: browsable,oplocks,changenotify,show-previous-versions
      <<: *login

The playbook is just a configuration file in .yml format (no coding required), you then invoke the playbook and Ansible will deploy the configuration for you. EG i saved the above playbook as "create_volume_cifs_share.yml" and then execute it

 

[root@testux01 playbooks]# ansible-playbook create_volume_cifs_share.yml

PLAY [Create Volume CIFS Share] *********************************************************************************************************************************************************************

TASK [Create Volume] ********************************************************************************************************************************************************************************
changed: [localhost]

TASK [Create CIFS Share] ****************************************************************************************************************************************************************************
changed: [localhost]

PLAY RECAP ******************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Example CLI result:

 

cluster1::*> vol show -vserver vserver1 -volume volume1 -fields volume, junction-path, aggregate, size, space-guarantee, security-style
vserver  volume  aggregate         size security-style junction-path space-guarantee
-------- ------- ----------------- ---- -------------- ------------- ---------------
vserver1 volume1 node1_aggr1 10GB ntfs           /volume1      none

cluster1::*> cifs share show -vserver vserver1 -share-name volume1$ -fields volume, share-name, share-properties
vserver  share-name share-properties                                      volume
-------- ---------- ----------------------------------------------------- -------
vserver1 volume1$   browsable,oplocks,changenotify,show-previous-versions volume1

The advantage to this method is that you don't need to develop scripts or write any code. Just define the configuration. Perhaps this is an option for you to consider and explore? A good place to start to learn more if you have never used ansible before is this blog:

 

https://netapp.io/2018/10/11/getting-started-with-netapp-and-ansible-first-playbook-example/

 

Hope that helps? Please let me know if you have any questions?

 

/Matt

If this post resolved your issue, help others by selecting ACCEPT AS SOLUTION or adding a KUDO.
Public