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

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!
Who is Participating?
soostibiConnect With a Mentor Commented:
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

Mohamed OsamaSenior IT ConsultantCommented:
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.

sp70Author Commented:
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.
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

Mohamed OsamaConnect With a Mentor Senior IT ConsultantCommented:
>>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 
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.
exec Command

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.
Mohamed OsamaSenior IT ConsultantCommented:
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.

sp70Author Commented:
Thank you both for wonderful solutions.

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

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!
Mohamed OsamaSenior IT ConsultantCommented:
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.

sp70Author Commented:
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.
Mohamed OsamaSenior IT ConsultantCommented:
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.