使用Powershell将文件检入TFS [英] Check a file into TFS using Powershell

查看:84
本文介绍了使用Powershell将文件检入TFS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为持续集成构建的一部分,我正在创建一个SQL脚本.生成此SQL脚本后,必须将其重新签入TFS.我正在Powershell中使用TFS Powertools.

As part of my continuous integration build I am creating an SQL script. This SQL script has to be checked back in to TFS after it is generated. I'm using the TFS Powertools in Powershell.

我在计算机上使用的代码是:

The code I used on my machine was:

Add-TfsPendingChange -Add -Item $filename | New-TfsChangeSet

这在我的开发箱上很好用,因为我所在的文件夹已映射到TFS工作区.当我将其移至构建服务器时,它不再起作用,因为TeamCity不会将其结帐映射到工作区,只会将文件拉下来.

This worked fine on my dev box because the folder I was in is mapped to a TFS workspace. When I move it to my build server it no longer works because TeamCity doens't map it's checkouts to a workspace it just pulls the files down.

如何将文件检入TFS中的特定文件夹而又不在映射的工作空间中?那有可能吗?

How do I check files into a specific folder in TFS without being in a mapped workspace? Is that even possible?

推荐答案

我为使用GO的连续交付项目进行了一些工作.我结合使用PowerShell和.NET程序集提供程序与Team Explorer一起工作.我无法完全在PowerShell中运行它(尽管可能有办法!)

I worked on something to do this for our continuous delivery project using GO. I got it working using a combination of PowerShell and the .NET assemblies provider with Team Explorer. I could not get it working purely in PowerShell (although there may be a way!)

以下脚本将检入物料路径中包含的任何内容,并将其作为参数提供到指定的服务器路径(也是参数)中.您还可以指定要使用的凭据和TFS服务器的URL.

The following script will check-in whatever is contained in the material path which is supplied as a parameter into the specified server path (also a parameter). You can also specify credentials to use and a url for the TFS server.

此代码需要安装Visual Studio或TFS Team Explorer客户端.您需要在AssemblyPath参数中向脚本提供程序集的目录位置.如果找不到这些程序集,则脚本将出错并显示缺少的程序集.

This code requires either Visual Studio or the TFS Team Explorer client to be installed. You need to provide the directory location of the assemblies to the script in the AssemblyPath parameter. If these assemblies are not found then the script will error and show which ones are missing.

注意:该代码已有一段时间没有被检查,因此可能存在错别字等.如果您有任何问题,请告诉我,我将尽力提供帮助.

NOTE: The code has not been checked for a while so there may be typos etc. If you have any problems let me know and I will try to help.

[CmdletBinding(PositionalBinding=$false)]
Param(
    [Parameter(Mandatory)] [string] $ServerUrl,
    [Parameter(Mandatory)] [string] $ServerPath,
    [Parameter(Mandatory=$False)] [string] $Domain = "",
    [Parameter(Mandatory=$False)] [string] $Username = "",
    [Parameter(Mandatory=$False)] [System.Security.SecureString] $Password,
    [Parameter(Mandatory)] [string] [ValidateScript({($_ -eq $null) -or (Test-Path -Path $_ -PathType Container)})] $MaterialPath,
    [Parameter(Mandatory)] [string] [ValidateScript({ Test-Path -Path $_ -PathType Container})] $AssemblyPath
)

<#
    .SYNOPSIS
    Responsible for checking in files into Source Control
    .DESCRIPTION
#>

$clientDllName = "Microsoft.TeamFoundation.Client.dll"
$commonDllName = "Microsoft.TeamFoundation.Common.dll"
$versionControlClientDllName = "Microsoft.TeamFoundation.VersionControl.Client.dll"
$versionControlClientCommonDllName = "Microsoft.TeamFoundation.VersionControl.Common.dll"


#Create global variables to hold the value of Debug and Verbose action preferences which can then be used for all module function calls and passed into the remote session.
$verboseParameter = $PSCmdlet.MyInvocation.BoundParameters["Verbose"]
if ($verboseParameter -ne $null)
{
    $Global:Verbose = [bool]$verboseParameter.IsPresent
}
else
{
    $Global:Verbose = $false
}
$debugParameter = $PSCmdlet.MyInvocation.BoundParameters["Debug"]
if ($debugParameter -ne $null)
{
    $Global:Debug = [bool]$debugParameter.IsPresent
}
else
{
    $Global:Debug = $false
}

$scriptName = $(Split-Path -Leaf $PSCommandPath)

#Ensure any errors cause failure
$ErrorActionPreference = "Stop"

Write-Host "Running script ""$scriptName"" as user ""$env:USERDOMAIN\$env:USERNAME"""

#Check assembly path is a valid directory
If (Test-Path -Path $AssemblyPath -PathType Container)
{
    Write-Host "Loading required assemblies from assembly path ""$AssemblyPath"""

    $clientDllPath = Join-Path -Path $AssemblyPath -ChildPath $clientDllName
    $commonDllPath = Join-Path -Path $AssemblyPath -ChildPath $commonDllName
    $versionControlClientDllPath = Join-Path -Path $AssemblyPath -ChildPath $versionControlClientDllName
    $versionControlClientCommonDllPath = Join-Path -Path $AssemblyPath -ChildPath $versionControlClientCommonDllName

    If (!Test-Path -Path $clientDllPath -PathType Leaf)
    {
        Throw "Required assembly ""$clientDllName"" not found at path ""$clientDllPath"""
    }
    If (!Test-Path -Path $commonDllPath -PathType Leaf)
    {
        Throw "Required assembly ""$commonDllName"" not found at path ""$commonDllPath"""
    }
    If (!Test-Path -Path $versionControlClientDllPath -PathType Leaf)
    {
        Throw "Required assembly ""$versionControlClientDllName"" not found at path ""$versionControlClientDllPath"""
    }
    If (!Test-Path -Path $versionControlClientCommonDllPath -PathType Leaf)
    {
        Throw "Required assembly ""$versionControlClientCommonDllName"" not found at path ""$versionControlClientCommonDllPath"""
    }

    #Load the Assemblies
    [Reflection.Assembly]::LoadFrom($clientDllPath) | Out-Null
    [Reflection.Assembly]::LoadFrom($commonDllPath)| Out-Null
    [Reflection.Assembly]::LoadFrom($versionControlClientDllPath) | Out-Null
    [Reflection.Assembly]::LoadFrom($versionControlClientCommonDllPath) | Out-Null

    #If the credentials have been specified then create a credential object otherwise we will use the default ones
    If ($Username -and $Password)
    {

        $creds = New-Object System.Net.NetworkCredential($Username,$Password,$Domain)
        Write-Host "Created credential object for user ""$($creds.UserName)"" in domain ""$($creds.Domain)"""

        $tfsProjectCollection = New-Object Microsoft.TeamFoundation.Client.TFSTeamProjectCollection($ServerUrl, $creds)
    }
    else
    {
        Write-Host "Using default credentials for user ""$Env:Username"""
        $tfsProjectCollection = New-Object Microsoft.TeamFoundation.Client.TFSTeamProjectCollection($ServerUrl)
    }

    $versionControlType = [Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer]
    $versionControlServer = $tfsProjectCollection.GetService($versionControlType)

    Write-Host "Version control server authenticated user: $($versionControlServer.AuthenticatedUser)"

    #Create a local path in the temp directory to hold the workspace
    $LocalPath = Join-Path -Path $env:TEMP -ChildPath $([System.Guid]::NewGuid().ToString())
    $null = New-Item -Path $LocalPath -ItemType Directory

    #Create a "workspace" and map a local folder to a TFS location
    $workspaceName = "PowerShell Workspace_{0}" -f [System.Guid]::NewGuid().ToString()
    $workspace = $versionControlServer.CreateWorkspace($workspaceName, $versionControlServer.AuthenticatedUser)
    $workingfolder = New-Object Microsoft.TeamFoundation.VersionControl.Client.WorkingFolder($ServerPath,$LocalPath)
    $result = $workspace.CreateMapping($workingFolder)
    $result = $workspace.Get() #Get the latest version into the workspace

    Write-Host "Copying files from materials path ""$MaterialPath"" to temporary workspace path ""$LocalPath"""
    robocopy $MaterialPath $LocalPath /s | Out-Null

    $checkInComments = "Files automatically checked in by PowerShell script ""$scriptName"""

    #Submit file as a Pending Change and submit the change
    $result = $workspace.PendAdd($LocalPath,$true)
    $pendingChanges = $workspace.GetPendingChanges()

    Write-Host "Getting pending changes"

    #Only try to check in if there are changes
    If ($pendingChanges -ne $null)
    {
        If ($pendingChanges.Count -gt 0)
        {
            $changeSetId = $workspace.CheckIn($pendingChanges,$checkInComments)

            Write-Host "Successfully checked in ""$($pendingChanges.Count)"" changes using changeset id ""$changeSetId"""
        }
        else
        {
            Write-Host "No changes to check-in"
        }
    }
    else
    {
        Write-Host "No changes to check-in"
    }

    Write-Host "Deleting workspace and temporary folders"
    $result = $workspace.Delete()
    $null = Remove-Item -Path $LocalPath -Recurse -Force
}
else
{
    Write-Error "The path to required assemblies ""$AssemblyPath"" cannot be found"
}

这篇关于使用Powershell将文件检入TFS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆