I have a PS script that I wrote that downloads files from source control from a team project. The script is as follows:
function Clear-Folder
{
Param ([string]$directory)
try
{
if ((Test-Path -Path $directory -PathType Container))
{
Write-Output "INFO: Clearing directory: $directory"
Remove-Item -Path "$directory\*" -Recurse -Force -Verbose -ErrorVariable exClear -ErrorAction Stop
}
else
{
Write-Output "INFO: Directory: $directory does not exist, so it is being created."
New-Item -Path $directory -ItemType Directory -Force -Verbose -ErrorVariable exClear -ErrorAction Stop
Write-Output "INFO: Directory: $directory successfully created."
}
}
catch
{
Write-Output "ERROR: $exClear"
}
}
Add-PSSnapin Microsoft.TeamFoundation.PowerShell
function Get-TFS
{
Param ([string]$TeamProjectName)
$tfsCollectionPath = 'http://ourtfsserver/tfs/DefaultCollection'
[Microsoft.TeamFoundation.Client.TfsTeamProjectCollection] $tfs = Get-TfsServer $tfsCollectionPath
$tfsCollection = New-Object -TypeName Microsoft.TeamFoundation.Client.TfsTeamProjectCollection -ArgumentList $tfsCollectionPath
$tfsVersionControl = $tfsCollection.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])
$locationToSearch = "$/$TeamProjectName/"
$sourceFolder = "C:\$TeamProjectName"
Clear-Folder -directory $sourceFolder
$items = Get-TfsItemHistory -HistoryItem $locationToSearch -Recurse -Server $tfs -Version '1084892~1084927' -IncludeItems |
Select-Object -Expand "Changes" |
Where-Object { $_.ChangeType -notlike '*Delete*'} |
Where-Object { $_.ChangeType -notlike '*Rename*'} |
Select-Object -Expand "Item" |
Where-Object { $_.ContentLength -gt 0}
Write-Output $items.Count
foreach ($item in $items)
{
Write-Output $item
$itemFullName = $item.ServerItem
Write-Output $itemFullName
try
{
$path = $itemFullName.Replace($locationToSearch, "$sourceFolder\")
$path = $path.Replace('/', '\')
Write-Output $locationToSearch
Write-Output $sourceFolder
Write-Output $path
Write-Output "INFO: Downloading file: $itemFullName to: $path"
$tfsVersionControl.DownloadFile($itemFullName, $path)
}
catch [System.Exception]
{
$exceptionMessage = $_.Exception.message
Write-Output "ERROR: Downloading file: $itemFullName to: $path failed; exception message: $exceptionMessage"
}
}
}
Get-TFS -TeamProjectName 'A'
Get-TFS -TeamProjectName 'B'
Get-TFS -TeamProjectName 'C'
I'm running this script in three locations:
1. locally on my machine during initial development of the scripts and testing
2. A server with OS: Windows Server 2012 R2
3. In a TFS 2015 Build Definition on the server in location #2
The script works just fine and has had no issues in all 3 locations for one Team Project. Then I run the script again but with passing a different Team Project to the "Get-TFS" function. It works for locations #1 and #2, but not for location #3. I've tried it with multiple different Team Projects (we have dozens of them) and so far it has only worked in all 3 locations for the first one. All others only work for locations #1 and #2, but not for location #3. And by "not work" I specifically mean that it produces the following PS log in the build definition:
"No history entries were found for the item and version combination specified."
(Also a "0" character is output, without the double quotes characters, for the code in my script: "Write-Output $items.Count")
So it is unable to find any items in history when run in location #3, even though it did find items and downloaded them successfully when run in locations #1 and #2.
Any idea why this is happening? I'm suspecting it's some kind of permissions issue, but I've not been able to find anything and I find it strange that it would work just fine in 2/3 of the locations instead of failing in all 3 if this was the case.
Other important details to note:
1. I've meticulously checked several times now that the Team Project names are all spelled correctly.
2. The Changeset Range is correct and does contain files to be downloaded within that range in all of the Team Projects I'm trying this for. Remember that I'm getting results and downloading the files successfully when I run the script in locations #1 and #2.
3. I realize that this might be some weird bug with TFS 2015 and that upgrading to Azure might very well solve the problem. I would love to upgrade, but unfortunately I'm in a workplace and there people that sit behind bigger desks than me that decide when the upgrade happens. They are planning on doing so, but in the meantime I still need to do this or find some workaround to get me by.
UPDATE:
I ran the following line:
$PSVersionTable.PSVersion
in all three locations with the following results:
Location 1:
Major : 5
Minor : 1
Build : 14409
Revision : 1018
MajorRevision : 0
MinorRevision : 1018
Location 2:
Major : 5
Minor : 1
Build : 14409
Revision : 1018
Location 3:
Major : 5
Minor : 1
Build : 14409
Revision : 1018
MajorRevision : 0
MinorRevision : 1018
So the powershell versions are all the same. That's not the issue.