ONTAP Rest API Discussions

PS script: New-NcExportRule giving rule index always first number

ansible_netapp_Provision
2,527 Views

When we execute export policy rule create through CLI, it always create "rule index" last number which is good and it should but when i did same using PS , "rule index" taking first number which is not needed. What is wrong here with PS? how can i solve it?

 

 

New-NcExportRule -ClientMatch "server1"  -ReadOnlySecurityFlavor "sys" -ReadWriteSecurityFlavor "sys"
1 ACCEPTED SOLUTION

mbeattie
2,474 Views

Hi,

 

The Index parameter of the "New-NcExportRule" CmdLet will assign the next "available" value, that is not necessarily the next concurrent index rule. To ensure the rule index is incremented you need to apply some programmatic logic if you don't want to accept the defaults. See the following example.

 

Syntax:

PS C:\> $credential = Get-Credential -Credential admin
PS C:\> .\AddExportRule.ps1 -Cluster cluster1.testlab.local -VserverName vserver2 -VolumeName nfs_data_001 -ClientMatch "192.168.108.0/24" -Credential $credential
Connected to cluster "cluster1.testlab.local" as user "admin"
Enumerated Volume "nfs_data_001" on vserver "vserver2"
Enumerate next concurrent export rule index "8" for policy "default" on vserver "vserver2"
Added export rule index "8" for clientmatch "192.168.108.0/24" to policy "default" for volume "nfs_data_001" on vserver "vserver2"

Source Code:

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 IP Address or IPv4 CIDR address")]
   [String]$ClientMatch,
   [Parameter(Mandatory = $False, HelpMessage = "The Read-Only rule type")]
   [ValidateSet("any","none","never","krb5","krb5i","krb5p","ntlm","sys")]
   [String]$RoRule="any",
   [Parameter(Mandatory = $False, HelpMessage = "The Read-Write rule type")]
   [ValidateSet("any","none","never","krb5","krb5i","krb5p","ntlm","sys")]
   [String]$RwRule="any",
   [Parameter(Mandatory = $False, HelpMessage = "The Protocol type")]
   [ValidateSet("any","cifs","flexcache","nfs","nfs2","nfs3","nfs4")]
   [String]$Protocol="nfs3",
   [Parameter(Mandatory = $True, HelpMessage = "The Credentials to authenticate to the cluster")]
   [System.Management.Automation.PSCredential]$Credential
)
#'------------------------------------------------------------------------------
#'Connect to the cluster.
#'------------------------------------------------------------------------------
Try{
   Connect-NcController -Name $Cluster -HTTPS -Credential $Credential -ErrorAction Stop | Out-Null
   Write-Host $("Connected to cluster ""$Cluster"" as user """ + $Credential.UserName + """")
}Catch{
   Write-Warning -Message $("Failed connecting to cluster ""$Cluster"". Error " + $_.Exception.Message)
   Break;
}
#'------------------------------------------------------------------------------
#'Enumerate the volume and export policy assigned to the volume.
#'------------------------------------------------------------------------------
Try{
   $volume = Get-NcVol -Name $VolumeName -Vserver $VserverName -ErrorAction Stop
   Write-Host "Enumerated Volume ""$VolumeName"" on vserver ""$VserverName"""
}Catch{
   Write-Warning -Message $("Failed enumerating volume ""$VolumeName"" on vserver ""$VserverName"". Error " + $_.Exception.Message)
   Break;
}
[String]$policy = $volume.VolumeExportAttributes.Policy
#'------------------------------------------------------------------------------
#'Increment the export policy rule index.
#'------------------------------------------------------------------------------
Try{
   [Int]$nextIndex = $(Get-NcExportRule -Vserver $VserverName -Policy $policy -ErrorAction Stop | Select-Object -ExpandProperty RuleIndex -Last 1) + 1
   Write-Host "Enumerate next concurrent export rule index ""$nextIndex"" for policy ""$policy"" on vserver ""$VserverName"""
}Catch{
   Write-Warning -Message $("Failed enumerating rule index for policy ""$policy"" on volume ""$VolumeName"" on vserver ""$VserverName"". Error " + $_.Exception.Message)
   Break;
}
#'------------------------------------------------------------------------------
#'Add the export policy rule.
#'------------------------------------------------------------------------------
Try{  
   New-NcExportRule -Policy $policy -ClientMatch $ClientMatch -Protocol $Protocol -ReadOnlySecurityFlavor $RoRule -ReadWriteSecurityFlavor $RwRule -VserverContext $VserverName -RuleIndex $nextIndex -ErrorAction Stop
   Write-Host "Added export rule index ""$nextIndex"" for clientmatch ""$ClientMatch"" to policy ""$policy"" for volume ""$VolumeName"" on vserver ""$VserverName"""
}Catch{
   Write-Warning -Message $("Failed adding export rule for clientmatch ""$ClientMatch"" to policy ""$policy"" for volume ""$VolumeName"" on vserver ""$VserverName"". Error " + $_.Exception.Message)
   Break;
}
#'------------------------------------------------------------------------------

Hope that helps

 

/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
2,475 Views

Hi,

 

The Index parameter of the "New-NcExportRule" CmdLet will assign the next "available" value, that is not necessarily the next concurrent index rule. To ensure the rule index is incremented you need to apply some programmatic logic if you don't want to accept the defaults. See the following example.

 

Syntax:

PS C:\> $credential = Get-Credential -Credential admin
PS C:\> .\AddExportRule.ps1 -Cluster cluster1.testlab.local -VserverName vserver2 -VolumeName nfs_data_001 -ClientMatch "192.168.108.0/24" -Credential $credential
Connected to cluster "cluster1.testlab.local" as user "admin"
Enumerated Volume "nfs_data_001" on vserver "vserver2"
Enumerate next concurrent export rule index "8" for policy "default" on vserver "vserver2"
Added export rule index "8" for clientmatch "192.168.108.0/24" to policy "default" for volume "nfs_data_001" on vserver "vserver2"

Source Code:

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 IP Address or IPv4 CIDR address")]
   [String]$ClientMatch,
   [Parameter(Mandatory = $False, HelpMessage = "The Read-Only rule type")]
   [ValidateSet("any","none","never","krb5","krb5i","krb5p","ntlm","sys")]
   [String]$RoRule="any",
   [Parameter(Mandatory = $False, HelpMessage = "The Read-Write rule type")]
   [ValidateSet("any","none","never","krb5","krb5i","krb5p","ntlm","sys")]
   [String]$RwRule="any",
   [Parameter(Mandatory = $False, HelpMessage = "The Protocol type")]
   [ValidateSet("any","cifs","flexcache","nfs","nfs2","nfs3","nfs4")]
   [String]$Protocol="nfs3",
   [Parameter(Mandatory = $True, HelpMessage = "The Credentials to authenticate to the cluster")]
   [System.Management.Automation.PSCredential]$Credential
)
#'------------------------------------------------------------------------------
#'Connect to the cluster.
#'------------------------------------------------------------------------------
Try{
   Connect-NcController -Name $Cluster -HTTPS -Credential $Credential -ErrorAction Stop | Out-Null
   Write-Host $("Connected to cluster ""$Cluster"" as user """ + $Credential.UserName + """")
}Catch{
   Write-Warning -Message $("Failed connecting to cluster ""$Cluster"". Error " + $_.Exception.Message)
   Break;
}
#'------------------------------------------------------------------------------
#'Enumerate the volume and export policy assigned to the volume.
#'------------------------------------------------------------------------------
Try{
   $volume = Get-NcVol -Name $VolumeName -Vserver $VserverName -ErrorAction Stop
   Write-Host "Enumerated Volume ""$VolumeName"" on vserver ""$VserverName"""
}Catch{
   Write-Warning -Message $("Failed enumerating volume ""$VolumeName"" on vserver ""$VserverName"". Error " + $_.Exception.Message)
   Break;
}
[String]$policy = $volume.VolumeExportAttributes.Policy
#'------------------------------------------------------------------------------
#'Increment the export policy rule index.
#'------------------------------------------------------------------------------
Try{
   [Int]$nextIndex = $(Get-NcExportRule -Vserver $VserverName -Policy $policy -ErrorAction Stop | Select-Object -ExpandProperty RuleIndex -Last 1) + 1
   Write-Host "Enumerate next concurrent export rule index ""$nextIndex"" for policy ""$policy"" on vserver ""$VserverName"""
}Catch{
   Write-Warning -Message $("Failed enumerating rule index for policy ""$policy"" on volume ""$VolumeName"" on vserver ""$VserverName"". Error " + $_.Exception.Message)
   Break;
}
#'------------------------------------------------------------------------------
#'Add the export policy rule.
#'------------------------------------------------------------------------------
Try{  
   New-NcExportRule -Policy $policy -ClientMatch $ClientMatch -Protocol $Protocol -ReadOnlySecurityFlavor $RoRule -ReadWriteSecurityFlavor $RwRule -VserverContext $VserverName -RuleIndex $nextIndex -ErrorAction Stop
   Write-Host "Added export rule index ""$nextIndex"" for clientmatch ""$ClientMatch"" to policy ""$policy"" for volume ""$VolumeName"" on vserver ""$VserverName"""
}Catch{
   Write-Warning -Message $("Failed adding export rule for clientmatch ""$ClientMatch"" to policy ""$policy"" for volume ""$VolumeName"" on vserver ""$VserverName"". Error " + $_.Exception.Message)
   Break;
}
#'------------------------------------------------------------------------------

Hope that helps

 

/Matt

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

ansible_netapp_Provision
2,468 Views

It is testing and working fine. Thank you  for quick help Matt.

Public