not able to delete test cases in azure devops through powershell scripts - azure

I have checked How to delete multiple test cases in Azure DevOps
It not works for me.
Using PowerShell scripts alone, I want to delete multiple test cases in one go in Azure DevOps. Currently, portal only allows to delete one at a time.
I have tried like below way, and throws exceptions.
$url = "https://dev.azure.com/testarulmouzhie/testDemo_Project/_apis/test/testcases/21?api-version=5.0-preview.1"
Invoke-RestMethod -Uri $url -Method Delete -ContentType application/json
it throws error like below one-
Even tried with the new api version, same error comes-
$url = "https://dev.azure.com/testarulmouzhie/testDemo_Project/_apis/test/testcases/21?api-version=5.1-preview.1"
Invoke-RestMethod -Uri $url -Method Delete -ContentType application/json
Attached the error for ref-
Invoke-RestMethod :
Azure DevOps
Service Status Support #AzureDevOps
At line:1 char:1
+ Invoke-RestMethod -Uri $url -Method Delete -ContentType application/json
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc
eption
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
Can anyone help to solve this?
Thanks in advance.
For ref, anyway simple GET rest api calls works fine. i have tried below one and those are working fine.
$AzureDevOpsPAT = "a2wzly2bsirXXXXXXXXXXXXXXXXXXXXXXXXXXX"
$OrganizationName = "testarulmouzhie"
$AzureDevOpsAuthenicationHeader = #{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($AzureDevOpsPAT)")) }
$UriOrga = "https://dev.azure.com/$($OrganizationName)/"
$uriAccount = $UriOrga + "_apis/projects?api-version=5.1"
Invoke-RestMethod -Uri $uriAccount -Method get -Headers $AzureDevOpsAuthenicationHeader
Even used fiddler and tried to capture error logs- attached those too

not able to delete test cases in azure devops through powershell scripts
It seems you do not have the Test Plans license to use the REST API Test Cases - Delete.
Azure Test Plans uses an access level called Basic + Test Plans, you need a Basic + Test Plans license to manage the test plans and test suites, etc. Please check the following link:
Manual test permissions and access
Here are two ways of ensuring you have the right license.
1) The user is assigned a test manager extension license - https://marketplace.visualstudio.com/items?itemName=ms.vss-testmanager-web
2) The user has a VS Enterprise or Test Professional subscription
You can do this by accessing test hub and trying to add test cases directly there with the same user (account create the PAT)as is used in the REST API.
Check this thread for some details.
Hope this helps.

Ah I think you have missed your the autorizationheader in your delete Invoke API
Please include the -Headers $AzureDevOpsAuthenicationHeader like below
--give below your pat access token
$AzureDevOpsPAT = "ukcvd42u5rXXXXXXXXXXXXXXXXXXX";
$AzureDevOpsAuthenicationHeader = #{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($AzureDevOpsPAT)")) };
$url = "https://dev.azure.com/testarulmouzhie/testDemo_Project/_apis/test/testcases/21?api-version=5.0-preview.1"
Invoke-RestMethod -Uri $url -Method Delete -ContentType application/json -Headers $AzureDevOpsAuthenicationHeader;
As Leo suggested it also maybe with the license, so how to check your license for the test cases?

Related

Azure devops automate target vm deletion from deployment group

There is a method available for deleting a target machine from an azure deployment group using the API and its documentation can be seen here.
Is there a similar functionality available as a powershell script?
There isn't a PowerShell module available for Azure DevOps. However, you can invoke the same Azure DevOps REST APIs through the Invoke-RestMethod cmdlet.
Here is how:
$Username =""
$Password="<Personal-Access-Token>"
$Tokens = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $Username,$Password)))
$Authheader = #{Authorization = 'Basic ' + $Tokens }
# Targets - Delete REST API
$Uri = "https://dev.azure.com/{organization}/{project}/_apis/distributedtask/deploymentgroups/{deploymentGroupId}/targets/{targetId}?api-version=6.0-preview.1"
Invoke-RestMethod -Uri $Uri -Method delete -Headers $Authheader

Get Azure DevOps service connection service principal id with powershell

I am working on automating Azure Active Directory App Registrations and Azure Devops Service Connections, and have hit a wall.
I want to query Azure DevOps service connections (service endpoints) by Service Principal ID (or at least get the id). This is possible when using Azure CLI:
az devops service-endpoint list --query "[?authorization.parameters.serviceprincipalid=='xxx']"
But since I am running this in Azure automation account as a powershell runbook, the Azure CLI is not supported.
Then I tried the Azure DevOps REST API, and called it from powershell, but the response does not contain the service principal ID, but just this:
authorization : #{parameters=; scheme=ServicePrincipal}
Does anyone have an idea on how to solve this?
UPDATE
I am calling the rest API like this:
$uriAccount = $UriOrg + "_apis/serviceendpoint/endpoints?endpointNames={name}&api-version=6.1-preview.4"
$result = Invoke-RestMethod -Uri $uriAccount -Method get -Headers $AzureDevOpsAuthenicationHeader
And $result.value gives me this:
authorization : #{parameters=; scheme=ServicePrincipal}
You can try the REST API Endpoints - Get Service Endpoints By Names.
GET https://dev.azure.com/{organization}/{project}/_apis/serviceendpoint/endpoints?endpointNames={endpointNames}&api-version=6.0-preview.4
In this REST API, you can find the id and details by the name of a service connection.
Here is an example to use the REST API in PowerShell:
$token = "{pat}"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$url="https://dev.azure.com/{organization}/{project}/_apis/serviceendpoint/endpoints?endpointNames={endpointNames}&api-version=6.0-preview.4"
$head = #{ Authorization =" Basic $token" }
Invoke-RestMethod -Uri $url -Method GET -Headers $head
Update:
The cause for this question is that you output result in the wrong way.
For JSON response bodies, there is no intuitive way to get results without specifying the final layer.
Here is my modified code, notice how I print result:
$token = "{pat}"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$url="https://dev.azure.com/{organization}/{project}/_apis/serviceendpoint/endpoints?endpointNames={endpointNames}&api-version=6.0-preview.4"
$head = #{ Authorization =" Basic $token" }
$reslut = Invoke-RestMethod -Uri $url -Method GET -Headers $head
echo $result.value.authorization.parameters

Is it possible to search a package from all Artifact Feeds in Azure DevOps?

I know it's possible to get all packages contained in a single Artifact Feed using the link below:
https://feeds.dev.azure.com/{organization}/{project}/_apis/packaging/Feeds/{feedId}/packages?api-version=6.0-preview.1
I noticed in Azure DevOps that the search bar has the ability to look into ALL feeds inside a project. Thus, my question is: Is it possible to achieve the same functionality through the API, and get all packages from all feeds instead of one.
Is it possible to achieve the same functionality through the API, and get all packages from all feeds instead of one.
As far as I know, this is achievable.
You could use the Rest API -Feed Management - Get Feeds to get All feeds in Project level.
Then you could use the Rest API to get the packages. You can execute these two apis simultaneously through powershell.
Here is the Powershell sample:
$token = "PAT"
$url="https://feeds.dev.azure.com/{Organization Name}/{Project Name}/_apis/packaging/feeds?api-version=6.0-preview.1"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$response = Invoke-RestMethod -Uri $url -Headers #{Authorization = "Basic $token"} -Method Get -ContentType application/json
ForEach( $feedid in $response.value.id )
{
echo $feedid
$url1="https://feeds.dev.azure.com/{Organization Name}/{Project Name}/_apis/packaging/Feeds/$($feedid)/packages?api-version=6.0-preview.1"
$response1 = Invoke-RestMethod -Uri $url1 -Headers #{Authorization = "Basic $token"} -Method Get -ContentType application/json
Write-Host "Package = $($response1 | ConvertTo-Json -Depth 100)"
}
In this case, you can get alls feeds of project scope in a project, and then get all the packages in it.
By the way, if you want to get all organization scope feeds, you only need to delete the project parameter in the URL.

Azure REST access token doesn't work - looks like one of commands is wrong?

So for last few days I've been working on getting REST working with our azure to perform a couple thousand of small changes. I think have the REST command to do it, but I am still struggling connecting with getting myself authenticated correctly. So yesterday I found this code on stackoverflow (in question 49211916)
##get token
$TENANTID=""
$APPID=""
$PASSWORD=""
$result=Invoke-RestMethod -Uri https://login.microsoftonline.com/$TENANTID/oauth2/token?api-version=1.0 -Method Post -Body #{"grant_type" = "client_credentials"; "resource" = "https://management.core.windows.net/"; "client_id" = "$APPID"; "client_secret" = "$PASSWORD" }
$token=$result.access_token
$Headers=#{
'authorization'="Bearer $token"
'host'="management.azure.com"
'contentype'='application/json'
}
Looks good for me. I already have created an app in AAD, so I copy and paste the 3 first values for it.
Then I get to my actual task - changing the device category in Intune. So I run the code I've found in question 957046 a few days ago:
#this is an example for 1 device:
$intuneDeviceId = 'deadbeef-aaaa-bbbb-cccc-0123456789ab' #update the IntuneDeviceID, you will need to implement a loop for mutiple devices
$deviceCategoryReqBody = '{"#odata.id":"https://graph.microsoft.com/beta/deviceManagement/deviceCategories/98765432-aaaa-bbbb-cccc-0123456789ab"}' #update the deviceCateg id
$patchDeviceReqBody = '{}'
#Running a GET method on the device ID to check its validity
Invoke-RestMethod -Uri "https://graph.microsoft.com/beta/deviceManagement/managedDevices/$intuneDeviceId/deviceCategory" -Headers $authToken -Method Get
#calling the PUT method to update device category for that specific device
Invoke-RestMethod -Uri "https://graph.microsoft.com/beta/deviceManagement/managedDevices/$intuneDeviceId/deviceCategory/`$ref" -Headers $authToken -Method Put -Body $deviceCategoryReqBody
#calling the PATCH method to update device details about device category
Invoke-RestMethod -Uri "https://graph.microsoft.com/beta/deviceManagement/managedDevices/$intuneDeviceId" -Headers $authToken -Method Patch -Body $patchDeviceReqBody
#Running a GET method on the device ID to check its validity
Invoke-RestMethod -Uri "https://graph.microsoft.com/beta/deviceManagement/deviceCategories/" -Headers $authToken -Method Get
#endregion
I substitute the variables and I get an error when running the first GET command already:
Invoke-RestMethod : The underlying connection was closed: Could not establish trust rel
ationship for the SSL/TLS secure channel.
What am I missing? As I said - I have registered an app in AAD (that's where I get tenant ID, app ID and password). I have added API permissions, but I am not a global admin for our tenant so they currently have "Not granted for Contoso" status. Does the global admin have to aprove it before I invoke the rest method, or is that in further steps, not my concern now?
Not sure about the TLS error, but this is definitely not right:
"resource" = "https://management.core.windows.net/"
Replace this with https://graph.microsoft.com to get a token for Microsoft Graph API.
Also, an application admin / cloud app admin / global admin needs to consent to your permissions in API permissions tab before it works. Application-level permissions always require admin consent.

Is there a way to make an API call from Azure runbook?

I have an automation account in Azure and I have a runbook in it. What I'm trying to do is to make an API call from this runbook. I'll need to login to some web service, get a session token and then use this session token to call some controller's methods.
So far I have only found some ways to call Azure runbooks through API (let's say from some backend c# code), but not vica versa. What I need to do is to call some c# methods FROM Azure runbook.
Is there a way to do it? If there is, how do I pass queries within my call?
What I'm expecting to see is something like:
$response = MakeApiCall -Url "www.someurl.com" -Body "some json for example"
Yes you can.
It's either
$Url = "https://my-url"
$Body = #{
field = "value"
}
Invoke-RestMethod -Method POST -Uri $url -Body $body -UseBasicParsing
or
Invoke-WebRequest
Invoke-RestMethod by default parses output, Invoke-WebRequest donesn't.

Resources