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.