2

我在下面编写了 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 中签出文件时出现错误?(下面的截图)

在此处输入图像描述

谁能告诉我如何解决这个错误,我错过了什么?

4

1 回答 1

2

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

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纯 powershell 的情况下完成同样的事情。您可以在我的 Azure Pipelines TFVC 任务中找到完整示例。您仍然需要一个本地工作区才能正常工作:

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

从本地工作区缓存中查找工作区的代码可以在共享模块中找到:

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

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

于 2020-09-24T15:32:27.687 回答