title: Microsoft.TeamFoundation.VersionControl.Client.ItemNotMappedException even when the workspace exists and has a mapping
body: <p>Using TFS 2015 update 2, an agent was installed in a machine, the agent creates its workspace:</p>
  
  <p><a href="https://i.stack.imgur.com/w1xVE.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/w1xVE.jpg" alt="enter image description here"></a></p>
  
  <p>Some custom MSBuild tasks developed InHouse were implemented in the build definition that will run on the agent. Those tasks perform some operations against the TFS server.</p>
  
  <p>When the build definition is queued for a new build here is what I got:</p>
  
  <p><a href="https://i.stack.imgur.com/X0sPB.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/X0sPB.jpg" alt="enter image description here"></a></p>
  
  <p>In the build machine I proceed to run the following script, in order to verify the existence of the workspace :</p>
  
  <pre class="lang-bsh prettyprint-override"><code># Script to find a Team Foundation workspace
  param(
      [string] $workspaceHint = $(get-location).Path
  )
  
  begin
  {
      # load the needed client dll's
      [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")
      [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.VersionControl.Client")
  
      # fetches a Workspace instance matching the WorkspaceInfo found in the cache file
      function getWorkspaceFromWorkspaceInfo($wsInfo)
      {
          $tfs = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($wsInfo.ServerUri.AbsoluteUri)
          $vcs = $tfs.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])
          $vcs.GetWorkspace($wsInfo)
          # TODO: likely add some convenience properties/methods for easier scripting support
      }
  }
  
  process
  {
      # is there only 1 workspace in our cache file?  If so, use that one regardless of the hint
      $workspaceInfos = [Microsoft.TeamFoundation.VersionControl.Client.Workstation]::Current.GetAllLocalWorkspaceInfo()
      if ($workspaceInfos.Length -eq 1)
      {
          return getWorkspaceFromWorkspaceInfo($workspaceInfos[0])
      }
  
      if (test-path $workspaceHint)
      {
          # workspace hint is a local path, get potential matches based on path
          $workspaceInfos = [Microsoft.TeamFoundation.VersionControl.Client.Workstation]::Current.GetLocalWorkspaceInfoRecursively($workspaceHint)
      }
      else
      {
          # workspace hint is NOT a local path, get potential matches based on name
          $workspaceInfos = @($workspaceInfos | ?{ $_.name -match $workspaceHint })
      }
  
      if ($workspaceInfos.Length -gt 1)
      {
          throw 'More than one workspace matches the workspace hint "{0}": {1}' -f
                  $workspaceHint, [string]::join(', ', @($workspaceInfos | %{ $_.Name}))
      }
      elseif ($workspaceInfos.Length -eq 1)
      {
          return getWorkspaceFromWorkspaceInfo($workspaceInfos[0])
      }
      else
      {
          throw "Could not figure out a workspace based on hint $workspaceHint"
      }
  }
  </code></pre>
  
  <p>The script is not able to find any workspace.</p>
  
  <p>Then, the TFS 2015 Power tools were installed with its powershell cmdlets in the machine and run the following script:</p>
  
  <pre class="lang-bsh prettyprint-override"><code>if ( (Get-PSSnapin -Name Microsoft.TeamFoundation.PowerShell -ErrorAction SilentlyContinue) -eq $null )
          {
              Add-PSSnapin -Name Microsoft.TeamFoundation.PowerShell
          }
  $ws = Get-TfsWorkspace -Path C:\t\1\s 
  
  $ws.Folders
  </code></pre>
  
  <p>Showing the workspace and the items mapped.</p>
  
  <p>Queuing new builds, keep showing the same error.
  The workspace is a public server one, and following some ancient post in msdn forums, I clean the TFS cache in the machine.</p>
  
  <p>Any clue how to make the Microsoft.TeamFoundation.VersionControl.Client be able to recognize the workspace?</p>