Trying to add persistence profile in existing virtual server

I am trying to add persistence profile in an existing Virtual server but not able to do so. It is showing some errors.

Add-PSSnapIn iControlSnapIn
Initialize-F5.iControl -Hostname "hostname" -Username "username" -Password "password"

$virtual_name = New-Object iControl.CommonVirtualServerDefinition
$virtual_name.name="app035"
$virtual_name.address="0.0.0.0"
$virtual_name.port=80
$vir_server = New-Object iControl.LocalLBVirtualServerVirtualServerPersistence
$vir_server.default_profile = $false
$vir_server.profile_name="cookie"

(Get-F5.iControl).LocalLBVirtualServer.add_persistence_profile($virtual_name,$vir_server)

Error this time is -

Exception calling "add_persistence_profile" with "2" argument(s): "Exception caught in 
LocalLB::urn:iControl:LocalLB/VirtualServer::add_persistence_profile()
Exception: Common::OperationFailed
    primary_error_code   : 16908342 (0x01020036)
    secondary_error_code : 0
    error_string         : 01020036:3: The requested virtual server 
(/Common/iControl.CommonVirtualServerDefinition) was not found."
At line:12 char:1
+ (Get-F5.iControl).LocalLBVirtualServer.add_persistence_profile($virtu ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SoapHeaderException

I am making some mistake not sure what exactly is that. Could someone please help me out with this ?

I do not know PowerShell, but from the error message,

The requested virtual server 
(/Common/iControl.CommonVirtualServerDefinition) was not found

, I would say that the virtual does not exist. Check if exists.

If that’s not the case, I suspect the data format of the persistence profile(s). It must be given in array (

[]

). For example, to perform the task equivalent to the following tmsh command (adding

cookie

persistence profile to the virtual

vs

:disappointed_face:

tmsh modify ltm virtual vs persist replace-all-with { cookie }

You need to PATCH the

/mgmt/tm/ltm/virtual/vs

endpoint with the data

{"persist":[ {"name":"cookie"} ]}

.

A curl call looks like this:

curl -sku : https:///mgmt/tm/ltm/virtual/vs \
  -X PATCH -H "Content-type: application/json" \
  -d '{"persist":[ {"name":"cookie"} ]}'