Automating deploy for services

Hi there,

I’m creating a powershell script to automate deploying windows service applications. So, what we usually do is stop the service through services.msc, create a backup version inside a folder named by current date, copy files to there, replace the service files with the new version and, finally, we start the service again.

I created the script below step-by-step and it seems fine, BUT, I tested running from a different location than original one and then, I got unexpected results… It changed the local files from where I was executing.

My plan is to use this script for deploying service at least from homologation environment and maybe production as well (do you guys consider any risk using as it is?)

Could anyone give me a feedback about source code it?

$serviceName = 'XblGameSave'
$today = get-date -Format "yyyyMMdd"
$appBackupPath = "c:\projects\app\backup\" + $today 
$appFiles = "c:\projects\app"
$appNewVersion = 'c:\projects\nova_versao'
$excludeFiles = 'no.txt','teste'

get-service $serviceName 
stop-service $serviceName 

mkdir $appBackupPath

$filesToCopy = get-childitem -name -Exclude $excludeFiles

copy -path $filesToCopy $appBackupPath

$novaVersao = Get-ChildItem -name appNewVersion

get-childitem -Exclude $excludeFiles | rename-item -NewName { $_.Name -replace '([\w ]+)','$1_old'}

$novaVersao | copy -destination $appFiles

start-service $serviceName 

Get-ChildItem has a parameter -Path, if not specified, it will consider the default value which is the current location. So you can specify the required path using -Path parameter.