Calling an API using powershell

MrFlinstone 506 Reputation points
2020-08-07T13:58:31.257+00:00

Hi All.

I have a curl script to call an api, but would like to use Ps to call it in the form of a loop 100 times and store he output response code i.e 200 etc.

curl --location --request GET 'https://my-api-url/224/employee' \  
--header 'URN-Token: xxxxxxxxxxxxx' \  
--header 'URN: 224'  

I tried to run this on PS using the code below.

$urn = 224  
#$token = 'xxxxxxxxxxxxx'  
$url = "https://my-api-url/$urn/employee"  
$headers = @{  
    'urn' = '224'  
    'urn-token' = 'xxxxxxxxxxxxx'  
}  
Invoke-RestMethod  -Uri $url -Headers $headers  




  
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,455 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. soumi-MSFT 11,766 Reputation points Microsoft Employee
    2020-08-07T14:53:57.147+00:00

    @MrFlinstone , Thank you for reaching out. To call an Azure AD protected API, you can use something like:

    This sample uses client_credentials flow to fetch access-tokens from AAD.

    You can find the Powershell script here.

    Note: Please modify the api as per requirement, as here we used Graph API for the test.

    Hope this helps.

    Do let us know if this helps and if there are any more queries around this, please do let us know so that we can help you further. Also, please do not forget to accept the response as Answer; if the above response helped in answering your query.

    1 person found this answer helpful.