简体   繁体   中英

How to checkout a file in TFS using PowerShell?

I have below PowerShell Script written to check-out the files in TFS by referring the path mentioned in the TFS Path.txt file.

  #$TFSCheckoutExe="C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\TF.exe"
    $TFSFilePaths=Get-Content "$PSScriptRoot\TFS Path.txt" 
    $TFSCheckoutExe="C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\TF.exe"
    
    
    $visualStudiopath = 'C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\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"
    
#TFS Collection Path
$sourceLocation = "http://vwmaztfsapp:8080/tfs/MatchCollection"

#Creating TFS Object 
$tfsCollectionUrl = New-Object System.URI($sourceLocation);
    
    $tfsCollection = New-Object -TypeName Microsoft.TeamFoundation.Client.TfsTeamProjectCollection -ArgumentList $tfsCollectionUrl  
    
    $VersionControl = $tfsCollection.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])  
    $latest = [Microsoft.TeamFoundation.VersionControl.Client.VersionSpec]::Latest  
    $recursionType = [Microsoft.TeamFoundation.VersionControl.Client.RecursionType]::Full 
    
    Foreach($TFSFilePath in $TFSFilePaths)
    {
          &$TFSCheckoutExe checkout $TFSFilePath | Out-Null 
    }

I am getting an error while trying to check-out the file in TFS? (screenshot below)

在此处输入图片说明

Can anyone please let me know how can I resolve this error, what am I missing?

In order to checkout a file, you first need a workspace mapping. It must be a server mapping to check out a file, local workspaces don't have a checkout option.

tf vc workspace new /noprompt YourUniqueNameHere /location:server

Then map your local folder to a server in the TFVC reporitory

tf vc workfold /map $/Project/Path c:\A\Local\Directory /workspace YourUniqueNameHere 

Then call get or checkout . I'm not 100% you can checkout a file without getting it first.

You can accomplish the same thing without depending on tf.exe in pure powershell. You can find a complete example in my TFVC tasks for Azure Pipelines. You still need a local workspace for this to work:

[string[]] $FilesToCheckout = $ItemSpec -split ';|\r?\n'
$RecursionType = [Microsoft.TeamFoundation.VersionControl.Client.RecursionType]$Recursion

Write-Message "Checking out ItemSpec: $ItemSpec, Recursive: $RecursionType"

Try
{
    $provider = Get-SourceProvider
    if (-not $provider)
    {
        return;
    }

    Foreach ($change in $FilesToCheckout)
    {
        Write-Message "Checking out: $change"
        
        $provider.Workspace.PendEdit(
            @($change),
            $RecursionType,
            $null,
            [Microsoft.TeamFoundation.VersionControl.Client.LockLevel]"Unchanged"
        )  | Out-Null
    }
}
Finally
{
    Invoke-DisposeSourceProvider -Provider $provider
}

The code to find a workspace from the local workspace cache can be found in the shared module :

function Get-TfsTeamProjectCollection()
{
    $ProjectCollectionUri = Get-VstsTaskVariable -Name "System.TeamFoundationCollectionUri" -Require
    $tfsClientCredentials = Get-VstsTfsClientCredentials -OMDirectory $(Find-VisualStudio)
        
    $collection = New-Object Microsoft.TeamFoundation.Client.TfsTeamProjectCollection(
        $ProjectCollectionUri,
        $tfsClientCredentials)
    $collection.EnsureAuthenticated()

    return $collection
}

function Get-SourceProvider {
    [cmdletbinding()]
    param()

    Write-Debug "Entering Get-SourceProvider"
    $provider = @{
        Name = $env:BUILD_REPOSITORY_PROVIDER
        SourcesRootPath = $env:BUILD_SOURCESDIRECTORY
        TeamProjectId = $env:SYSTEM_TEAMPROJECTID
    }
    $success = $false
    try {
        if ($provider.Name -eq 'TfsVersionControl') {
            $provider.TfsTeamProjectCollection = Get-TfsTeamProjectCollection

            $versionControlServer = $provider.TfsTeamProjectCollection.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])
            $versionControlServer.add_NonFatalError($OnNonFatalError)
            
            $workstation = [Microsoft.TeamFoundation.VersionControl.Client.Workstation]::Current
            $workstation.EnsureUpdateWorkspaceInfoCache($versionControlServer, $versionControlServer.AuthorizedUser)

            $provider.VersionControlServer = $versionControlServer;
            $provider.Workspace = $versionControlServer.TryGetWorkspace($provider.SourcesRootPath)

            if (!$provider.Workspace) {
                Write-Message -Type Debug "Unable to determine workspace from source folder: $($provider.SourcesRootPath)"
                Write-Message -Type Debug "Attempting to resolve workspace recursively from locally cached info."
                
                $workspaceInfos = $workstation.GetLocalWorkspaceInfoRecursively($provider.SourcesRootPath);
                if ($workspaceInfos) {
                    foreach ($workspaceInfo in $workspaceInfos) {
                        Write-Message -Type Debug "Cached workspace info discovered. Server URI: $($workspaceInfo.ServerUri) ; Name: $($workspaceInfo.Name) ; Owner Name: $($workspaceInfo.OwnerName)"
                        try {
                            $provider.Workspace = $versionControlServer.GetWorkspace($workspaceInfo)
                            break
                        } catch {
                            Write-Message -Type Debug "Determination failed. Exception: $_"
                        }
                    }
                }
            }

            if ((!$provider.Workspace) -and $env:BUILD_REPOSITORY_TFVC_WORKSPACE) {
                Write-Message -Type Debug "Attempting to resolve workspace by name: $env:BUILD_REPOSITORY_TFVC_WORKSPACE"
                try {
                    $provider.Workspace = $versionControlServer.GetWorkspace($env:BUILD_REPOSITORY_TFVC_WORKSPACE, '.')
                } catch [Microsoft.TeamFoundation.VersionControl.Client.WorkspaceNotFoundException] {
                    Write-Message -Type Debug "Workspace not found."
                } catch {
                    Write-Message -Type Debug "Determination failed. Exception: $_"
                }
            }

            if (!$provider.Workspace) {
                Write-Message -Type Warning (Get-LocalizedString -Key 'Unable to determine workspace from source folder ''{0}''.' -ArgumentList $provider.SourcesRootPath)
                return
            }

            if ($provider.Workspace.Location -eq "Server")
            {
                Write-Warning "Server workspace support is experimental."
            }

            $provider.Workspace.Refresh()

            $success = $true
            return New-Object psobject -Property $provider
        }

        Write-Warning ("Only TfsVersionControl source providers are supported for TFVC tasks. Repository type: $provider")
        return
    } finally {
        if (!$success) {
            Invoke-DisposeSourceProvider -Provider $provider
        }
        Write-Message -Type Debug "Leaving Get-SourceProvider"
    }

This code should contain enough information to build a new workspace too if you'd want that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM