Deploy an application to multiple folders and install it with powershell

So i had to dabble my feet into Powershell this week. Something i need to work a lot more on in the future. So expect to see more scripts popping up soon ๐Ÿ™‚

What i had to do was create a script that would allow me to roll out an application to many folders under a directory. As this particular program does not “scale” quite well on users so we needed to manually scale out many instances of the same service on the same server. Now this was setup on multiple servers as well for redundancy. Now we created a folder on a share with the files needed and the config file.

We named the folder like test_001, test_002 and so on, but needed ย a script that would create the folders, copy the files in, edit the config file and add the 001, 002 and so on to each config file as they needed to identify them self with a certain domain name for the in house built connector that was listening on port 80 and 443. We also needed to install the program as a service and start the service.

 

#Parameters
#Start is for first number in series
#End is for last number in series
#Cluster is for if you have different names for the service, like if you have different services for different websites. Can call them, Prod, test, uat, beta and so on. 
#So name will be like MWS_Prod_001

Param(
   $start,
   $end,
   $cluster
)
#Create Folders
$folders = $start..$end
$MWSfolders= "c:\folderpath\MWS_"+$cluster
$DNS="MWS_"+$cluster+"_"
$Url=$cluster+"_"
New-Item -Path ($folders | %{$MWSfolders+"_"+"{0:D3}" -f $_}) -ItemType Directory

#Copy Files to folders use -Exlude to exclude and folders
$Source = '\\sourceserver\folder\*.*'    
    Get-ChildItem -Path C:\folderpath\ -Include "*mws_*" -Exclude "*Proxy*" -Recurse |ForEach-Object {
 
     $_.FullName
    Copy-Item -Path $Source -Destination $_ -Force -Recurse
}


#Edit MWS Config to update mws name and dns name, Install MWS services 
Get-ChildItem -Path C:\fodlerpath -Include "*config.xml*" -Exclude "*Proxy*" -Recurse | 
	ForEach-Object {
           
           $MwsNumber=$_.DirectoryName.Substring($_.DirectoryName.Length-3)
		   $DNSNumber= $DNS+$MwsNumber
		   $UrlNumber=$Url+$MwsNumber
           (Get-Content $_.FullName) | ForEach-Object {$_ -replace 'MWS0a',$DNSNumber } | Set-Content $_.FullName
           (Get-Content $_.FullName) | ForEach-Object {$_ -replace 'MWS0b',$UrlNumber} | Set-Content $_.FullName
           
           #Install MWS services
           cd $_.DirectoryName
           Start-Process -FilePath  executable.exe -ArgumentList "--config=config.xml --install"          
	}  
 

 

#Start Services
Get-Service -Name "mws*" | Where-Object {$_.Status -eq "Stopped"} | Start-Service

Im hoping this little script can help others out in the future.

Leave a Reply

Your email address will not be published. Required fields are marked *