【发布时间】:2017-04-12 17:58:29
【问题描述】:
我编写了一个 Powershell 脚本,它连接到我们的 TFS 服务器、创建工作区、下载最新源代码并执行夜间构建和发布。
我遇到的问题是它总是与我自己的凭据连接,根据我的阅读,这是因为我以我的身份登录到机器上。我创建了一个新的域用户帐户,并且我们在 TFS 中授予了此管理员权限,但是我无法让脚本使用这些凭据。
下面是处理初始连接和当前工作区创建的脚本部分:
$subfolder = [System.Guid]::NewGuid().ToString()
$tfsServer = "http://tfsserver:8080/tfs/xyz"
$tfsCollection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($tfsServer)
$tfsVersionCtrlType = [Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer]
$tfsVersionCtrl = $tfsCollection.GetService([type] $tfsVersionCtrlType)
$tfsWorkspace = $tfsVersionCtrl.CreateWorkspace($subfolder, $tfsVersionCtrl.AuthenticatedUser)
为了完整起见,以下是“获取”逻辑的其余部分:
$tfsWorkspace.Map($serverLocation, $localLocation)
$recursion = [Microsoft.TeamFoundation.VersionControl.Client.RecursionType]::Full
$versionSpec = [Microsoft.TeamFoundation.VersionControl.Client.VersionSpec]::Latest
$itemSpec = New-Object Microsoft.TeamFoundation.VersionControl.Client.ItemSpec($serverLocation, $recursion)
$fileRequest = New-Object Microsoft.TeamFoundation.VersionControl.Client.GetRequest($itemSpec, $versionSpec)
$getStatus = $tfsWorkspace.Get($fileRequest, [Microsoft.TeamFoundation.VersionControl.Client.GetOptions]::Overwrite)
我在通过 Powershell 使用 TFS 时遇到的主要问题是,Microsoft 一直在改变他们的实现,在某些情况下是相当彻底的,而我们留下的只是互联网上散落的代码帖子和文档的碎片(包括这里在 SO 上),它引用了旧的插件和其他现在已经过时的引用,它们会让你走上无数错误的道路。
无论如何,所以我尝试创建 Windows 凭据、PSCredentials 等(似乎没有被任何东西接受),提供 ICredential 的旧方法现在已经过时了,我真的不知道该去哪里。
基本上,我只想创建一个工作区、签出项目、更新文件并重新签入——所有这些都是作为我们新的“tfsService”用户帐户。请帮忙...
更新:
根据@Nick 的回答,我需要进行以下更改。请注意 [System.Uri] 的使用,这是为我工作所必需的(不确定这是否是我的设置的怪癖,因为其他人似乎不需要这样做)。此外,我需要将 TfsTeamProjectCollection 的构造函数调用全部放在一行上,因为按照 Nick 的示例,将其拆分为单独的行,这对我也不起作用。
$cred = New-Object System.Net.NetworkCredential("username", "password", "domain")
$tfsCollection = New-Object Microsoft.TeamFoundation.Client.TfsTeamProjectCollection([System.Uri]$tfsServer, $cred)
#$tfsCollection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($tfsServer)
$tfsVersionCtrlType = [Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer]
$tfsVersionCtrl = $tfsCollection.GetService([type] $tfsVersionCtrlType)
$tfsWorkspace = $tfsVersionCtrl.CreateWorkspace($subfolder, $tfsVersionCtrl.AuthenticatedUser)
【问题讨论】:
-
立即浮现在脑海中的问题是,“当 TFS 已经具备构建和部署能力时,为什么还要重新发明轮子?”
-
嗨丹尼尔,这不是我真正想要的,但正如你所问的,这家公司只有 TFS2013,没有部署能力。你有这个问题的答案吗?
标签: powershell tfs