Skip to content

Instantly share code, notes, and snippets.

@crshnbrn66
Last active December 11, 2018 00:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crshnbrn66/73ea6364f6d4402d73a9 to your computer and use it in GitHub Desktop.
Save crshnbrn66/73ea6364f6d4402d73a9 to your computer and use it in GitHub Desktop.
#http://nakedalm.com/powershell-tfs-2013-api-1-get-tfscollection-and-tfs-services/
#http://blogs.msdn.com/b/alming/archive/2013/03/07/using-powershell-and-tfs-api-to-list-users-in-tfs-2010.aspx
#http://www.mikepoulson.com/2014/04/enumerating-tfs-permissions-for-items.html
function Connect-ToTfs
{
Param([string] $Collectionurl)
#the collection url will be cast as a uri to the getteamproject collection.
write-host $Collectionurl
if ($CollectionUrl -ne '')
{
#if collection is passed then use it and select all projects
$tfs = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection([uri]$CollectionUrl)
}
else
{
#if no collection specified, open project picker to select it via gui
$picker = New-Object Microsoft.TeamFoundation.Client.TeamProjectPicker([Microsoft.TeamFoundation.Client.TeamProjectPickerMode]::NoProject, $false)
$dialogResult = $picker.ShowDialog()
if ($dialogResult -ne 'OK')
{
#exit
}
$tfs = $picker.SelectedTeamProjectCollection
}
Return $tfs
}
<#
.SYNOPSIS
Invoke-VisualStudioDlls this checks to see what versions of Visual studio are installed and then will reflect in the dlls needed for use with this script.
.DESCRIPTION
If the script detects version 2015 then it will call another function for importing the dlls from 2015.
If the script detects version 2014 then it will call another function for importing the dlls from 2013.
.EXAMPLE
PS C:\> Invoke-VisualStudioDlls
.NOTES
Additional information about the function.
#>
function Invoke-VisualStudioDlls
{
if (Test-Path 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer')
{
Write-Output "importing Visual Studio 2015 Dll's"
Invoke-Visual15StudioDlls
}
elseif (Test-Path 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ReferenceAssemblies\v2.0')
{
Write-Output "importing Visual Studio 2013 Dll's"
Invoke-Visual13StudioDlls
}
}
<#
.SYNOPSIS
This function is meant to be called to import Visual Studio 2015 Dll's
.DESCRIPTION
This script adds the Types for visual studio 2013
.EXAMPLE
PS C:\> Invoke-Visual13StudioDlls
.NOTES
#>
function Invoke-Visual13StudioDlls
{
$visualStudiopath = 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ReferenceAssemblies\v2.0'
$visualStudiopath45 = 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\ide\ReferenceAssemblies\v4.5'
Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.VersionControl.Client.dll"
Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.Common.dll"
Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.WorkItemTracking.Client.dll"
Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.Client.dll"
Add-type -path "$visualStudiopath45\Microsoft.TeamFoundation.ProjectManagement.dll"
}
<#
.SYNOPSIS
This function is meant to be called to import Visual Studio 2015 Dll's
.DESCRIPTION
This script adds the Types for visual studio 2015
.EXAMPLE
PS C:\> Invoke-Visual15StudioDlls
.NOTES
#>
function Invoke-Visual15StudioDlls
{
$visualStudiopath = 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer'
#$visualStudiopath45 = 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer'
Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.VersionControl.Client.dll"
Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.Common.dll"
Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.WorkItemTracking.Client.dll"
Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.Client.dll"
Add-type -path "$visualStudiopath\Microsoft.TeamFoundation.ProjectManagement.dll"
Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.Build.Common.dll"
}
function Open-TFSProject
{
#this function assumes you've connected with the TEam foundation client at least once. Picker will not work if you havent
# this function returns two objects the first is the tfs connection object used by this function to get to TFS
# the second returns the list of selected projects.
$picker = New-Object Microsoft.TeamFoundation.Client.TeamProjectPicker([Microsoft.TeamFoundation.Client.TeamProjectPickerMode]::MultiProject, $false)
$dialogResult = $picker.ShowDialog()
if ($dialogResult -ne 'OK')
{
exit
}
$tfs = $picker.SelectedTeamProjectCollection
$projectList = $picker.SelectedProjects
return $tfs,$projectList
}
function Get-TFSWorkitemsWithAttachments
{
$TFSObject = Open-TFSProject
$tfs = $TFSObject | Select-Object -first 1
$projects = $tfsObject | Select-Object -Skip 1
$wis = $tfs.GetService([Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore])
$fullworkItemList = @()
foreach($project in $projects)
{
$p = $project.name
$workitemQuery = "Select * from issue where [System.Teamproject] = `"$p`"" # this gets the workitems for the project chosen from the picker.
$workitems = $wis.Query($workitemQuery)
if($workitems.attachedFilecount -gt 0)
{
foreach($attach in $workitems.attachments)
{
write-host $project.name ' ' $project.uri " has this " $attach.id $attach.Name $attach.uri.AbsoluteUri
}
}
$fullworkitemlist += $workitems
}
return $fullworkItemList
}
function Get-TFSWorkitems
{
$TFSObject = Open-TFSProject
$tfs = $TFSObject | Select-Object -first 1 #first item in TFS object is the connection to the TFS server
$projects = $tfsObject | Select-Object -Skip 1 #remaining items in the TFS Object contain the selected projects.
$fullworkItemList = @()
$wis = $tfs.GetService([Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore])
foreach($project in $projects)
{
$p = $project.name
$workitemQuery = "Select * from issue where [System.Teamproject] = `"$p`"" # this gets the workitems for the project chosen from the picker.
$workitems = $wis.Query($workitemQuery)
$fullworkitemlist += $workitems
}
return $tfs,$fullworkItemList
}
function New-TFSworkItem
{
#this function assumes you are sending the object to create the new TFSLinked items from.
#TFSDestination is the project to which you are going to create work items in
#tfsScource is where you are getting the work items from
#defectids this is an array of IDs that represent data you wish to input into the Destination project.
param([object]$tfsSourceObject, [string[]]$DefectIds)
$tfsDestinationObject =Get-TFSWorkitems
$tfsDest = $TFSDestinationObject | Select-Object -first 1 #first item in TFS object is the connection to the TFS server
$projectsDest = $tfsDestinationObject | Select-Object -Skip 1 #remaining items in the TFS Object contain the selected projects.
$DestProjname = ($projectsDest |Select-Object -first 1 ).IterationPath #this assumes the first bug /task in the collection has only the name of the project.
$tfsSource = $tfsSourceObject | Select-Object -first 1 #first item in TFS object is the connection to the TFS server
$workItemsSource = $tfsSourceObject | Select-Object -Skip 1 #remaining items in the TFS Object contain the selected projects.
$sourceProjname = ($workItemsSource |Select-Object -first 1 ).IterationPath
#create the work item Store object.
$wis = $tfsDest.GetService([Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore])
$workitemStore = $wis.projects[$DestProjname] #|Select-Object -first 10
if($defectids -eq $null)
{
foreach($wi in $workItemsSource)
{
$bug = $workitemStore.workitemtypes[($wi.type.Name)]
$workitem = $bug.NewWorkItem()
$validid = $wi.id
[Microsoft.TeamFoundation.WorkItemTracking.Client.RelatedLink]$newlink = New-Object -TypeName Microsoft.TeamFoundation.WorkItemTracking.Client.RelatedLink -ArgumentList ($validId)
$workitem.links.add($newlink)
$workitem.title = $wi.title + "-- $validid"
$workitem.description = $wi.Description
#$workitem.state = $wi.state
#$workitem.ChangedBy = $wi.ChangedBy #this is a readonly field
#$workitem.createdby = $wi.Createdby #this is a readonly field
$workitem.iterationpath = $wi.iterationpath.replace($sourceProjname,$DestProjname)
#NodeName is a readonly field
#if($wi.nodename -eq $sourceProjName)
#{
# $workitem.nodename = $wi.nodename.replace($sourceProjname,$DestProjname)
#}
#else
#{
# $workitem.nodename = $wi.nodename
#}
$workitem.areapath = $wi.AreaPath.replace($sourceProjname,$DestProjname)
write-host "$workitem is about to be entered"
#$savedworkItem = $workitem.save()
}
}
else
{
foreach($def in $defectids)
{
$wi = $workItemsSource | ?{$_.id -eq $def}
$bug = $workitemStore.workitemtypes[($wi.type.Name)]
$workitem = $bug.NewWorkItem()
$validid = $wi.id
[Microsoft.TeamFoundation.WorkItemTracking.Client.RelatedLink]$newlink = New-Object -TypeName Microsoft.TeamFoundation.WorkItemTracking.Client.RelatedLink -ArgumentList ($validId)
$workitem.links.add($newlink)
$workitem.title = $wi.title + "-- $validid"
$workitem.description = $wi.Description
$workitem.iterationpath = $wi.iterationpath.replace($sourceProjname,$DestProjname)
$workitem.areapath = $wi.AreaPath.replace($sourceProjname,$DestProjname)
write-host "$workitem is about to be entered " $wi.title
$savedworkItem = $workitem.save()
}
}
}
function update-TFSworkItems
{
#this function assumes you are sending the object to create the new TFSLinked items from.
#TFSDestination is the project to which you are going to create work items in
#tfsScource is where you are getting the work items from
#defectids this is an array of IDs that represent data you wish to input into the Destination project.
param([object]$tfsSourceObject, [object]$tfsDestinationObject)
$tfsDest = $TFSDestinationObject | Select-Object -first 1 #first item in TFS object is the connection to the TFS server
$projectsDest = $tfsDestinationObject | Select-Object -Skip 1 #remaining items in the TFS Object contain the selected projects.
$DestProjname = ($projectsDest |Select-Object -first 1 ).IterationPath #this assumes the first bug /task in the collection has only the name of the project.
$tfsSource = $tfsSourceObject | Select-Object -first 1 #first item in TFS object is the connection to the TFS server
$workItemsSource = $tfsSourceObject | Select-Object -Skip 1 #remaining items in the TFS Object contain the selected projects.
$sourceProjname = ($workItemsSource |Select-Object -first 1 ).IterationPath
#create the work item Store object.
$wis = $tfsDest.GetService([Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore])
$object = @()
$workitemStore = $wis.projects[$DestProjname] #|Select-Object -first 10
foreach($wi in $workItemsSource)
{
$obj = New-Object PSObject
$save = $false
$sourceId = $wi.id
$sourceTitle = $wi.Title
$workitem = $projectsdest | ?{$_.title -like "$sourceTitle*-- $sourceId"}
$obj | Add-Member -MemberType NoteProperty -Name "SourceTitle" -Value $SourceTitle
$obj | Add-Member -MemberType NoteProperty -Name "SourceId" -Value $sourceid
if($workitem -ne $null)
{
$destWorkTitle = $workitem.title
$destworkid = $workitem.id
write-host "Source workitem title: $sourceTitle"
Write-host "Source workitem Id: $sourceId"
write-host "Destination workitem title: $destWorkTitle"
Write-host "Destination workitem Id: $destWorkid"
$obj | Add-Member -MemberType NoteProperty -Name "Destinationtitle" -Value $destWorkTitle
$obj | Add-Member -MemberType NoteProperty -Name "DestinationID" -Value $destWorkid
$obj | Add-Member -MemberType NoteProperty -Name "SourceDescription" -Value ($wi.Description)
$obj | Add-Member -MemberType NoteProperty -Name "DestinationDescription" -Value ($workitem.description)
#if($workitem.description -ne $wi.Description)
#{
# $workitem.open()
# $workitem.description = $wi.Description
# Write-Output "updating Description" $workitem.description
# $save = $true
#}
$obj | Add-Member -MemberType NoteProperty -Name "SourceIterationPath" -Value ($wi.iterationpath)
$obj | Add-Member -MemberType NoteProperty -Name "DestinationIterationPath" -Value ($wi.iterationpath.replace($sourceProjname,$DestProjname))
if($workitem.iterationpath -ne ($wi.iterationpath.replace($sourceProjname,$DestProjname)))
{
if(($workitem.isopen) -eq $false)
{ $workitem.open()}
$workitem.iterationpath = $wi.iterationpath.replace($sourceProjname,$DestProjname)
Write-Output "updating iterationPath" $workitem.iterationpath
$save = $true
}
$obj | Add-Member -MemberType NoteProperty -Name "SourceAreaPath" -Value ($wi.areapath)
$obj | Add-Member -MemberType NoteProperty -Name "DestinationAreaPath" -Value ($wi.AreaPath.replace($sourceProjname,$DestProjname))
$obj | Add-Member -MemberType NoteProperty -Name "SourceState" -Value ($wi.state)
$obj | Add-Member -MemberType NoteProperty -Name "DestinationState" -Value ($workitem.state)
if ($workitem.areapath -ne ($wi.AreaPath.replace($sourceProjname,$DestProjname)))
{
if(($workitem.isopen) -eq $false)
{ $workitem.open()}
$workitem.areapath = $wi.AreaPath.replace($sourceProjname,$DestProjname)
Write-Output "updating AreaPath " $workitem.areapath
$save = $true
}
if($save)
{
write-host "$destWorkId is about to be updated"
$savedworkItem = $workitem.save()
if(($workitem.isopen) -eq $true)
{ $workitem.close()}
}
}
else
{
Write-Output "cannot find $sourceTitle"
Write-Output "cannot find $sourceId"
}
$object += $obj
}
return $object
}
Function update-TFSState
{
#this function assumes you are sending the object to create the new TFSLinked items from.
#TFSDestination is the project to which you are going to create work items in
#tfsScource is where you are getting the work items from
#defectids this is an array of IDs that represent data you wish to input into the Destination project.
param([object]$tfsSourceObject, [object]$tfsDestinationObject)
$tfsDest = $TFSDestinationObject | Select-Object -first 1 #first item in TFS object is the connection to the TFS server
$projectsDest = $tfsDestinationObject | Select-Object -Skip 1 #remaining items in the TFS Object contain the selected projects.
$DestProjname = ($projectsDest |Select-Object -first 1 ).IterationPath #this assumes the first bug /task in the collection has only the name of the project.
$object = @()
$tfsSource = $tfsSourceObject | Select-Object -first 1 #first item in TFS object is the connection to the TFS server
$workItemsSource = $tfsSourceObject | Select-Object -Skip 1 #remaining items in the TFS Object contain the selected projects.
$sourceProjname = ($workItemsSource |Select-Object -first 1 ).IterationPath
#create the work item Store object.
$wis = $tfsDest.GetService([Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore])
$workitemStore = $wis.projects[$DestProjname]
foreach($wi in $workItemsSource)
{
$obj = New-Object PSObject
$save = $false
$sourceId = $wi.id
$sourceTitle = $wi.Title
$workitem = $projectsdest | ?{$_.title -like "$sourceTitle*-- $sourceId"}
$obj | Add-Member -MemberType NoteProperty -Name "SourceTitle" -Value $SourceTitle
$obj | Add-Member -MemberType NoteProperty -Name "SourceId" -Value $sourceid
if($workitem -ne $null)
{
$destWorkTitle = $workitem.title
$destworkid = $workitem.id
write-host "Source workitem title: $sourceTitle"
Write-host "Source workitem Id: $sourceId"
write-host "Destination workitem title: $destWorkTitle"
Write-host "Destination workitem Id: $destWorkid"
$obj | Add-Member -MemberType NoteProperty -Name "Destinationtitle" -Value $destWorkTitle
$obj | Add-Member -MemberType NoteProperty -Name "DestinationID" -Value $destWorkid
if(($workitem.state) -ne ($wi.state))
{
if(($workitem.State) -eq 'New')
{
if(($workitem.isopen) -ne $true)
{
$workitem.open()
}
$workitem.state ='Active'
$workitem.validate()
$workitem.save()
$workitem.close()
}
if((($workitem.State) -eq 'Active') -and (($workitem.state) -ne ($wi.state)))
{
if(($workitem.isopen) -ne $true)
{
$workitem.open()
}
$workitem.state ='Resolved'
$workitem.validate()
$workitem.save()
$workitem.close()
}
If((($workitem.state) -eq 'Resolved') -and (($workitem.state) -ne ($wi.state)))
{
if(($workitem.isopen) -ne $true)
{
$workitem.open()
}
$workitem.state ='Closed'
$workitem.validate()
$workitem.save()
$workitem.close()
}
}
$obj | Add-Member -MemberType NoteProperty -Name "SourceState" -Value ($wi.state)
$obj | Add-Member -MemberType NoteProperty -Name "DestinationState" -Value ($workitem.state)
# if($save)
# {
# write-host "$destWorkId is about to be updated"
# $savedworkItem = $workitem.save()
# if(($workitem.isopen) -eq $true)
# { $workitem.close()}
# }
}
else
{
Write-Output "cannot find $sourceTitle"
Write-Output "cannot find $sourceId"
}
$object += $obj
}
return $object
}
#$projects = $null
Invoke-VisualStudioDlls
#$defects = get-content .\temp.txt
$sourceWorkItems = Get-TFSWorkitems
$DestinationWorkItems = Get-TFSWorkitems
#$updateditems = update-TFSworkItems $sourceWorkItems $DestinationWorkItems
#$updatedStates = update-TFSState $sourceWorkItems $DestinationWorkItems
#New-TFSworkItem $wi $defects #this function creates work items from the workitem object passed to it .. if defects are passed it will only enter new defects based on the array that is passed.
#$basepath2013 = 'http://your.tfs.server.com:8080/tfs/DefaultCollection'
#$basePath = 'http://your.tfs.server.com:8080/tfs/DefaultCollection'
#$basepath2013dev = 'http://your.tfs.server.com:8080/tfs/defaultcollection'
#$tfs = Connect-ToTfs -Collectionurl $basePath
#$tfs = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($serverName)
#$css = $tfs.GetService([Microsoft.TeamFoundation.Server.ICommonStructureService])
#$auth = $tfs.GetService([Microsoft.TeamFoundation.Server.IAuthorizationService])
#$gss = $tfs.GetService([Microsoft.TeamFoundation.Server.IGroupSecurityService])
#$ss = $tfs.GetService([Microsoft.TeamFoundation.Framework.Client.ISecurityService])
#$vcs = $tfs.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])
#$ims = $tfs.GetService([Microsoft.TeamFoundation.Framework.Client.IIdentityManagementService])
#$wis = $tfs.GetService([Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore])
#$cssNamespace = $ss.GetSecurityNamespace([Microsoft.TeamFoundation.Server.AuthorizationSecurityConstants]::CommonStructureNodeSecurityGuid)
#$workitemsWAttachments = Get-TFSWorkitemsWithAttachments
#$workitems = Get-TFSWorkitems
#$workitems2 = Get-TFSWorkitems
#$tfs.disconnect()
##remove variables used in this script ------
#remove-variable css,auth,gss,ss,vcs,ims,cssnamespace,workitems,tfs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment