DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Building REST API Backend Easily With Ballerina Language
  • How to Automate Restful APIs Using Jayway Library
  • RESTful API Example With Spring Data REST and JPA Hibernate Many To Many Extra Columns
  • REST-API Versioning Strategies

Trending

  • The Convergence of Testing and Observability
  • Spring WebFlux Retries
  • Parallelism in ConcurrentHashMap
  • Analyzing Stock Tick Data in SingleStoreDB Using LangChain and OpenAI's Whisper
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Getting Work Item Data in Powershell Through REST API

Getting Work Item Data in Powershell Through REST API

Here's a comprehensive look at extracting​ work item data with a REST API through Powershell.

Ricci Gian Maria user avatar by
Ricci Gian Maria
·
Jan. 19, 16 · Tutorial
Like (2)
Save
Tweet
Share
6.70K Views

Join the DZone community and get the full member experience.

Join For Free

vsts and the latest versions of on-premise tfs have the ability to access data through rest api . this way to access tfs data is really convenient especially if used from powershell scripts, because you do not need any external dependency, except being able to issue rest requests with the invoke-restrequest cmdlet.

to simplify accessing your vsts account, you can enable alternate credentials, needed to issue a request with simple basic authentication . here is a powershell snippet that retrieve a work item with a given id.


$username = "alternateusername"
$password = "alternatepassword"

$basicauth = ("{0}:{1}" -f $username,$password)
$basicauth = [system.text.encoding]::utf8.getbytes($basicauth)
$basicauth = [system.convert]::tobase64string($basicauth)
$headers = @{authorization=("basic {0}" -f $basicauth)}

$result = invoke-restmethod -uri https://gianmariaricci.visualstudio.com/defaultcollection/_apis/wit/workitems/100?api-version=1.0  -headers $headers -method get

this is a simple code you can find everywhere on the internet, it is just a matter of converting username and password in base64 string and pass the result to invoke-restmethod with the –headers argument. the result is an object parsed from the json returned by the call . the nice aspect of powershell is that you can simply use the get-member cmdlet to list properties of returned object. here is the result of instruction


$result | get-member

image








figure 1 : result of the get-member shows all properties of returned object

in this situation, we can see the id of the work item and the rev property, but probably all the data we need is inside the fields property. if we simply write-output the $result.fields we got the full list of all the fields of the returned work item

image

figure 2: content of fields property of returned work item

any property can be accessed with standard dot notation (but pay attention to the dot in the name of the property, fields property is actually an hashset):


$closedby = $result.fields.'microsoft.vsts.common.closedby'
write-output "work item was closed by: $closedby"

thanks to powershell ise you can also use intellisense to have all the properties conveniently listed.

image








figure 3: intellisense on returned object

REST API Web Protocols PowerShell Data (computing) Property (programming)

Published at DZone with permission of Ricci Gian Maria, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Building REST API Backend Easily With Ballerina Language
  • How to Automate Restful APIs Using Jayway Library
  • RESTful API Example With Spring Data REST and JPA Hibernate Many To Many Extra Columns
  • REST-API Versioning Strategies

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: