使用 Powershell 从 TFS2015 版本控制中获取单个文件作为空值表达式失败?

Get single file out of TFS2015 version control using Powershell fails as null valued expression?

在 TFS 2015 中设置 vNext 版本,我们正在使用 powershell 获取外部 web.config 文件。我从我们的旧 XAML 构建中改编了这个脚本:

[CmdletBinding()]
param([string]$TfsUrl, [string]$TfsConfigurationPath, [string]$ConfigurationDestination)
$tfs = $TfsUrl
if($tfs -ne $null)
{
    $tfsItems = $tfs.vcs.GetItems($TfsConfigurationPath).Items
}

if ($tfsItems.Count -eq 0)
{
    Write-Host "[DeployResources] WARNING: No configurations were found in TFS."
}
else
{
    foreach ($tfsItem in $tfsItems)
    {
        $fileFound = $tfsItem.ServerItem -match '([^/]+)$'

        if ($fileFound -ne $null)
        {
            $fileName = $matches[1]
            $tfsItem.DownloadFile((Join-Path $ConfigurationDestination $fileName))    
        }
    }
}

它失败了: $tfsItems = $tfs.vcs.GetItems($TfsConfigurationPath).Items 错误:

You cannot call a method on a null-valued expression.
At [Where I'm running the file from]\ExternalConfigs.ps1:54 char:9
+         $tfsItems = $tfs.vcs.GetItems($configurationSource).Items
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

对于参数,我将 TFS 服务器的 URL 作为:

http://(server name):8080/tfs/(Collection name)

然后我给它我目标分支内的 configs 文件夹的完整路径,现在我 运行 它在本地将文件放到我的桌面上。文件的路径不是空的,所以我完全不确定为什么它说它是空的。我哪里错了?

已根据您的脚本在本地调试,请尝试以下脚本:

[CmdletBinding()]
param([string]$TfsUrl, [string]$TfsConfigurationPath, [string]$ConfigurationDestination)


add-type -Path 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\Microsoft.TeamFoundation.VersionControl.Client.dll'
add-type -Path 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\Microsoft.TeamFoundation.Client.dll'

$tfs = new-object Microsoft.TeamFoundation.Client.TfsTeamProjectCollection(New-Object Uri($TfsUrl))

$tfsVersionControl = $tfs.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])

if($tfs -ne $null)
{
    $tfsItems = $tfsVersionControl.GetItems($TfsConfigurationPath).Items

}

if ($tfsItems.Count -eq 0)
{
    Write-Host "[DeployResources] WARNING: No configurations were found in TFS."
}
else
{
    foreach ($tfsItem in $tfsItems)
    {
        $fileFound = $tfsItem.ServerItem -match '([^/]+)$'

        if ($fileFound -ne $null)
        {
            $fileName = $matches[1]
            $tfsItem.DownloadFile((Join-Path $ConfigurationDestination $fileName))    
        }

    }
}