Search code examples
powershelltfs-2015vnext

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


Setting up a vNext Release in TFS 2015 and we are using powershell to get external web.config files. I've adapted this script from our old XAML builds:

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

It fails on: $tfsItems = $tfs.vcs.GetItems($TfsConfigurationPath).Items with the error:

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

For Parameters I'm handing it the URL of our TFS server as:

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

I then give it the full path to the configs folder inside the branch I'm targeting and for now I'm running it locally to put the file onto my desktop. The path to the files isn't null so I'm not at all sure why it's saying it's null. Where am i going wrong here?


Solution

  • Debugged locally based on your script, please try below script:

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