如何使用PowerShell在TFS中检出文件?

问题描述

下面我编写了PowerShell脚本,以通过引用TFS Path.txt文件中提到的路径来签出TFS中的文件

  #$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 
    }

尝试在TFS中检出文件时出现错误? (下面的屏幕截图)

enter image description here

有人可以让我知道如何解决错误吗?我想念什么?

解决方法

为了签出文件,首先需要一个工作区映射。必须是服务器映射才能签出文件,本地工作区没有签出选项。

tf vc workspace new /noprompt YourUniqueNameHere /location:server

然后将您的本地文件夹映射到TFVC库中的服务器

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

然后调用getcheckout。我不是100%可以不先获取就可以检出文件。

您无需依赖tf.exe,就可以完成同样的事情。您可以在我的TFVC tasks for Azure Pipelines.中找到完整的示例,您仍然需要一个本地工作空间才能使其正常工作:

[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
}

从本地工作空间缓存中查找工作空间的代码可以为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"
    }

如果您愿意的话,此代码还应包含足够的信息来构建新的工作区。