Thursday, February 27, 2014

Introducing TfsManager Some PowerShell cmdlets to work with Tfs

TfsManager is a project hosted on github by me.  The idea came about this project when I was fiddling around with Team Foundation Server Object Model.  Using the .dlls that are provided as part of Visual Studio installation or Team Explorer, you can pretty much do anything with TFS using code.  I wanted to explore what I can do with that Object Model.  At first sight these PowerShell cmdlets don’t seem to solve a significant problem that can’t be solved with traditional GUI or TF.exe.  But let’s hold on to that thought for a while.  Let’s get started.

github url: https://github.com/mitulsuthar/TfsManager

You can fork it, submit issues and pull requests.  I will be happy to work with you. 

Install the PowerShell module by using this single command. 

(new-object Net.WebClient).DownloadString("https://raw.github.com/mitulsuthar/TfsManager/master/tools/InstallTfsManager.ps1") | iex

This will copy .dll file into your Modules folder under a directory called TfsManager.Cmdlets.


image


Note: There is no help file included right now so traditional help cmdlets won’t return any examples.


1. Get all the cmdlets available for this module.


PS C:\> get-command -module TfsManager.cmdlets


 image


2. Get all the Tfs Server Url configured on your machine.


PS C:\>Get-TfsServer


image


Note: I apologize for some whitening on the output as I had to hide some sensitive information.


If you have just one Tfs Server then it will return a String object and if you have more than one then it will return array of string.


3. Get all the Team Project Collection for a particular Tfs Server. 

PS C:\> Get-TfsProjectCollection -ServerUri (get-TfsServer)[0]

From (2) you can see I am referencing the first Tfs Server which is the VisualStudio hosted version of Tfs.  If your credentials aren’t cached then it will ask you to first connect to it using a Microsoft Account.  After you provide your credentials you will see your Tfs Project Collections.


image


Alright. Here comes why these cmdlets are of interest. Because they provide you the full .Net object work inside PowerShell.  Let’s get the returned Project Collection into a variable $col and do a get-member.


image


Ah ! See all the methods and properties you can get to.  But I know you are not happy with this.  Let’s do something more.  So if you have worked with Tf.exe then you might know that Tf.exe commands work if you provide TeamProjectcollectionUri or if you are currently on a path that is mapped to your project folder.  I am going to navigate to a project folder locally inside PowerShell and then try to run some cmdlets.


3. Get mapped workspace for a particular path

PS C:\Your Mapped Folder Path>Get-TfsWorkspace

image


Aha! It returns a .Net object of Type Workspace.  Let’s do Get-Member on the workspace and see if you are impressed or not.  There are more methods and properties that I can’t show in the screenshot below.  You can do all the interesting stuff with just Get-TfsWorkspace command. You can get reference to objects returned by these cmdlets and write your own script to do some work. 


image


Actually this project TfsManager is a by product of something I am working with managing Tfs Builds. I will share that project some time later.  But talking of Builds let find out if there is something for us in these cmdlets.


4. Get all the BuildDefinitions associated with the currently mapped folder or a project.

PS C:\Your Mapped Folder Path>Get-TfsBuildDefinition

Warning: This will enumerate all the properties of a BuildDefinition  and it will clutter your output. 


Again Get-Member to the rescue.  After getting a reference to our build definition we perform the following methods on that BuildDefinition.  For shorthand do Get-TfsBuildDefinition | Select Name


image


Alright one last command before I conclude this post.


5. Get all the builds for a particular mapped path

PS C:\Your Mapped Folder Path>Get-TfsBuild

This command will first get the Team Project and find all the builds. Again be careful it will enumerate all the properties for the object IBuildDetail.  So a Get-Member again to see all the methods.


image


To see just the important information you can do the following command.


image


Sometimes if you have lots of builds older than certain date or meet certain criteria and want to just delete them then you can do that easily in a script.


6.  Ok I couldn’t resist this one.  Not very useful but you can still play with it. For a particular path you can get PendingChanges too.


Get-TfsPendingChanges | select localitem


So that’s about it for now.  I have lots of hypothetical scenarios coming in my mind that I can do with these cmdlets but I want to hear from you on what do you think about this project and what are the bugs and issues. 

Wednesday, February 12, 2014

Work with TF.exe from Visual Studio Package Manager Console

I work with Team Foundation Server and I have found sometimes it takes lots of mousing [I mean mouse movements] to accomplish a certain task.  While if you don’t know then all the commands that are available to us from Visual Studio are also available from the command line.  Oh! wait it is the Visual Studio command prompt. So there is just one thing you will have to do in order to make TF.exe available from almost everywhere.  Add the following path to your system environment variable and TF.exe will be now accessible from the command prompt. 

C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE

[This is the path for Visual Studio 2013 installed on my machine. If you are using VS2012 then change 12.0 to 11.0 version]

After this you can just do like this Win + R + cmd and Enter.  Then just type TF /?

image

Since this is available from normal command prompt what would prevent us from opening PowerShell and try there. 

image

Oh! It works there.  Btw this was my favorite approach to work with Team Foundation Server until I thought what if this would work from Visual Studio Package Manager Console which is using PowerShell. 

image

Awesome. So why I like this approach? If I am working on something in regular PowerShell and to find pending changes I have to navigate to my working directory to do tf status.  But with Package Manager Console I can just do tf status and it gives me pending changes for just that project and that particular branch that I am in.  Try doing pwd it will give the current location of your project.  And since docking in VS is supported for all the windows I just dock Package Manager Console to another monitor.

I use regular PowerShell and Package Manager Console for committing changes to TFS interchangeably. 

Sunday, February 9, 2014

PSBuild makes working with MSBuild.exe easier from PowerShell

PSBuild is a project started by Sayed Hashimi to simplify working with MSBuild.exe from PowerShell. It is hosted on gitHub.  I really like this project because I love PowerShell and if I can do things in PowerShell then I am more inclined to use PowerShell first. 

PSBuild installs into your Modules folder and is available once you install it by the following very simple command. I love projects that go one extra mile to make setup as easier as possible.  Open PowerShell and copy the following command and paste into console by just right clicking your mouse.

(new-object Net.WebClient).DownloadString("https://raw.github.com/ligershark/psbuild/master/src/GetPSBuild.ps1") | iex

Once you have installed it, you can list all the commands available by this command.

Get-Command –Module PSBuild

image


The most important command we are interested here is Invoke-MSBuild cmdlet.

Get-Help Invoke-MSBuild

There are lots of examples on how to use Invoke-MSBuild

Get-Help Invoke-MSBuild -Examples

image


MSbuild is very powerful and rightly so as it is used to build visual studio projects.  With Visual Studio 2013 MSbuild is being shipped with Visual Studio and is now referred to as MSBuild 2013. Previously it was shipped with .Net framework and versioning was also different now the version is bumped up to 12.0 from 4.0 to match with VS2013 (it is 12.0). It also comes as separate package known as Build Tools Package. This allows you to install the Build Tools Package as standalone and create a light weight build server. Path to MSBuild has also been changed and Invoke-MSBuild makes it easier for us to not know these different versions and hunt around for paths.  If you want to get MSBuild.exe then you can use one of the cmdlets as


Get-MSbuild


image


Another one to check the versions


& (Get-MSBuild) /version


image


If you want to get the MSBuild.exe help then


& (Get-MSBuild) /help


As you have noticed,  you can invoke MSbuild like this if you want but Invoke-PowerShell is better because you don’t have to second guess the syntax of properties and target that you will specify.  You pass properties as hash table. Let’s do something interesting here.


I am going to create an empty asp.net mvc website and try to build it and then publish to IIS website using a publish profile.


image


Simple call to build the project.
PS C:\BadSourceCodes\demoWebApp\demoWebApp> Invoke-MSBuild .\demoWebApp.csproj


image


You can get the latest log file


PS C:\Get-PSbuildLog


And if you want to open the log file then


PS C:\Open-PSBuildLog


Since this is a web project you can setup web publishing inside visual studio.  You can see these series of article on how to do web publishing here. I have created a website in IIS7 and setup Web Deploy publishing for that website.  Thanks to Sayed for his blog post - I was able to publish my website to the IIS website I by passing these parameters.


>Invoke-MSbuild .\demoWebApp.csproj –DefaultProperties (@{'Configuration'='Release';'DeployOnBuild'='true';'PublishProfile'='Default Settings';'username'='myusername';'password'=’password';'AllowUntrustedCertificate'='true'})


image


With PSBuild you can create a script that you can schedule to auto run on your build server that will automatically build the project and deploy the web project to IIS website.


Go check out the project on github and happy automating.