I have a project where it is needed to change some files during build process.
I have to use Powershell to do this. I have already configured all required steps to do this. All steps work on my client pc. The Build server has the same configuration. Vs 2015 (for TF Powertools 2015) and VS 2017 installed. When I queued a Build, the build fails at the point where he tries to get the Workspace. Maybe this is because, the build agent creates only local workspaces? At this point the required changes are already checked out. I Cannot use TF.exe checkin, because there are checkin policies which prevents the checkin without associated workitem. This is what I try to do at this step:

* Get Workspace via path of the sources ($Env:BUILD\_SOURCESDIRECTORY)
* Get the pending changes of this workspace `$pendingChanges = $tfsws.GetPendingChanges()`
* Create workitemcheckininfo with an associated workitem
* Checkin with created workitemcheckininfo `$changesetNumber = $tfsws.CheckIn($pendingChanges,"$CommentString checked in by BuildServer",$null, $workItemChanges,$null)`

As I dont get the Workspace (step 1), the following steps will not start.

This is what I tried so far:

1
=

```
$binpath   = "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer"
#$binpath   = "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ReferenceAssemblies\v2.0"
Add-Type -path "$binpath\Microsoft.TeamFoundation.Client.dll"
Add-Type -Path "$binpath\Microsoft.TeamFoundation.WorkItemTracking.Client.dll"
Add-Type -Path "$binpath\Microsoft.TeamFoundation.VersionControl.Client.dll"
Add-Type -Path "$binpath\Microsoft.TeamFoundation.Common.dll"

$teamProjectCollection = "http://mytfs/tfs/defaultcollection"
$tfs = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($teamProjectCollection)

#The next line fails, as the commandlet is from TF Powertools 2015 and the TFS server is 2017.
#I get the error message "Microsoft.TeamFoundation.Client.TfsTeamProjectCollection cannot be converted to Microsoft.TeamFoundation.Client.TfsTeamProjectCollection"
$tfsws = Get-TfsWorkspace -Server $tfs -Computer $hostname -Owner $Username

```

2
=

```
$binpath   = "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer"
#$binpath   = "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ReferenceAssemblies\v2.0"
Add-Type -path "$binpath\Microsoft.TeamFoundation.Client.dll"
Add-Type -Path "$binpath\Microsoft.TeamFoundation.WorkItemTracking.Client.dll"
Add-Type -Path "$binpath\Microsoft.TeamFoundation.VersionControl.Client.dll"
Add-Type -Path "$binpath\Microsoft.TeamFoundation.Common.dll"

$localReference = join-path $Env:BUILD_SOURCESDIRECTORY $TargetBranch
$teamProjectCollection = "http://mytfs/tfs/defaultcollection"
$tfsTeamProjectCollection = New-Object Microsoft.TeamFoundation.Client.TfsTeamProjectCollection($teamProjectCollection)
$versioncontrolServer = $tfsTeamProjectCollection.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])

#The next lines fail, as $versioncontrolServer is nothing
[Microsoft.TeamFoundation.VersionControl.Client.Workstation]::Current.EnsureUpdateWorkspaceInfoCache($versionControlServer, $username);
$tfsws = $versioncontrolServer.GetWorkspace($localReference)

```

Two things, that maybe causing the problems: 1st => build agent only uses local workspaces? 2nd => TFS 2017 and VS 2015 are not compatible enough?

Has someone a good working example or solution?

I thought about other options. Maybe I could program an executable, which does my stuff.

Can I checkin without workspace and later associate the workitem? How to programmatically associate a workitem with an existing Changeset?