REST API and Powershell

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements

REST API and Powershell

Not applicable

Has anyone used Powershell to interact with REST API on PA500?

When I use the Invoke-RestMethod cmdlet to try and generate a key, I get back an empty response. If I run the keygen through a browser, it successfully returns a key. Running 6.1.1.

PS C:> Invoke-RestMethod -Uri http://<ip-of-PA500>/api/?type=keygen%26ampuser=<adminuser>%26amppassword=<adminpassword>

response

-------------

response

http://<ip-of-PA500>/api/?type-keygen&user=<adminuser>&password=<adminpassword>

<response status="success">

     <result>

          <key>

               <.....generated key.....>

          </key>

     </result>

</response>

1 accepted solution

Accepted Solutions

L2 Linker

Try this

$HostPA = "http://hostname"

[xml]$content = Invoke-WebRequest "$HostPA/api/?type=keygen&user=user-name&password=user-password"

$apiKey = $content.response.result.key

$apiKey

View solution in original post

3 REPLIES 3

L5 Sessionator

sphippen

URL encode for & is %26 I am not why you have that extra "amp" in there, may be that is causing the issue ?

L2 Linker

Try this

$HostPA = "http://hostname"

[xml]$content = Invoke-WebRequest "$HostPA/api/?type=keygen&user=user-name&password=user-password"

$apiKey = $content.response.result.key

$apiKey

That worked...although I'm puzzled as to why Invoke-RestMethod did not.

I retried the same steps you listed but replaced Invoke-WebRequest with Invoke-RestMethod. It worked as well.

Essentially, applying the xml bracket and assigning the output of the command to a variable, let me pull out the relevant information. However, I wasn't expecting it to work that way. I had hoped the cmdlet would output the entire XML stream to the display and not need me to specify the specific data portions.

  • 1 accepted solution
  • 8885 Views
  • 3 replies
  • 1 Likes
Like what you see?

Show your appreciation!

Click Like if a post is helpful to you or if you just want to show your support.

Click Accept as Solution to acknowledge that the answer to your question has been provided.

The button appears next to the replies on topics you’ve started. The member who gave the solution and all future visitors to this topic will appreciate it!

These simple actions take just seconds of your time, but go a long way in showing appreciation for community members and the LIVEcommunity as a whole!

The LIVEcommunity thanks you for your participation!