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

Posted

技术标签:

【中文标题】如何使用 PowerShell 在 TFS 中签出文件?【英文标题】:How to checkout a file in TFS using PowerShell? 【发布时间】:2021-01-10 22:15:14 【问题描述】:

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

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

【问题讨论】:

【参考方案1】:

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

tf vc workspace new /noprompt YourUniqueNameHere /location:server

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

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

然后致电getcheckout。我不是 100% 你可以在不先获取文件的情况下签出文件。

您可以在纯 powershell 中不依赖 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"
    

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

【讨论】:

以上是关于如何使用 PowerShell 在 TFS 中签出文件?的主要内容,如果未能解决你的问题,请参考以下文章

Visual Studio 不跟踪更改,或者在我编辑文件时从源代码管理中签出文件

项目未显示为在Visual Studio 2013中签入TFS

如何使用 REST API 在 PowerShell 中获取 TFS 构建报告 [重复]

在服务器或集合级别禁用 TFS 签入权限

使用 PowerShell 设置 TFS 批准者

如何找出我在 git 中签出的版本? [复制]