Can one use the REST API with PowerShell's 'Invoke-WebRequest'?

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

Welcome Idira to LIVEcommunity!

Our community just got stronger! Explore new identity security resources, connect with experts, and discover more ways to learn and collaborate.

Meet Idira

Can one use the REST API with PowerShell's 'Invoke-WebRequest'?

L2 Linker

I tried to connect to our PVWA with PowerShell's 'Invoke-WebRequest'. ​

$CA = 'https://pvwaLink.com/PasswordVault/' $response = Invoke-WebRequest -Uri $CA -SessionVariable UrlSession # Filling in the name and password to login (not the real credentials) $response.Forms[0].Fields["pvBody_PageTemplate_innerHolder_ctrlLogon_txtUsername"]="admin" $response.Forms[0].Fields["pvBody_PageTemplate_innerHolder_ctrlLogon_txtPassword"]="admin" #Authenticate $UrlResponse = Invoke-WebRequest -Uri ($CA + $response.Forms[0].Action) -WebSession $UrlSession -Method POST -Body $response.Forms[0].Fields

The result I get for the variabl $UrlResponse is:

StatusCode : 200

StatusDescription : OK

Content : {...}

RawContent : HTTP/1.1 200 OK

Pragma: ...

X-UA-Compatible: ...

X-XSS-Protection: ..; mode=...

X-Content-Type-Options: ....

X-FRAME-Options: ....

Access-Control-Expose-Headers: ...

Headers : {[]}

RawContentLength : ...

 

I saw the material for Postman but I was wondering whether this approach is feasible as well.

1 accepted solution

Accepted Solutions

L6 Presenter

PowerShell Invoke-WebRequest (or Invoke-RestMethod) can certainly be used.

You should however refer to the API documentation; as far as I can tell, the code you have posted does not interact with the API.

 

An example authentication request to the (classic) API could look like this:

$url = "https://PVWA/PasswordVault/WebServices/auth/Cyberark/CyberArkAuthenticationService.svc/Logon" $body = New-Object -Property @{"username" = "YourUser"; "password" = "SomePassword" } -TypeName psobject | ConvertTo-Json   Invoke-RestMethod -Uri $url -Method POST -ContentType "application/json" -Body $body

Other API endpoints for the version 10 API are available (dependent on your PAS version).

 

This module may save you some time: https://www.powershellgallery.com/packages/psPAS/

 

View solution in original post

1 REPLY 1

L6 Presenter

PowerShell Invoke-WebRequest (or Invoke-RestMethod) can certainly be used.

You should however refer to the API documentation; as far as I can tell, the code you have posted does not interact with the API.

 

An example authentication request to the (classic) API could look like this:

$url = "https://PVWA/PasswordVault/WebServices/auth/Cyberark/CyberArkAuthenticationService.svc/Logon" $body = New-Object -Property @{"username" = "YourUser"; "password" = "SomePassword" } -TypeName psobject | ConvertTo-Json   Invoke-RestMethod -Uri $url -Method POST -ContentType "application/json" -Body $body

Other API endpoints for the version 10 API are available (dependent on your PAS version).

 

This module may save you some time: https://www.powershellgallery.com/packages/psPAS/

 

  • 1 accepted solution
  • 2615 Views
  • 1 replies
  • 0 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!