Forum Discussion
PowerShell Help with JSON and Invoke-WebRequest
This is the F5
$MyLTM_IP = '10.5.183.4'
Load the SnapIn first
Add-PSSnapIn iControlSnapIn
Need to create Login
$secpasswd = ConvertTo-SecureString 'password1' -AsPlainText -Force$mycreds = New-Object System.Management.Automation.PSCredential ('user1', $secpasswd)
Initialize-F5.iControl first by logging into F5
Initialize-F5.iControl -HostName "$MyLTM_IP" -PSCredentials $mycreds
This shows the available partitions
(Get-F5.iControl).ManagementPartition.get_partition_list()
This command shows all of the actions available to the F5.
Invoke-WebRequest -Method GET -URI '[https://10.5.183.4/mgmt/tm/ltm](https://10.5.183.4/mgmt/tm/ltm)'-Credential $mycreds
A modification of an existing object requires the PUT method, POST is for object creation.
Fails:
Invoke-WebRequest -Method POST -Uri '[https://10.5.183.4/mgmt/tm/ltm/pool](https://10.5.183.4/mgmt/tm/ltm/pool)' { "name":"cpg-pool2", "partition":"CL-COM_SELFSERV", "members" : [{"name":"10.5.183.199", "description":"Web server"}] }
Fails:
Invoke-WebRequest -Method POST -Uri '[https://10.5.183.4/mgmt/tm/ltm/pool/~CL-COM_SELFSERV~cpg-pool2](https://10.5.183.4/mgmt/tm/ltm/pool/~CL-COM_SELFSERV~cpg-pool2) [{"name":"10.5.183.199", "description":"Web server"}]'
Fails:
Invoke-WebRequest -Method POST -Uri '[https://10.5.183.4/mgmt/tm/ltm/node/~CL-COM_SELFSERV](https://10.5.183.4/mgmt/tm/ltm/node/~CL-COM_SELFSERV)' [{"name" : "10.5.183.199", "description" : "Web server"}]'
PS U:> Invoke-WebRequest -Method POST -Uri '[https://10.5.183.4/mgmt/tm/ltm/pool/~CL-COM_SELFSERV~cpg-pool2](https://10.5.183.4/mgmt/tm/ltm/pool/~CL-COM_SELFSERV~cpg-pool2) [{"name":"10.5.183.199", "description":"Web server"}]'
Invoke-WebRequest : The remote server returned an error: (401) Unauthorized.At line:1 char:1
+ Invoke-WebRequest -Method POST -Uri '[https://10.5.183.4/mgmt/tm/ltm/pool/~CL-COM](https://10.5.183.4/mgmt/tm/ltm/pool/~CL-COM) ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
I am trying to add a new Node 10.5.183.199 to the existing Pool cpg-pool2 that is located in the Partition CL-COM_SELFSERV. I have tried different variations and the Invoke-WebRequest works for other commands.
What am I missing other than not understanding where the ~ is used and when [ ] JSON is used?
6 Replies
Hi there!
Looks like you're mixing REST and the snap-in.
Also looks like you missed the pre-formatted code option in the forum which would make all the jumple above actually readable.
I'll make you a deal:
If you paste all your future questions in a proper format, I'll help you with the script. :)
/Patrik
Try this if you want to use the iControl snapin:
if ( (Get-PSSnapin | Where-Object { $_.Name -eq "iControlSnapIn"}) -eq $null ){ Add-PSSnapIn iControlSnapIn } If you don't want to use TLS1.2, comment the line below [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 Authenticate $Success = Initialize-F5.iControl -Hostname 10.5.183.4 -Username "user1" -Password "password1" Get an iControl Handle $F5 = Get-F5.iControl $Poolname = "/CL-COM_SELFSERV/cpg-pool2" $Member = New-Object -TypeName iControl.CommonAddressPort $Member.address = "/CL-COM_SELFSERV/10.5.183.199" $Member.port = 80 $f5.LocalLBPool.add_member_v2(@($Poolname), @(@($Member)))
/Patrik
- cgaines22_14499
Nimbostratus
This Works [example](Invoke-WebRequest -Method GET -URI 'https://10.5.183.4/mgmt/tm/ltm/pool/?$filter=partition eq Certlab' -Credential $mycreds)
- Brad_Parker
Cirrus
For what its worth I would recommend using Invoke-RestMethod rather than Invoke-WebRequest. As for your question about getting a 401. You will need to authenticate with a get request before you send a PUT or POST. I'm not sure why this is the case, but it works. - cgaines22_14499
Nimbostratus
Thanks Brad that changed the look of the Get request to a short return versus using Invoke-WebRequest. They both return values but Invoke-RestMethod seems to only refer to the object in the URI not the whole URI list. I will try the Get statement before the Post statement and see what occurs.
- cgaines22_14499
Nimbostratus
This builds 1 new Pool and puts 1 new Node in that Pool. Invoke-RestMethod and Invoke-WebRequest both work one returns more information in the final output. The node did not receive the description so I will have to keep on working on that part.
Invoke-RestMethod -Method POST -Uri "https://10.5.183.4/mgmt/tm/ltm/pool" -Credential $mycreds -Body '{"name":"cpg-pool2","partition":"CL-COM_SELFSERV","members":[{"name":"10.5.183.199:80","address":"10.5.183.199","description":"Web server"}]}' -Headers @{"Content-Type"="application/json"}
Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com