It's easy to utilize Excel add-ins or extensions from marketplace to export the work items, right? Then why build a script in PowerShell?
To answer this in curt, I manage multiple instances for multiple projects so I use PowerShell, which is a quick way for me to explore work items in i go!
Comments
- <#
- .SYNOPSIS
- A PowerShell function to export Visual Studio Team Servies work items.
- .Description
- A PowerShell role to consign Visual Studio Team Servies work items.
- .EXAMPLE
- PS C:\> Export-VSTSWorkItem -Instance "Fabrikam" -Token "PAT -WorkItemType Bug
- Yields all the bugsin from the work items tabular array.
- .EXAMPLE
- PS C:\> Consign-VSTSWorkItem -Example "Fabrikam" -Token "PAT"-WorkItemType Task
- Yields all the chorein from the work items table.
- .EXAMPLE
- PS C:\> Export-VSTSWorkItem -Example "Fabrikam" -Token "PAT"-WorkItemType Epic
- Yields all the epicin from the work items table.
- .EXAMPLE
- PS C:\> Consign-VSTSWorkItem -Instance "Fabrikam" -Token "PAT"-WorkItemType Ballsy -ExportAs Csv
- Exports informationevery bit csv
- .EXAMPLE
- PS C:\> Consign-VSTSWorkItem -Instance "Fabrikam" -Token "PAT"-WorkItemType Epic -ExportAs FancyHtml
- Exports dataas a fancy html
- .NOTES
- @ChendrayanV
- #>
Parameters are my best friend as always,
- param (
- # Visual Studio Team Services Account Name
- [Parameter(Mandatory)]
- $Instance,
-
- # Create a Personal Admission Token
- [Parameter(Mandatory)]
- $Token,
-
- # Opt the Work Items Type. (Change as required)
- [Parameter(Mandatory)]
- [ValidateSet('Bug' , 'Task' , 'Ballsy' , 'Characteristic' )]
- $WorkItemType,
-
- # Consign in your favorite format.
- [Parameter()]
- [ValidateSet('Csv' , 'HTML' , 'FancyHTML' )]
- $ExportAs
- )
Nosotros utilise a personal access token so it's a must to convert the token to base 64 cord. Equally a organisation admin, it took some fourth dimension for me to understand but the beneath snippet makes it happen.
- $Authentication = ( ":$Token" )
- $Hallmark = [System.Text.Encoding]::ASCII.GetBytes($Authentication)
- $Authentication = [System.Catechumen]::ToBase64String($Authentication)
Using switch argument, modify the WIQL every bit illustrated below.
- switch ($WorkItemType)
- {
- "Issues"
- {
- $Body = @{
- Query ="Select * from WorkItems WHERE [System.WorkItemType] = '$WorkItemType'"
- } | ConvertTo-Json
- }
- "Task"
- {
- $Body = @{
- Query ="Select * from WorkItems WHERE [System.WorkItemType] = '$WorkItemType'"
- } | ConvertTo-Json
- }
- "Ballsy"
- {
- $Body = @{
- Query ="Select * from WorkItems WHERE [Organisation.WorkItemType] = '$WorkItemType'"
- } | ConvertTo-Json
- }
- "Feature"
- {
- $Trunk = @{
- Query ="Select * from WorkItems WHERE [System.WorkItemType] = '$WorkItemType'"
- } | ConvertTo-Json
- }
- }
Only, we are converting the hash table to JSON using ConvertTo-Json cmdlet and with no more than theory, below is the full script!
- part Export-VSTSWorkItem
- {
- <#
- .SYNOPSIS
- A PowerShell function to consign Visual Studio Team Servies piece of work items.
- .DESCRIPTION
- A PowerShell function to consign Visual Studio Team Servies piece of work items.
- .Case
- PS C:\> Export-VSTSWorkItem -Case'Fabrikam' -Token "PAT" -WorkItemType Bug
- Yields all the bugsin from the work items tabular array.
- .EXAMPLE
- PS C:\> Export-VSTSWorkItem -Instance'Fabrikam' -Token "PAT" -WorkItemType Task
- Yields all the taskin from the work items tabular array.
- .Example
- PS C:\> Export-VSTSWorkItem -Case'Fabrikam' -Token "PAT" -WorkItemType Epic
- Yields all the epicin from the work items tabular array.
- .Instance
- PS C:\> Export-VSTSWorkItem -Instance'Fabrikam' -Token "PAT" -WorkItemType Ballsy -ExportAs Csv
- Exports informationas csv
- .EXAMPLE
- PS C:\> Consign-VSTSWorkItem -Case'Fabrikam' -Token "PAT" -WorkItemType Epic -ExportAs FancyHtml
- Exports dataequally a fancy html
- .NOTES
- @ChendrayanV
- #>
- [CmdletBinding()]
- param (
- # Visual Studio Team Services Account Name
- [Parameter(Mandatory)]
- $Instance,
-
- # Create a Personal Access Token
- [Parameter(Mandatory)]
- $Token,
-
- # Opt the Work Items Type. (Change as required)
- [Parameter(Mandatory)]
- [ValidateSet('Bug' , 'Task' , 'Ballsy' , 'Feature' )]
- $WorkItemType,
-
- # Export in your favorite format.
- [Parameter()]
- [ValidateSet('Csv' , 'HTML' , 'FancyHTML' )]
- $ExportAs
- )
-
- brainstorm
- {
- }
-
- process
- {
- $Hallmark = (":$Token" )
- $Authentication = [Arrangement.Text.Encoding]::ASCII.GetBytes($Authentication)
- $Authentication = [System.Catechumen]::ToBase64String($Authentication)
- switch ($WorkItemType)
- {
- "Bug"
- {
- $Body = @{
- Query ="Select * from WorkItems WHERE [Organisation.WorkItemType] = '$WorkItemType'"
- } | ConvertTo-Json
- }
- "Task"
- {
- $Body = @{
- Query ="Select * from WorkItems WHERE [System.WorkItemType] = '$WorkItemType'"
- } | ConvertTo-Json
- }
- "Ballsy"
- {
- $Body = @{
- Query ="Select * from WorkItems WHERE [Arrangement.WorkItemType] = '$WorkItemType'"
- } | ConvertTo-Json
- }
- "Feature"
- {
- $Body = @{
- Query ="Select * from WorkItems WHERE [System.WorkItemType] = '$WorkItemType'"
- } | ConvertTo-Json
- }
- }
- $RestParams = @{
- Uri ="https://$Instance.visualstudio.com/DefaultCollection/_apis/wit/wiql?api-version=one.0"
- Method ="Mail"
- ContentType ="application/json"
- Headers = @{
- Authorization = ("Basic {0}" -f $Authentication)
- }
- Torso = $Trunk
- }
- effort
- {
- $Id = (Invoke-RestMethod @RestParams).workitems.id -join","
- if ($Id -ne $ cipher )
- {
- $Fields = @('System.Id' , 'System.Title' , 'System.AssignedTo' ,
- 'Organization.Country' , 'System.CreatedBy' , 'System.WorkItemType' ) -join ","
- $RestParams["Uri" ] = "https://$Instance.visualstudio.com/DefaultCollection/_apis/wit/WorkItems?ids=$Id&fields=$Fields&api-version=1"
- $RestParams["Method" ] = "Become"
- $RestParams.Remove("Trunk" )
- $Issue = Invoke-RestMethod @RestParams
- if (! $PSBoundParameters[ 'ExportAs' ])
- {
- ($Result.value.fields)
- }
- }
- else
- {
- Write-Alert"No Items are available in $WorkItemType"
- }
-
- switch ($ExportAs)
- {
- 'csv'
- {
- $Outcome.value.fields | Consign-Csv .\WITReport.csv -NoTypeInformation
- }
- 'HTML'
- {
- $Result.value.fields | Consign-Csv .\WITReport.html -NoTypeInformation
- }
- 'FancyHTML'
- {
- Add-Content".\style.CSS" -Value " body {
- font-family:Calibri;
- font-size:10pt;
- }
- th {
- background-color:black;
- color:white;
- }
- td {
- background-color:#19fff0;
- color:black;}"
- $Result.value.fields | ConvertTo-Html -CssUri .\Manner.css | Out-File .\Written report.html
- }
- }
- }
- take hold of
- {
- $_.Exception.Message
- }
- }
-
- terminate
- {
- }
- }
Enjoy PowerShell!
Before long I volition connect with you lot all with a series of detailed blog posts under the umbrella of "Work with Visual Studio Squad Services using REST API in PowerShell".
0 Response to "Tfs Work Item Attach Uploaded Attachments Api C#"
Post a Comment