Encapsulate Team Room API calls with a PowerShell Commandlet

This post is Part 2 in the three part series on Release Management + PowerShell + TFS TeamRoom APIIn this second post I am going to “PowerShell enable” the TeamRoomService API I created in part 1.

Because i am fairly new to PowerShelll, finding a way to achieve this took few searches and some reading on MSDN. Luckily there is more than enough on it! A good starter is here: http://msdn.microsoft.com/en-us/library/dd878294(v=vs.85).aspx

I decided to build from the PSCmdLet functionality while I wanted to preserve properties (remember the TFSURL, TFS UserName and TFS Password for the service?) between possible multiple TeamRoom calls. This saves typing verbose parameters over and over again. To preserve the settings we need to utilize the session state and are therefor required inheriting from the PSCmdLet class.

I decided to create a custom baseclass (PSCmdletBase) to be able to use in the actual Command Lets (Add, Find, Get, Remove). The base class checks if the required parameters are supplied.

P2_PSCmdletBase

Now let’s go create the CommandLet for posting a message. The class will inherit from the PSCmdLetbase. It has two mandatory properties defined. One for the message the other for the Team Room name.

P2_TeamRoomMessage_Add

The main method is quite simple, because the TeamRoomService previously created contains all the logic. Below an instance is created, the properties are provided, and the PostMessage method is called.

P2_PostMessage

Having this compiled into a DLL we are ready to get to use it from the PowerShell commandline. Again this took some reading, I started on this page http://msdn.microsoft.com/en-us/library/dd878310(v=vs.85).aspx. Browsing a little further I came across this page http://msdn.microsoft.com/en-us/library/dd901839(v=vs.85).aspx. It shows us that we can import a .DLL as a module. Let give that a try.

First open a PowerShell Command window (running as Administrator). Mind that executing command from the PowerShell Console might require additional execution permissions. Please Read about them here http://go.microsoft.com/fwlink/?LinkID=135170.

For my demo purposes I set my PowerShell to Unrestricted mode.

P2_PowerShell_Set_ExecutionPolicy

Now let’s provide the command to import the module. If all goes well (no visual feedback) its successful. Note I am importing directly from my project output directory.

P2_PowerShell_Import_Module

Now we should be able to call the CommandLet’s “Add-TeamRoomMessage” method. There are several ways of calling it. Remember that we first need to set the required variables!  (I intentionally *** my password, it will be typed in plain text).

P2_PowerShell_Set_Variables

Now we are ready to use the CommandLet. We pass the TeamRoom and Message parameters to the command directly and hit enter.

P2_PowerShell_Add_TeamRoomMessage

We receive feedback hat we succesfully posted the message with ID 1242.

Now let’s browse to the TeamRoom to see our message! Yes, there it is!

P2_TeamRoomSite_ShowingMessage

This wraps up part two of this series! In the next and last part of this series we will dive into Visual Studio Release Management to allow us to post the message from there!

Note: The source code will be posted as a complete download with the next post so you get a full working solution.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s