batch – Proxmedia https://pxm-software.com Software Outsourcing & Nearshore Development Mon, 27 Feb 2017 14:01:08 +0000 en-US hourly 1 https://wordpress.org/?v=4.7.2 Using Power-Shell in automated tasks (batch script alternative) https://pxm-software.com/using-power-shell-in-automated-tasks-batch-script-alternative/ Sun, 28 Jun 2015 20:16:10 +0000 https://proxmedia.pl:99/en/?p=4136 Power Shell it’s the Microsoft’s Automation framework sitting on top of .Net (System.Management.Automation) that lets us perform advanced automated tasks. It can help not only the administrators but also the programmers to save a lot of time by automating repeatable processes. The script’s syntax is quite intuitive and easy to learn so it allows users […]

Artykuł Using Power-Shell in automated tasks (batch script alternative) pochodzi z serwisu Proxmedia.

]]>
Power Shell it’s the Microsoft’s Automation framework sitting on top of .Net (System.Management.Automation) that lets us perform advanced automated tasks. It can help not only the administrators but also the programmers to save a lot of time by automating repeatable processes. The script’s syntax is quite intuitive and easy to learn so it allows users already writing e.g. batch scripts to grasp it very fast.

If we have for example an old batch file that executes sql scripts stored in the folder as follows:

 @ECHO OFF
 echo script folder: %1
 echo server: %2
 echo database: %3
 echo database user: %4
 echo ----------------

setlocal enabledelayedexpansion
cd /d %1

for %%d in (*.sql) do (
   set filecontent=
   for /f "delims=" %%a in (%%d) do (set "filecontent=!filecontent!%%a")
   set "temp=!filecontent!"
   set "filecontent=!temp:#PARAM#=%4!"
   echo executing %%d
   echo ----------
   sqlcmd -S%2 -E -Q"!filecontent!" -d%3 
)
pause

then the above script can be easily replaced with Power-Shell. Using PS gives us the same flexibility of scripting languages and at the same time allows us to interact with .Net framework when required.

Before we can start executing PS scripts on our computer, we need to enable execution which is disabled by default. We can do it using following cmdlets:

 //enable executing 
 Set-ExecutionPolicy RemoteSigned

 //get back to default settings
 Set-ExecutionPolicy Default

After we’ve created test.ps1 file, we can start implementation. In order to make testing easier we can define default params inside the script using param(…) constructor. Please note that by using [string] keyword we actually creating strongly typed objects. Defined parameters will be overwritten by the user’s passed-in params.

ps-script

The first command (dir $ScriptFolder) just lists all files within the directory, then ForEach-Object function iterates through all files getting their content into $query variable. Next the $query text is being replaced with our parameter if we have any. The final task is just to execute SQLCMD command passing in query and database connection settings.

param (
[string]$ScriptFolder = "Database\Procedures",
[string]$Server = ".\SQL2008R2",
[string]$Database = "MyDb",
[string]$DatabaseUser = "user"
)


(dir $ScriptFolder) | 
  ForEach-Object { 
    $query = get-content $_.FullName -Raw
    $query = $query.Replace("#PARAM#","user");
    SQLCMD -S $Server -E -Q $query -d $Database
   }

In order to run our Power-Shell script we need just execute following line:

 //command line
 powershell -noexit C:\test.ps1 -ScriptFolder "C:\Database\Procedures" -Server ".\SQL2008R2" -Database "MyDB" -DatabaseUser "user"

Good luck with your Power-Shell scripting 🙂

Artykuł Using Power-Shell in automated tasks (batch script alternative) pochodzi z serwisu Proxmedia.

]]>
Auto creating SVN tags and custom release installers https://pxm-software.com/auto-creating-svn-tags-and-custom-release-installers/ Thu, 12 Mar 2015 13:06:04 +0000 https://proxmedia.pl:99/en/?p=3941 Automation within the software development company allows to save tremendous amount of time in the long run. The time spend for initial configuration of the automated tasks is almost nothing compared to running all processes manually. In this article I will show you how to configure auto SVN tagging and creating custom installation files each […]

Artykuł Auto creating SVN tags and custom release installers pochodzi z serwisu Proxmedia.

]]>
Automation within the software development company allows to save tremendous amount of time in the long run. The time spend for initial configuration of the automated tasks is almost nothing compared to running all processes manually. In this article I will show you how to configure auto SVN tagging and creating custom installation files each time after the TeamCity build is performed.

In our scenario we want to copy our current branch files to the https://svn.mycompany.com/Releases/Tags/ReleaseVersion location and from that location we want to create installation file that we want to deploy on the client server.

The first step is to create new TeamCity build configuration and as a first build step define following configuration:

svn_tags

Let’s create our svn-create-TAGS.bat file. The implementation is quite simple. There are 3 parameters that we will pass in from the TeamCity step: ReleaseVersion (%1), SVNRevision (%2), BranchSVNRoot (%3). Based on that we will trigger script below.

Because subversion copy function doesn’t have replace functionality that we can use, we then decide to delete current tag if exists. If the tag doesn’t exist then we will get svn error that could be passed to TeamCity Log. In our case we will use -q param to not display any error messages as this script will be run multiple times so each time the tags will have to replaced if the same release version is used.

After running this script we will have following tag created eg. https://svn.mycompany.com/Releases/Tags/Release.2.8.0.3″

 echo version %1
 echo revision %2
 echo branch path %3

  echo delete first if exists as there is no overwrite function for svn copy
  "C:\Program Files\TortoiseSVN\bin\svn.exe" delete "https://svn.mycompany.com/Releases/Tags/Release.%1" -q 
    -m "TeamCity generated: Deleting TAG branch for the release: %1" 

 echo copy to tag
 "C:\Program Files\TortoiseSVN\bin\svn.exe" copy -r%2 "%3/MyMainProjectFolder" "https://svn.mycompany.com/Releases/Tags/Release.%1" 
   -m "TeamCity generated: Creating TAG branch for the release: %1"

We also need to configure TeamCity parameters as follows:
svntags_params

When we have release tag created, we can now make release installer that can be deployed to the client. To do that we will define new build step (configuration is similar as the step above – script name and params will only be different).

Let’s create Installer_build.bat script then. We will pass in 2 parameters ReleaseVersion (%1), SVNRevision (%2). The script will create folder structure in Windows Temp location that we will use to import and compress our release files to. After we done with it we will clear that location and move executable file to our final release file location.

The script is basically importing files from the tag we created earlier, then the files are being compressed using 7za.exe command line utility. We can re-build the project in between but in our case project was built already by the other TeamCity process before it was moved to the release tag. After creating zip package we can build custom installer based on that file. This depends on your custom solution. In our example we will simply output the final zip file to be deployed to the client. Please see in-line comments for detailed information:

 echo params
 echo version %1
 echo revision %2

 echo top level folder stucture
 set root=%TEMP%\MyProjectBuild
 set export="%root%\Export"
 set build=%root%\Build
 set installer=%root%\Installer

 echo make sure nothing already exists
 rmdir /s /q "%root%"

 mkdir "%root%"
 mkdir "%build%"

 echo Create the installer
 echo %installer%
 mkdir %installer%

 svn export -r%2 "https://svn.mycompany.com/Releases/Tags/Release.%1/" "%export%"

 echo you may build you project here or proceed
 set MSBUILD_PATH=C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe

 echo Copy files ready to be compressed
 set InstallerFiles=%build%

 echo copy from export filder to the final build folder
 xcopy "%export%\*.*" "%InstallerFiles%\" /E /Y

 echo set tools location that contains your compressor file, set path the same as the executing batch file
 set Tools="%~dp0"
 set BuildZip=%installer%\MyProject.%1.zip

 echo current path %Tools%

 echo build your custom installer or export just zip file
 set FinalExecutable=%BuildZip%
 %Tools%\7za.exe a -tzip -r "%BuildZip%" "%build%\*.*

 echo copy it to the releases location and clear up
 set Releases=C:\Installers

 copy "%installer%\%FinalExecutable%" "%Releases%\%FinalExecutable%"

 if exist "%Releases%\%FinalExecutable%" (
	rmdir /s /q "%root%"
	echo The build is complete.
 ) else (
	echo Unable to copy to final destination. The installer is here: %installer%\%FinalExecutable%
 )

After creating above TeamCity build steps we can configure the release installer build to be triggered on successful main project build.

svntag_build_trigger

In order to get above scripts to work you need to adjust it to your requirements as I took it from the running solution I have created and for obvious reasons had to change the paths and names.
I hope this will help you to be more productive in your daily programming 🙂

Artykuł Auto creating SVN tags and custom release installers pochodzi z serwisu Proxmedia.

]]>
Executing sql scripts in direcory using .bat file https://pxm-software.com/executing-sql-scripts-in-direcory-using-bat-file/ Mon, 09 Feb 2015 09:56:52 +0000 https://proxmedia.pl:99/en/?p=3894 If you are in situation where you have to execute all sql scripts located as separate files in one folder, executing .bat file is the one of the options you can use. Executing batch command is quite flexible solution as you don’t have to recompile it every time the script will change. The code below […]

Artykuł Executing sql scripts in direcory using .bat file pochodzi z serwisu Proxmedia.

]]>
If you are in situation where you have to execute all sql scripts located as separate files in one folder, executing .bat file is the one of the options you can use. Executing batch command is quite flexible solution as you don’t have to recompile it every time the script will change.

The code below loops through all files within the specified directory, loads each of them into variable and replaces the param string if exists before execution. It is helpful if you need to replace database specific users (e.g. execute with ‘username’) etc.

Please note that variables within the loop are accessed by !myVar! instead of %myVar% because of delayed expansion applied.

Command cd /d %1 sets the current directory to the specified by the user in first parameter.

 @ECHO OFF
 echo script folder: %1
 echo server: %2
 echo database: %3
 echo database user: %4
 echo ----------------

setlocal enabledelayedexpansion
cd /d %1

for %%d in (*.sql) do (
   set filecontent=
   for /f "delims=" %%a in (%%d) do (set "filecontent=!filecontent!%%a")

   set "temp=!filecontent!"

   set "filecontent=!temp:#PARAM#=%4!"

   echo executing %%d
   echo ----------
   sqlcmd -S%2 -E -Q"!filecontent!" -d%3 
)
pause

Above code can be executed as follows (trusted database connection is used)

 C:\updateDB.bat "C:\scripts\" "myServer" "databaseName" PARAM_TO_REPLACE

Artykuł Executing sql scripts in direcory using .bat file pochodzi z serwisu Proxmedia.

]]>