简体   繁体   English

使用PowerShell从构建步骤访问TFS TeamProjectCollection

[英]Access TFS TeamProjectCollection from build step with PowerShell

I'm trying to create a pre-build step with powershell in one of our team projects to increment the build number for all assemblies/applications within that VS solution. 我正在尝试在我们的一个团队项目中使用powershell创建一个预构建步骤,以增加该VS解决方案中所有程序集/应用程序的构建号。 This is for TFS2015-update 2. 这是针对TFS2015更新2的。

I found some example code here and there online and have pieced together what I imagine would work. 我在网上到处都找到了一些示例代码,并拼凑出了我想像的效果。 I can run the powershell script manually on the TFS server (authenticated to the server as my windows account) and it works fine, but yet it won't function correctly within the build process. 我可以在TFS服务器上手动运行powershell脚本(已通过Windows帐户验证为服务器),它可以正常工作,但在构建过程中无法正常运行。

Here is the sample code in question where I keep getting my error. 这是有问题的示例代码,我不断收到错误。 It has to do with authentication/connection to the TFS server: 它与身份验证/与TFS服务器的连接有关:

$CollectionUrl=New-Object System.Uri("$env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI")
$BuildUrl="$env:BUILD_BUILDURI"

# get the team project collection
$TeamProjectCollection=[Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($CollectionUrl)
# get the build server
$BuildServer=$TeamProjectCollection.GetService([Microsoft.TeamFoundation.Build.Client.IBuildServer])

# get the build detail
$BuildDetail=$BuildServer.GetBuild($BuildUrl)

# get the build definition
$BuildDefinition=$BuildDetail.BuildDefinition

There error I am getting is: 我得到的错误是:

Exception calling "GetService" with "1" argument(s): "TF30063: You are not authorized to access http://servername/TPCName 带有“ 1”参数的异常调用“ GetService”:“ TF30063:您无权访问http:// servername / TPCName

I have tried using both the call above to get the TPC, and the constructer for it and neither works. 我尝试使用上面的调用来获取TPC,以及它的构造方法,但都没有用。

$TeamProjectCollection=New-Object Microsoft.TeamFoundation.Client.TfsTeamProjectCollection($CollectionUrl)

So I started trying to build a credential object based on my login credentials to the server and see if I could connect to the TPC that way. 因此,我开始尝试根据到服务器的登录凭据构建凭据对象,并查看是否可以通过这种方式连接到TPC。 Still doesn't work (on server or in build process), but different error: 仍然不起作用(在服务器上或在构建过程中),但是出现了不同的错误:

$netCred = New-Object System.Net.NetworkCredential($username,$password)
$basicCred = New-Object Microsoft.TeamFoundation.Client.BasicAuthCredential($netCred)
$tfsCred = New-Object Microsoft.TeamFoundation.Client.TfsClientCredentials($basicCred)

# get the team project collection
$TeamProjectCollection=New-Object Microsoft.TeamFoundation.Client.TfsTeamProjectCollection($CollectionUrl, $tfsCred)

Throws the exception: 引发异常:

Exception calling "GetService" with "1" argument(s): "TF400324: Team Foundation services are not available from the server. 带有“ 1”参数的异常调用“ GetService”:“ TF400324:服务器上没有Team Foundation服务。

I have also tried my best at modifying TFS permissions for the windows account that TFS service runs as to make sure there were no issues there. 我还尽力为TFS服务运行的Windows帐户修改TFS权限,以确保那里没有问题。 I'm fairly certain I added it to all roles/groups that my identity was in, but still the build wouldn't authenticate. 我相当确定我已将其添加到我的身份所在的所有角色/组中,但仍无法进行身份验证。

I'm at a loss at this point. 我现在不知所措。 Can someone help me figure out how to connect to the TFS server correctly using defined credentials, or help me adjust what permissions the service account, identity, configuration choices, etc; 有人可以帮助我确定如何使用定义的凭据正确连接到TFS服务器,还是可以帮助我调整服务帐户,身份,配置选择等的权限; needed to access the teamProjectCollection. 需要访问teamProjectCollection。

You need to specifically instruct TFS/VSTS to allow the executing build to call back to TFS/VSTS. 您需要专门指示TFS / VSTS允许执行中的构建回叫TFS / VSTS。 This protects your server for malicious developers exiting build scripts. 这样可以保护您的服务器免受恶意开发人员退出构建脚本的侵害。

在此处输入图片说明

You should see an "enable Auth" option in the Options Tab of your Build. 您应该在构建的“选项”选项卡中看到“启用身份验证”选项。

**note: You should use the Task SDK provided: https://github.com/Microsoft/vsts-task-lib **注意:您应该使用提供的Task SDK: https//github.com/Microsoft/vsts-task-lib

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TFS构建powershell脚本步骤:无法访问$(Date:yyyyMMdd)变量 - TFS build powershell script step : Not able to access $(Date:yyyyMMdd) variable TFS vNext Powershell构建步骤正在使用旧版本的Powershell模块 - TFS vNext Powershell build step is using older version of Powershell module TFS在目标计算机上构建“ PowerShell”步骤失败:如何调试? - TFS Build “PowerShell on Target Machines” Step Fails: How to debug? TFS中的Robocopy构建PowerShell步骤报告失败但没有错误 - Robocopy in TFS Build PowerShell Step Reports Failure But Has No Error 将值从Powershell传递到TFS Build Workflow - Pass a value from powershell to TFS Build Workflow 验证TFS build 2015中先前构建步骤的结果 - Validating result from previous build step in TFS build 2015 TFS:在Powershell脚本中访问在构建模板中使用的局部变量 - TFS: Access a local variable used in build template in a powershell script 如何在TFS Build Definition中获取PowerShell脚本任务,以便与x64 MSBuild步骤配合使用? - How to get PowerShell Script Task in TFS Build Definition to play nice with x64 MSBuild step? 将秘密变量从TFS Build传递给Powershell脚本 - Pass Secret Variable from TFS Build to Powershell script 如何使TFS 2015中的PowerShell任务失败 - How to fail the build from a PowerShell task in TFS 2015
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM