This post is a follow up to "Visual Studio Build Events using PowerShell". I wanted to post a more compelling example of what could be done easily with the Pre/Post Build events in Visual Studio .NET using PowerShell.
When developing Windows Services that are deployed on a remote machine within your development environment it can be very annoying to have to stop and start the service each time you build. The following script when executed within a post build event (read the original article) on a Windows Service project will take the following steps:
- Stop the service on the remote computer
- Wait for the service to stop and remove the files in the remote service root directory (to ensure a fresh deployment each time)
- Copy all the files from the Visual Studio project's Target Directory to the remote service root directory
- Start the service on the remote computer
DeployConfig.xml Fragment
<project targetFileName="Service.exe">
<buildConfig name="Debug">
<user name="default">
<buildEvent name="Post">
<![CDATA[write-Output "Script Started"
write-Output ""
write-Output "Stopping IMM DAM Service on REMOTEMACHINENAME"
write-Output ""
$remoteService = (Get-WmiObject -computer REMOTEMACHINENAME Win32_Service -Filter "Name='Service Title'")
$hideOutput = ($remoteService.StopService() | out-string)
write-host -no "Waiting for 'Service Title' to stop"
while((Test-Path '\\REMOTEMACHINENAME\c$\SERVICEDIR\*.*') -eq $True) {
remove-item -path "\\REMOTEMACHINENAME\c$\SERVICEDIR\*.*" -force -erroraction silentlycontinue
write-host -no "."
}
write-Output ""
write-Output ""
copy-item -path "{targetDir}*.*" -destination "\\REMOTEMACHINENAME\c$\SERVICEDIR" -force
write-Output "Files Copied"
write-Output ""
write-Output "Starting 'Service Title' on REMOTEMACHINENAME"
write-Output ""
$hideOutput = ($remoteService.StartService() | out-string)
write-Output "Script Complete"]]>
</buildEvent>
</user>
</buildConfig>
</project>
