Solved

Powershell script - How to monitor a folder for a new build?

Posted on 2010-09-08
10
1,803 Views
Last Modified: 2013-11-25
Hello Experts!

In Powershell, how do you constantly monitor a folder or network share for changes such as a new build?  (There could be multiple builds every day in which case the folder name for the latest build has the following naming convention - 'Project Name - Test_yyyymmdd.#'  where # is an incrementing value for any given build on that day.)
  -- preferrably ties in with TFS to monitor when the latest successful build is complete

Thank you!
0
Comment
Question by:sp70
  • 5
  • 3
10 Comments
 
LVL 13

Accepted Solution

by:
soostibi earned 250 total points
ID: 33640852
I do not know TFS, but the folder watcher part can be something like this. You insert your code instead of my line 9 in the attached code. The watcher runs until you close the PowerShell window.
$watcher = New-Object System.IO.FileSystemWatcher

$watcher.NotifyFilter = "DirectoryName", "LastWrite"

$watcher.IncludeSubdirectories = $false

$watcher.Path = "C:\yourpath"

"Changed", "Created", "Renamed" | 

	ForEach-Object {

		Register-ObjectEvent -InputObject $watcher -EventName $_ -SourceIdentifier "watcher.$_" -Action {

			if((get-item $eventargs.fullpath).psiscontainer -and $eventargs.name -match 'Project Name - Test'){ 

				(get-item $eventargs.fullpath).psiscontainer, $eventargs.name | Out-Host

			}

		}

	}

Open in new window

0
 
LVL 23

Expert Comment

by:Admin3k
ID: 33644856
You can use TFS itself to do the monitoring of the folder & running the Deployment scripts afterwards.
Is this TFS 2010 or 2008 ?
Both support continuous integration builds, you can also optionally configure your team build to run unit tests after the build is done & Deploy or not based on status of the test results.
For TFS 2008
For TFS 2010
Also , just in case you are a Powershell Fan & prefer the "do it yourself" approach , then why don't you take a look at the TFS deployer project.
it basically does what you are asking , a service listening for particular TFS events, then will run powershell scripts based on certain Criteria, 
Hope this helps.


0
 

Author Comment

by:sp70
ID: 33647480
Admin3k,
Thank you for the information.  I'm not in charge of the build system.  Your recommendation would probably fit my needs, but our lead developer will not put Beta software in this specific environment.  My needs (for Test) were basically to monitor a drop share for daily builds every morning and automatically set up my testing environments - a winsrv2k3 and an XP machine - based on this initial trigger.  I am looking for scripts or available utilities so I don't have to reinvent the wheel.
Someone told me about taking advantage of powershell by looking at the directory list in an array.  Admittedly, I'm VERY amature with regard to scripting & programming, so I'm diving into powershell.  
Thanks again for the info.
0
 
LVL 23

Assisted Solution

by:Admin3k
Admin3k earned 250 total points
ID: 33648099
>>I am looking for scripts or available utilities so I don't have to reinvent the wheel
This is exactly my point, you will not find lots of ready made scripts that are aimed at Team build, The deployment is usually triggered from the Team build itself, the functionality is there in TFS for us to use.
Although the argument about Beta programs is valid, However with lots of programs from Codeplex , this is a community release , The majority of the developers are either Microsoft staff or MVPs who contribute to the open source community.
The quality of the software is very high, and is updated regularly, and the fact that the source code is available is a bonus.
Also another thing to note, is that Nothing will be installed on the TFS server itself, you install the Deployer on a separate test machine, the TFS deployer service will subscribe to an Event (Build success, Build quality changes,etc..) (web service on TFS application tier), then run the script you wish 
http://notgartner.wordpress.com/2006/12/16/getting-started-with-tfs-deployer/
Deployment scripts for .NET applications will be relatively simple to write, the activities are not that much.
Copy , Backup, Stop remote services (use Powershell 2 remote feature, or PSexec from Sysinternals), Register DLLs to GAC (Gacutil), Compile and publish websites, restart IIS ,etc..
All this can be either achieved by the build master writing or using Custom MSBuild tasks , or by running Post build scripts
With TFS 2008, there is a simple approach to deploy the builds once they are completed , by editing the Build Definition XML file  (TFSBuild.Proj) , and adding the below custom properties to copy Binaries to an additional Drop folder & run external scripts.
AfterDropBuild
exec Command
http://blogs.microsoft.co.il/blogs/shair/archive/2008/10/01/how-to-add-custom-build-step-to-teambuild-with-visual-studio-2008.aspx


In Team Build 2010 , The same can be achieved by adding the Workflow Activity InvokeProcess , then add your own script as the parameter.
If the above is out of scope , and you just wish to monitor the Build drop location for updates and need the code for doing so , then as mentioned above the FileSystemWatcher Class should do the trick.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 23

Expert Comment

by:Admin3k
ID: 33648210
Another thing that came to mind, if you wish to configure your script to monitor the Build Drop location for new Folders, then Deploy based only on the fact that the new folder was created, some errors will occur.
The folder is created early in the team build process , to store the Build log first, then the binaries once the compilation is complete, this can take a very long time based on the Size of the solution you are building, any additional Unit tests that will run ,etc..
Also Partially succeeded, failed & canceled builds will still create their own folders, you need to ensure that what will be deployed is a proper successful build.
Thus the general direction to initiate the deployment Post the successful build event from the Build server.


0
 

Author Closing Comment

by:sp70
ID: 33665436
Thank you both for wonderful solutions.

soostibi,
Being new to powershell, I'll try and get this to work for me, but not quite sure of the syntax at this point.

Admin3k,
Thank you for letting me know that I didn't have to put this on the TFS Server.  I'll also try implementing this approach.  I'm actually watching for Test Builds - though not currently of a given quality.  Part of my daily process is to check how the morning build proceeded before doing any sort of testing.

We are using TFS 2010.

The main problem I've been faced with (that appears to be getting resolved today) was that our entire dev and test team has NOT had administrative rights to our machines.  (No - I did not stutter - yes it's insane).  That's been the main cause of delay with actually implementing the recommendations posted here.

Thanks again for the help!
0
 
LVL 23

Expert Comment

by:Admin3k
ID: 33665741
If your TFS administrator has enabled e-mail notification, it may be worth its while to use the project alerts tool in Team explorer to subscribe to the built completion events.
right click your team project in Visual studio>Project alerts & choose to be notified by e-mail (enter yours) when a build is completed.


0
 

Author Comment

by:sp70
ID: 33666492
Admin3k,
Email Notification - that's a touchy subject.  Long story short, ain't happening.  We're contracting with a company that's utilizing Lotus Notes and the Lead Dev already said that email integration will not be happening.  I only wish.  Believe me, they're very frustrated about many components of this project and the limitations that have been imposed on us.
0
 
LVL 23

Expert Comment

by:Admin3k
ID: 33667756
Things seem tough.
now let us see , you need a standalone tool / script that will monitor builds being queued , finished & notify of complete / broken builds without sending an email 
There is a nifty little app that used to be part of TFS power tools, but  I believe it ships now With Visual studio, you should be able to find it somewhere in Start Menu >visual studio, called Build notification tool, it will do just that.


0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
The viewer will learn how to dynamically set the form action using jQuery.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now