I am trying to make a rest API call using "invoke-webrequest" in azure powershell runbook.
At first, I got the error message regarding the Internet Explorer Engine. When I searched about it, I found that using **-UseBasicParsing** would solve the problem. However, after using it, I am not facing the error message anymore but I am getting empty "**Parsed-Html**".
The code is running fine when I am running it locally.
$result = invoke-webrequest -uri $url -Headers $Header -UseBasicParsing
I expect the output of Parsed-Html to be an object, but it is null.
In Automation, when we use cmdlet Invoke-WebRequest we need to use the–UseBasicParsing option as we do not have the Internet Explorer in Azure Automation sandboxes.
In this scenario you should use **HtmlAgilityPack**. Upload the **HtmlAgilityPack.dll** as module and use command "add-type -Path .\HtmlAgilityPack.dll " in runbook to generate the **HtmlDocument**
Code would look something like below:
$URI = "https://www.google.fr/search?q=googe&rlz=1C1CHBF_frFR711FR711&oq=googe&aqs=chrome..69i57j69i60l5.912j0j7&sourceid=chrome&ie=UTF-8#q=google"
$wc = New-Object System.Net.WebClient
$htmlString=$wc.DownloadString($URI)
cd C:\Modules\User\HtmlAgilityPack
add-type -Path .\HtmlAgilityPack.dll
$doc = New-Object HtmlAgilityPack.HtmlDocument
$doc.LoadHtml($htmlString)
$root= $doc.DocumentNode
$root.SelectSingleNode("//head/title").Innertext
You can check this link for further reference.
Hope it helps.
Related
I'm just missing data for my usage details invoke-restmethod call. If I do this using postman or thunderclient it gives me the full details in json format. But when I set this up using powershell and export it it a .csv file I am missing part of my data.
Help?
usage details api: https://learn.microsoft.com/en-us/rest/api/consumption/usage-details/list
The Invoke-RestMethod cmdlet sends HTTP and HTTPS requests to Representational State Transfer (REST) web services that return richly structured data.
PowerShell formats the response based to the data type.
You are missing some of your data because when the REST endpoint returns multiple objects, the objects are received as an array. If you pipe the output from Invoke-RestMethod to another command, it is sent as a single [Object[]] object. The contents of that array are not listed one by one for the next command on the pipeline.
Please check this Invoke-RestMethod document for more information.
The way you can solve the issue is by converting the output result to a CSV by using ConvertTo-CSV cmdlet. You have use format-table in PowerShell and specify which you want export or view. If you there are internal elements you need to do a loop and export it.
Please check this example of ConvertTo-CSV documentation for more information.
I'm trying to download all the test step attachments for a test case through API Call using GET https://dev.azure.com/{organization}/{project}/_apis/test/Runs/{runId}/Results/{testCaseResultId}/attachments/{attachmentId}?api-version=6.0-preview.1.
My call works but where do I find the attachment in my files?
How Do I Download Test Step Attachments for Test Cases in Azure DevOps
That because the REST API Attachments - Get Test Result Attachment Zip
will display the context of the attachments instead of download the attachments directly.
To resolve this issue, we could save the attachment to the file by the scripts like:
$AttachmentsOutfile = "D:\Testcase\AttachmentsOutfile.trx"
$connectionToken="You PAT Here"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::
ASCII.GetBytes(":$($connectionToken)"))
$AuditLogURL = "https://dev.azure.com/{organization}/{project}/_apis/test/Runs/{runId}/Results/{testCaseResultId}/attachments/{attachmentId}?api-version=6.0-preview.1"
$AuditInfo = Invoke-RestMethod -Uri $AuditLogURL -Headers #{authorization = "Basic $base64AuthInfo"} -Method Get –OutFile $AttachmentsOutfile
The test result:
I'm trying to create queries across several projects. I've got 7 or 8 queries in total and a dozen or so projects so thought that having some sort of script may make it easier.
I've come up with this (basically copy and paste from somewhere else admittedly) but I have no idea where/how to run it, and whether I've even got everything. I'm assuming that I haven't because I don't have the PAT token but I'm really not sure.
I'm completely out of my comfort zone with this but if I get one right then I'm hoping the rest will be easy because I'm just amending the query. Could anyone offer a bit of help? Should I be using Visual Studio Code or something else to run this? Do I just drop this code in or am I missing something key??
POST https://dev.azure.com/MattR778/ProjectA/_apis/wit/queries/{query}?api-version=5.0
{ "name": "No Status Change", "wiql": "Select [System.Id], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.State], [System.AreaPath], [System.IterationPath]
From WorkItems Where [System.TeamProject] = #project AND [System.WorkItemType] = 'User Story' AND [System.State] = 'New' AND [Microsoft.VSTS.Common.StateChangeDate]
< #startofday( '-90d') }
I thought I had it, and I got an error message because I hadn't put a " towards the end (rookie mistake). I've re-run it but I still can't get it to work. I feel like it's really close but I tried running the sample request on https://learn.microsoft.com/en-us/rest/api/azure/devops/wit/queries/create?view=azure-devops-rest-6.0 and it had a similar response so I don't think it's the body.
I'm getting back this message:
{
"$id": "1",
"innerException": null,
"message": "TF401243: The query {query} does not exist, or you do not have permission to read it.",
"typeName": "Microsoft.TeamFoundation.WorkItemTracking.Server.QueryItems.QueryItemNotFoundException, Microsoft.TeamFoundation.WorkItemTracking.Server",
"typeKey": "QueryItemNotFoundException",
"errorCode": 600288,
"eventId": 3200
}
I've added the PAT to the authorization tab, so not sure what the issue is. I think it's nearly there, and I've been able to link into my DevOps project before with a simple GET test (so I don't think it's an issue with the connection between Postman and DevOps?), but I'm a little unsure of where to go next with this.
The full screenshot of what I've tried to run in Postman
You can run REST API in PowerShell.
Here is the script.
$token = "{PAT}"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$url="https://dev.azure.com/MattR778/ProjectA/_apis/wit/queries/{query}?api-version=5.0"
$body = #'
{
"name": "No Status Change",
"wiql": "Select [System.Id], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.State], [System.AreaPath], [System.IterationPath] From WorkItems Where [System.TeamProject] = #project AND [System.WorkItemType] = 'User Story' AND [System.State] = 'New' AND [Microsoft.VSTS.Common.StateChangeDate]< #startofday( '-90d')
}
'#
$head = #{ Authorization =" Basic $token" }
Invoke-RestMethod -Uri $url -Method Post -Headers $head -Body $body -ContentType application/json
You can run the script in PowerShell or the PowerShell task in Azure DevOps pipeline.
In addition, you can also use some softwares designed to run APIs, like Postman.
Update:
In Postman, if you want to authenticate with PAT, you need to select "Basic Auth" in "Authorization" and enter PAT in the "Password".
In "Body", you need to select "raw" and choose "JSON". Then, paste your request body in the testbox.
The scripts above are for command-line tools such as PowerShell or Bash. If you are using Postman, you'd just need to provide the request body.
Update 2:
Recently we have moved our web application from one server to another in Sharepoint 2010. Back up of entire web application was taken and restored in another server. But in the new server , the PDF files in the document library is not getting opened in browser. it always open in browser
I have already made following changes but didn,t work
Set browser file handling to Permissive from central admin
Set "open in browser" in setting s of doc library
Set the doc library file handling property using
$docLib = $web.lists["Your Document Library Title"]
$docLib.BrowserFileHandling = "Permissive"
$docLib.Update()
Added "AllowedInlineDownloadedMimeType.Add("Application/Pdf") in web app
Installed Adober eader in client machine
Even after trying all these, the PDF files are still opening in Client application(Adobe reader) but not in the browser
It would have been great help if anybody provide a solution for this. I have been banging head on this for two days
Regards
Vishnu
You need to add the MIME type to SharePoint 2010 to enable this - and it needs to be done at Farm level.
The powershell on here will do it; (I've not run it, but it is Technet so source should be good)
# <#
# .DESCRIPTION
#
# This script adds new MIME type to "AllowedInlineDownloadedMimeTypes" property list of defined SharePoint 2010 Web Application.
#
# Script prompts you for MIME type and Web Application URL.
#
# Code shall run in context of Farm Administrators group member.
#
# .NOTES
# File Name : Add_MIME_Type.ps1
# Author : Kamil Jurik, WBI Systems a.s.
# Created : 11/12/2011
#
If ( (Get-PSSnapin -Name "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null ) {
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
Get-SPWebApplication
$WebApp = Get-SPWebApplication $(Read-Host "`nEnter Web Application URL")
Write-Host `n"Mime Type Examples:"`n"application/pdf, text/html, text/xml"`n
If ($WebApp.AllowedInlineDownloadedMimeTypes -notcontains ($MimeType = Read-Host "Enter a required mime type"))
{
Write-Host -ForegroundColor White `n"Adding" $MimeType "MIME Type to defined Web Application"$WebApp.url
$WebApp.AllowedInlineDownloadedMimeTypes.Add($MimeType)
$WebApp.Update()
Write-Host -ForegroundColor Green `n"The" $MimeType "MIME type has been successfully added."
} Else {
Write-Host -ForegroundColor Red `n"The" $MimeType "MIME type has already been added."
}
You can set the Browse file handling property to permissive
http://technet.microsoft.com/en-us/library/cc262107(v=office.14).aspx
I have 100 web servers and noticed that some of them have request filtering enabled for ".exe" extension. This means that if a user wants to access a .exe file with an application we ave it is blocked by the server.
Is there a way of doing any type of command line or script whereby I know which servers are affected? Currently I am testing by doing a manual check by going to internet explorer and typing in:
http://***SERVER1***/website/OFFICEPRO/2007/Office%20Professional/setup.exe
http://***SERVER2***/website/OFFICEPRO/2007/Office%20Professional/setup.exe
If I get a pop-up box stating to enter my credentials then that works. Hwoever if I get "Error Page (404)" then that does not work.
Thanks,
You can use powershell to do this, the following should get you on your way:
$webClient = new-object Net.WebClient
gc servers.txt | Foreach-Object {
# send credentials to server
$webClient.UseDefaultCredentials = $true;
$url = "http://$_/website/OFFICEPRO/2007/Office%20Professional/setup.exe"
try
{
$reult = $webClient.DownloadString($url)
Write-host -fore Green $url "OK"
}
catch [System.Net.WebException]
{
Write-host -fore Red $url $_
}
}
The file named servers.txt contains the list of servers you want to check.