Powershell&TFS_Part 1

简介:

前言

憋了这么久,Powershell主题终于开搞了。因为工作需要,Powershell可以说是半路出家边学边卖,所以这个主题可能没有Python和Linux那样分类、归纳得太好。而且我们跳过语法,直接应用开搞。语法什么的,就在一个个脚本中稍带介绍吧。

TFS

Team Fundation Server:是一种为Microsoft产品提供源代码管理、数据收集、报告和项目跟踪,和为协作软件开发的项目。 可作为独立的软件,或Visual Studio Team System(VSTS)服务器端。 
TFS包含: 
1. 团队项目集合:Team Foundation Server中所有数据的主要组织单位。集合可以包含一个或多个团队项目。 
2. 团队项目:团队用于共享开发特定软件技术或产品所需的团队活动的中心点。 
3. 应用层、数据层、客户端层:组成Team Foundation Server的逻辑层。这些层可能全部部署在同一台物理计算机上,也可能安装在多台计算机上。

对象模型

通过Team Foundation Server的对象模型,Team Foundation的逻辑层可以进行通信交互,软件集成商及其他公司可以自定义和扩展Team Foundation Server功能。 
Team Foundation Server对象模型是一组包含下列接口的托管 API: 
▲Team Foundation Framework 服务 
◆管理服务 
◆事件服务 
◆团队项目集合服务 
◆注册服务 
◆安全性服务 
◆位置服务 
◆标识管理服务 
◆目录服务 
◆作业服务 
◆属性服务 
版本控制对象模型 
▲工作项跟踪对象模型 
▲Team Foundation Build 对象模型 
API的动态链接库存放路径: 
D:\Visual Studio 2012\Microsoft Visual Studio 11.0\Common7\IDE\ReferenceAssemblies\v2.0

Powershell

Windows PowerShell是一种命令行外壳程序和脚本环境,使命令行用户和脚本编写者可以利用.NET Framework的强大功能。PowerShell v3采用新的cmdlet让管理员能够更深入到系统进程中,这些进程可以制作成可执行的文件或脚本(script)。一条cmdlet是一条轻量命令,Windows PowerShell运行时间在自动化脚本的环境里调用它。Cmdlet包括显示当前目录的Get-Location,访问文件内容的Get-Content和结束运行进程的Stop-Process。 
一些Cmdlet的例子: 
查看可以指令get-command *-service 
查看指令帮助手册get-help [commands] 
查看对象的成员信息get-service | get-member

Powershell面向对象

Powershell命令的输出即为对象。可以将输出对象发送给另一条命令以作为其输入。因此,Windows PowerShell为曾使用过其他外壳程序的人员提供了熟悉的界面,同时引入了新的、功能强大的命令行范例。通过允许发送对象(而不是文本),它扩展了在命令之间发送数据的概念。

Powershell默认会在PC中设置执行脚本权限

为防止恶意脚本的执行,PowerShell有一个执行策略,默认情况下,这个执行策略被设为受限的(Restricted),意味着PowerShell脚本无法执行,你可以使用下面的cmdlet命令获取当前的执行策略: 
Get-ExecutionPolicy 
你可以选择使用的执行策略有: 
Restricted (脚本不能运行) 
RemoteSigned (本地创建的脚本可以运行,但从网上下载的脚本不能运行,除非它们拥有由受信任的发布者签署的数字签名) 
AllSigned (仅当脚本由受信任的发布者签名才能运行) 
Unrestricted (脚本执行不受限制,不管来自哪里,也不管它们是否有签名都能执行)。 
你可以使用下面的cmdlet命令设置PowerShell的执行策略: 
Set-ExecutionPolicy <PolicyName>

调试脚本

断点

运行一个可能有BUG的PowerShell脚本时,保护自己的方法就是在脚本的关键位置插入断点,插入断点最简单的方法是根据行号插入。 
插入断点

New-PSBreakpoint -Script C:\Scripts\a.ps1 -Line 10 
  • 1
  • 1

你也可以将断点绑定到变量上,如果你希望你的脚本任何时候都可以修改a$的内容,可以使用下面的命令:

New-PSBreakpoint -Script C:\scripts\a.ps1 -variables a #注意,我在变量名后并没有包括美元符号。
  • 1
  • 1

可以和PSBreakpoint一起使用的动词包括New,Get,Enable,Disable和Remove。

Step

调试一个脚本时,有时可能需要逐行运行脚本,这时你可以使用Step-Into cmdlet命令,它会使脚本一行一行地执行,不管有没有设置断点,如果你想从这种步进式运行模式退出来,使用Step-Out cmdlet命令即可,但需要注意的是,使用Step-Out cmdlet命令后,断点仍然有效。 
顺便说一句,如果你的脚本使用了函数,你可能对Step-Out cmdlet更感兴趣,Step-Out的工作方式和Step-Into一样,不过,如果Step-Out调用了一个函数,Windows不会逐步执行,整个函数将会一次性执行。

Microsoft Visual Studio Team Foundation Server 2012 Power Tools

Power Tools可以使用Powershell来管理TFS,是连接TFS的必备工具。 
下载地址:https://visualstudiogallery.msdn.microsoft.com/b1ef7eb2-e084-4cb8-9bc7-06c3bad9148f 
Windows PowerShell Cmdlets
This tool provides a Windows PowerShell interface that supports basic version control commands and a pipeline and glue to enable scripting.
Work Item Templates
A plug-in to Visual Studio, this tool provides an add-in to the Team menu and a new section on the Work Items page in Team Explorer. Use this tool to create new work items with default values, or to easily apply values to existing work items. 
Power Tools支持的指令集

Command Description
addprojectportal Add or move portal for an existing team project
addprojectreports Add or overwrite reports for an existing team project
annotate Display line-by-line change information for a file
bind Convert VSS-bound solutions into TFS-bound solutions
branches Convert, reparent, list, and update branches
builddefinition Clone, Diff or Dump build definitions
buildprocesstemplate Manage build process templates
connections Modifies Team Explorer client connection settings
createteamproject Create a team project
getcs Get only the changes in a particular changeset
online Pend adds, edits, deletes to writable files
query Query for work items
review Review (diff/view) workspace changes
scorch Ensure source control and the local disk are identical
searchcs Search for changesets matching specific criteria
treeclean Delete files and folders not under version control
unshelve Unshelve into workspace with pending changes
uu Undo changes to unchanged files in the workspace
workitem Create, update, or view work items

安装Power Tools后添加Snapin: 
Snapin是一组.NET程序集,或者程序集集合,其中包含了Cmdlets、提供程序、类型扩展和格式元数据。

Add-PSSnapin Microsoft.TeamFoundation.PowerShell 
  • 1
  • 1

查看Snapin指令集

Get-Command -Module Microsoft.TeamFoundation.PowerShell 
  • 1
  • 1

程序集

程序集是 .NET Framework编程的基本组成部分,类似于API、动态链接库。 
1. 程序集(assembly)是一个或多个托管模块,以及一些资源文件的逻辑组合。 
2. 程序集是组件复用,以及实施安全策略和版本策略的最小单位。 
3. 程序集是包含一个或者多个类型定义文件和资源文件的集合。在程序集包含的所有文件中,有一个文件用于保存清单。(清单是元数据部分中一组数据表的集合,其中包含了程序集中一部分文件的名称,描述了程序集的版本,语言文化,发布者,共有导出类型,以及组成该程序集的所有文件)。 
4. 作为一个单元进行版本控制和部署的一个或多个文件的集合。程序集是 .NET Framework应用程序的主要构造块。所有托管类型和资源都包含在某个程序集内,并被标记为只能在该程序集的内部访问,或者被标记为可以从其他程序集中的代码访问。程序集在安全方面也起着重要作用。代码访问安全系统使用程序集信息来确定为程序集中的代码授予的权限集。

连接到TFS

在安装了Power Tools之后,只需要执行两条指令:TFS2012修复了64位机加注册表的BUG。

PS C:\Users\JMilk.fan> $serverUrl = '[TFSURL]'
PS C:\Users\JMilk.fan> Get-TfsServer -Name $serverUrl
  • 1
  • 2
  • 1
  • 2

Team Project Collections:团队项目集合(connect to ream foundation server),一个Team Project Collection可以包含很多个Team Project,类似于VS解决方案与项目之间的一对多关系。

获取TPCList

Team Project Collections:团队项目集合(connect to ream foundation server),一个Team Project Collection可以包含很多个Team Project,类似于VS解决方案与项目之间的一对多关系。

[Reflection.Assembly]::Load("Microsoft.TeamFoundation.Client, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
$instanceBaseUrl = "TFSURL";

$tfsServer = New-Object Microsoft.TeamFoundation.Client.TfsConfigurationServer $instanceBaseUrl;

# 调用 ITeamProjectCollectionService
$tpcSvc = $tfsServer.GetService([Microsoft.TeamFoundation.Framework.Client.ITeamProjectCollectionService]);

foreach($co in $tpcSvc.GetCollections())
{
    Write-Host $co.Name    #打印TPC内的项目名称
} 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

获取TPC中的team project

[Reflection.Assembly]::Load("Microsoft.TeamFoundation.Client, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");

$url = New-Object -TypeName Uri -ArgumentList "TFSURL"

$project = New-Object -TypeName Microsoft.TeamFoundation.Client.TfsTeamProjectCollection -ArgumentList $url

Get-TfsChildItem -Server $project | Select ServerItem, CheckinDate 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

获取workspace

加载对象模型程序集:类似一般项目添加引用,不加载无法使用程序集的属性和方法。 
LoadWithPartialName(”)函数,需要传入程序集的dll文件名字或文件全路径。如果传入的是dll文件名字,则会先在当前启动路径当中查找。

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")
  • 1
  • 1

创建对象

$url = New-Object -TypeName Uri -ArgumentList "TFSURL"
$project = New-Object -TypeName Microsoft.TeamFoundation.Client.TfsTeamProjectCollection -ArgumentList $url
Get-TfsWorkspace -Server $project 
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

获取workItemStore

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.WorkItemTracking.Client")
$teamProjectCollection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection("TFSURL")
$ws = $teamProjectCollection.GetService([type]"Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore")
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

访问workitem

使用TFS API编程访问TFS服务器中workitem,首先要明确TFS服务器的URL,TFS服务器访问就像访问网站。需要通过一个网络地址。 
workitem相关程序集的组织结构

Microsoft.TeamFoundation.Client.TfsTeamProjectCollection;
Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore;
Microsoft.TeamFoundation.WorkItemTracking.Client.Project;
Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemType;
Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

TfsTeamProjectCollection像是一座物理建筑,它有门牌编号,我们通过门牌号码找到该建筑。 
WorkItemStore像是图书馆,里边存放各种类别,各种年级的书籍。 
Project像是年级的概念,每个年级是隔离的,每个年级也都有不同类别的书,比如各年级都有语文,数学,英语等类别的书籍。 
WorkItemType是类别的概念,比如数学,英语。每个Project有不同WorkItemType的工作项。如一年纪有语文学习的书籍,数学学习的书籍;二年级也有语文类的书籍,数学类的书籍。

获取workItem中的项目

查询TFS中的工作项是通过SQL语句Query出来的,返回结果是Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemCollection.

$incidents = $ws.Query(@"
SELECT [System.Id]
FROM WorkItems 
WHERE 
[System.WorkItemType] = 'RDIncident'  AND  [System.State] = 'Triage' AND   [System.Title] Contains 'Rma:'
"@)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

获取指定TPC的更改集

[Reflection.Assembly]::Load("Microsoft.TeamFoundation.Client, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");

$url = New-Object -TypeName Uri -ArgumentList "TFSURL"

$co = New-Object -TypeName Microsoft.TeamFoundation.Client.TfsTeamProjectCollection -ArgumentList $url

Get-TfsChangeset -Server $co –Latest 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

URI\URL\URN的区别

URI:是uniform resource identifier,统一资源标识符,用来唯一的标识一个资源。 
URL:是uniform resource locator,统一资源定位器,它是一种具体的URI,即URL可以用来标识一个资源,而且还指明了如何locate这个资源。 
URN:uniform resource name,统一资源命名,是通过名字来标识资源,比如jmilk@fan.com。 
也就是说,URI是以一种抽象的,高层次概念定义统一资源标识,而URL和URN则是具体的资源标识的方式。URL和URN都是一种URI。

最后

因为工作的保密性,所以不能拿手上项目的代码来充当例子,就在网上找到了一些相似的代码来作说明。:-(

Jmilk

转载:http://blog.csdn.net/jmilk/article/details/50086191

目录
相关文章
|
5月前
|
分布式计算 Java Spark
windows中pyspark的配置
windows中pyspark的配置
73 0
|
5月前
|
Apache Windows
Windows下启动Apache报错:ServerRoot must be a valid directory
Windows下启动Apache报错:ServerRoot must be a valid directory
144 0
|
Docker Windows 容器
在Docker中的Neo4j导入CSV文件报错:Couldn‘t load the external resource at: file:/...解决办法
在Docker中的Neo4j导入CSV文件报错:Couldn‘t load the external resource at: file:/...解决办法
415 0
在Docker中的Neo4j导入CSV文件报错:Couldn‘t load the external resource at: file:/...解决办法
|
分布式计算 Hadoop Linux
Windows运行Hadoop时报错:Could not locate executablenull\bin\winutils.exe in the Hadoo
Windows运行Hadoop时报错:Could not locate executable null\bin\winutils.exe in the Hadoop binaries 从标题报错中就可以看到,不能定位到winutils.exe,在hadoop中
144 0
|
Python
Python 库配置问题:“Couldn‘t find ffmpeg or avconv - defaulting to ffmpeg, but may not work“. 解决办法
Python 库配置问题:“Couldn‘t find ffmpeg or avconv - defaulting to ffmpeg, but may not work“. 解决办法
748 0
Python 库配置问题:“Couldn‘t find ffmpeg or avconv - defaulting to ffmpeg, but may not work“. 解决办法
命令行里对SAP Spartacus执行命令ng test core
命令行里对SAP Spartacus执行命令ng test core
命令行里对SAP Spartacus执行命令ng test core