I'm using query string parameters in a Graph API request to fetch users from Azure AD, however none appear to be working when I execute it through Powershell.
Here's the documentation on the API: https://learn.microsoft.com/en-us/graph/query-parameters
Here's some details about the scenario:
The request: https://graph.microsoft.com/v1.0/users?$top=999&$select=userPrincipalName
Executed via Powershell using the Invoke-RestMethod command
It seems to work when I use the Graph Explorer: https://developer.microsoft.com/en-us/graph/graph-explorer
I assume your app registration is set up properly and you have given the appropriate permissions etc. got the token and make the restmethod call, and the call returns full user profiles and not just the upn. It would be helpful if you provided some code. Assuming the above, from the little additional info, probably powershell is replacing your $top and $select with blank or something, because it will likely treat those as powershell variables instead of literally put them in the request url. meaning you probably did something like invoke-restmethod -Uri "graphurl?$top=etcetc" change that to single quote to take the literal string eg. invoke-restmethod -Uri 'graphurl?$top=etcetc'
Related
I am running this code and its not returning the data to my $response variable as I expected.
$targetUrl = "https://www.tigerrunresort.com/vacation-rentals-homes-search.asp?txtStartDate=8%2F1%2F2022&txtEndDate=8%2F7%2F2022&categoryid=11317&destinationID=0&communityID=0&amenBedrooms=-1&maxPersons=0&advs=&sort=0¤tPage=1&flexibleDates=0&flexibleDatesThreshold=&searchAmenities=&showOnlySpecials=0"
$Response = Invoke-WebRequest -Uri $targetUrl
In rawcontent I should expect to see the string "Sorry" which does not show. What do I need to do to get it to return the values as I would see interactively?
Thanks!
You are not seeing it because it is not actually in the response. It is probably created and appended to the DOM by Javascript. Due to the Invoke-WebRequest not executing Javascript, it is never part of the response. If you are looking to orchestrate browsers (which do execute Javascript and offer access to the DOM) maybe you can try taking a look at Selenium (https://www.selenium.dev/)
For lauching a web request you can use for example start-process like this : start-process microsoft-edge:http://google.com/
My question is basically the title. I couldn't find such information reading this page and searching the web.
My scenario is the following: whenever I create a PR to master I add a tag with some information, like the lib version seen here:
Then, during the build process, I generate the release notes and would like to access that PR tag inside a task like I do with $(Build.BuildId) here:
How can I accomplish that? Something like $(PullRequest.Tag)
Thanks!
How can I accomplish that? Something like $(PullRequest.Tag) Thanks!
There's no predefined variable for Pull Request tag. (I use printenv command in CMD task to confirm this!)
Here're my workaround:
Use Powershell task to call this rest api, the response would contain the tag of specific PR
$url = "$($env:SYSTEM_TEAMFOUNDATIONSERVERURI)$env:SYSTEM_TEAMPROJECTID/_apis/git/repositories/$($env:BUILD_REPOSITORY_NAME)/pullRequests/$($env:SYSTEM_PULLREQUEST_PULLREQUESTID)/labels?api-version=5.1-preview.1"
$response = Invoke-RestMethod -Uri $url -Method Get -Headers #{
Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
}
Write-Host "##vso[task.setvariable variable=PullRequestTag;isOutput=true]$($response.value.name)"
Then pass the variable $response.value.name(come from rest api response, the name represents the tag name) to output variable PullRequestTag(custom variable) so that next tasks can access the returned tag name.
Notes:
1.Make sure the job which contains the Powershell task allow scripts to access the OAuth token cause my script uses OAuth token instead of PAT to call rest api. (Click Agent Job Name=>Additional options)
2.Since it's a output variable, we should use format $(referenceName.variablename).
After that, we can use $(PS.PullRequestTag) in next tasks to access the tag name.
3.Since your scenario is in a pipeline run triggered by PR, so actually the Powershell task should only run when current pipeline is triggered by PR instead of manual run/CI.
Use and(succeeded(), eq(variables['Build.Reason'], 'PullRequest')) in PS task's control options. See conditions and Build.Reason variable.
Update:
If I added several tags when creating the PR:
The $(PS.PullRequestTag)'s value would be:
I doubt there's a predefined variable for a pull request tag, but you can achieve the goal with the REST API. For example, you can have a build task (e.g. a PowerShell script) which:
gets all tags (aka labels) of a pull request
parses the list of tags to find out the necessary one
set the build variable to the name of the necessary tag
Powershell [ Invoke-WebRequest] is it posible to make copy of Session from a certain point and then re-use session from the saved state?
I have no problem logging in and completing the full request.
The question is that I should perform hundreds of similar requests and to make a selection, for each parameter I send a separate post request (there are eight of post request) and I have to execute these requests sequentially.
the first four are identical for everyone, so, is it possible to save state of session after these four requests in copy of variable and then use it?
i tried to make copy of session using
$sessionAfterBasicRequest=$session.PSObject.Copy()
but it's not working
maybe I'm missing basic things...
I'm having trouble understanding what you're trying to do, but I think you want to maintain a single session for these many requests.
See this example for Invoke-WebRequest, explaining how to maintain the session foir a stateful web service when using `Invoke-WebRequest. I'll repeat the content below for posterity, since this example already explains what is going on:
$Body = #{
User = 'jdoe'
password = 'P#S$w0rd!'
}
$LoginResponse = Invoke-WebRequest 'https://www.contoso.com/login/' -SessionVariable 'Session' -Body $Body -Method 'POST'
$Session
$ProfileResponse = Invoke-WebRequest 'https://www.contoso.com/profile/' -WebSession $Session
$ProfileResponse
The first call to Invoke-WebRequest sends a sign-in request. The command specifies a value of "Session" for the value of the -SessionVariable parameter, and saves the result in the $LoginResponse variable. When the command completes, the $LoginResponse variable contains an BasicHtmlWebResponseObject and the $Session variable contains a WebRequestSession object. This logs the user into the site.
The call to $Session by itself shows the WebRequestSession object in the variable.
The second call to Invoke-WebRequest fetches the user's profile which requires that the user be logged into the site. The session data stored in the $Session variable is used to provide session cookies to the site created during the login. The result is saved in the $ProfileResponse variable.
The call to $ProfileResponse by itself shows the BasicHtmlWebResponseObject in the variable.
I am starting to use PowerShell to call the Azure DevOps REST API. But it seems like when I try to add parameters it tell me:
A parameter cannot be found that matches parameter name
'repositoryId'
Here is what my call looks like in PowerShell. If I take out the parameter it works. What am I doing wrong?
Invoke-RestMethod -Uri 'https://dev.azure.com/{organization}/{project}/_apis/build/builds?api-version=5.1' -repositoryId $repoId -Headers (my authentication) -Method Get
Per Microsoft's documentation this should work.
https://learn.microsoft.com/en-us/rest/api/azure/devops/build/builds/list?view=azure-devops-rest-5.1
repositoryId should be url parameter as Booga Roo mentioned. The error indicated that Repository type is missing.
You should add another parameter to your uri repositoryType={repositoryType}.So the uri should be like below.
Please check here for all repositoryTypes
https://dev.azure.com/{Organization}/{Project}/_apis/build/builds?repositoryId={id}&repositoryType=TfsGit&api-version=5.1
Addition:
You can get your repositoryId from URL of Repositories page under Repos in the Project Settings. Check below screentshot.
The Invoke-RestMethod cmdlet does not have a -repositoryId parameter. The phrasing and examples on the help page are for "URI Parameters" instead of PowerShell parameters. It means you need to build it into -Uri value instead of trying to use it directly.
I suggest using this:
Invoke-RestMethod -Uri "https://dev.azure.com/{organization}/{project}/_apis/build/builds?repositoryId={$repoId}&api-version=5.1" -Headers (my authentication) -Method Get
Side note: There are double quotes around this example URI. This is so the variable expansion for $repoId will occur and be properly interpreted as part of the URI. Using single quotes as in the original example will prevent this and treat it as a literal string value and won't perform any subsitutions.
I'm interfacing with an API with PowerShell Invoke-RestMethod cmdlet.
One of the API endpoints requires the method "Link", however this method is not supported by Invoke-RestMethod.
With CURL it is working fine
Is there a way around this?
Error message:
Invoke-RestMethod : Cannot bind parameter 'Method'. Cannot convert value "LINK" to type "Microsoft.PowerShell.Commands.WebRequestMethod". Error: "Unable to match the identifier name LINK to a valid enumerator name. Specify one of the following enumerator names and try again:
Default, Get, Head, Post, Put, Delete, Trace, Options, Merge, Patch
In PowerShell v6+, Invoke-WebRequest has a parameter CustomMethod.
This can be used with the Request Method required by the endpoint is not an available option on the -Method.
Look at the MS documentation on how to use custom method in invoke-webrequest
Update
You can use .Net class WebRequest to send your request as follows, and this will work on any PowerShell version:
$req = [net.webrequest]::create('https://www.google.com')
$req.Method = 'LINK'
$resp = $req.GetResponse()
Look here for further information