Microsoft Virtualization Discussions

Scripting SVM Creation - Set-NcVserver -AllowedProtocols <input from hash table>

mcjunkin
1,929 Views

Trying to setup a script using a hash table to create a new SVM. After the SVM is created I'm trying to set the allowed protocols with a comma seperated value from a hash table. It errors out with invalid value specifed. The only way I can get it to work is if I hard code the protocols in the script. Individually all other aspects of the command work as expected. Any help is grealy apprecidate. 

 

Hash table values: 

VserverName=usadbashb1tst

Aggregates=data_*

AllowedProtocol=nfs,iscsi <--I have also tried 'nfs','iscsi' with the same result

 

D:\>Set-NcVserver -Name $values.VserverName -Aggregates $values.Aggregates -AllowedProtocols $values.AllowedProtocol

Set-NcVserver : Invalid value specified for "allowed-protocols" element within "vserver-modify": "'nfs','iscsi'".
At line:1 char:1
+ Set-NcVserver -Name $values.VserverName -Aggregates $values.Aggregate ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (usadbashb1000:NcController) [Set-NcVserver], EINVALIDINPUTERROR
+ FullyQualifiedErrorId : ApiException,DataONTAP.C.PowerShell.SDK.Cmdlets.Vserver.SetNcVserver

1 REPLY 1

mbeattie
1,913 Views

Hi,

I think it's because the "-AllowProtocols" must be formatted as an array. For example:

 

#'------------------------------------------------------------------------------
#'Function to format an array as a string.
#'------------------------------------------------------------------------------
Function Format-Array{
   Param(
      [Parameter(Mandatory=$False, HelpMessage="An array to format as a string expression")]
      [Array]$Array
   )
   [String]$result = "@("
   For($i = 0; $i -lt $Array.length; $i++){
      [String]$result += "'" + $Array[$i] + "'"
      If(($i + 1) -lt $Array.length){
         [String]$result += ","
      }
   }
   [String]$result += ")"
   Return $result
}
#'------------------------------------------------------------------------------
#'Set the allowed protocols.
#'------------------------------------------------------------------------------
[String]$VserverName = "vserver1"
[Array]$protocols    = @("cifs","nfs","iscsi")
[String]$command     = $("Set-NcVserver -Name $VserverName -AllowedProtocols " + $(Format-Array -Array $protocols) + " -ErrorAction Stop")
Try{
   Invoke-Expression -Command $command -ErrorAction Stop
   Write-Host "Executed Command`: $command"
}Catch{
   Write-Warning -Message $("Failed Executing Command`: $command. Error " + $_.Exception.Message)
   Break;
}
#'------------------------------------------------------------------------------

The output of the command is:

 

Executed Command: Set-NcVserver -Name vserver1 -AllowedProtocols @('cifs','nfs','iscsi') -Error
Action Stop

Note: The "-AllowedProtocols" paramater is expressed as an array

 

If you are using a hashtable as your input for the allowed protcols you can assign the keys to an array and format it as expression. For Example:

 

#'------------------------------------------------------------------------------
#'Function to format an array as a string.
#'------------------------------------------------------------------------------
Function Format-Array{
   Param(
      [Parameter(Mandatory=$False, HelpMessage="A comma delimited string to format as an array")]
      [Array]$Array
   )
   [String]$result = "@("
   For($i = 0; $i -lt $Array.length; $i++){
      [String]$result += "'" + $Array[$i] + "'"
      If(($i + 1) -lt $Array.length){
         [String]$result += ","
      }
   }
   [String]$result += ")"
   Return $result
}
#'------------------------------------------------------------------------------
#'Set the protocols hashtable.
#'------------------------------------------------------------------------------
[Hashtable]$protocols = @{};
[Hashtable]$protocols.Add("cifs", 1)
[Hashtable]$protocols.Add("nfs", 1)
[Hashtable]$protocols.Add("iscsi", 1)
[Array]$allowedProtocols = $protocols.Keys
#'------------------------------------------------------------------------------
#'Set the allowed protocols.
#'------------------------------------------------------------------------------
[String]$VserverName = "vserver1"
[String]$command     = $("Set-NcVserver -Name $VserverName -AllowedProtocols " + $(Format-Array -Array $allowedProtocols) + " -ErrorAction Stop")
Try{
   #Invoke-Expression -Command $command -ErrorAction Stop
   Write-Host "Executed Command`: $command"
}Catch{
   Write-Warning -Message $("Failed Executing Command`: $command. 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.
Public