Tuesday, 31 January 2012 07:56

Deployment script Part II - Combining file location and filename

Written by 
Rate this item
(4 votes)

I have been working on the PowerShell deployment script yesterday night. The script handles files based on a file location and filename. When working with files we need error-handling. I can, for example, tell the script that the MSI needed for installation is C:\Deploy_script\My_BizTalk_application.msi without actually placing it there. Typos also are common errors which have to be handled.

So let’s work with the example above. Let’s tell the script that the MSI can be found in C:\Deploy_script\ and that the name of the MSI is My_BizTalk_Application.msi.

$sourcelocation = “C:\Deploy_script\"
$msi = "My_BizTalk_Application.msi" 

So before doing anything with that file (copy it, start installation, etc.) we need to check if it exists. We can use the cmdlet Test-Path for that. Because I want to check if the file exist I need to combine the file location and filename. This can be done by just placing the variables next to each other like this: $sourcelocation$msi. The code to check the file looks like this:

if (Test-Path $sourcelocation$msi)
   {write-host "The file $sourcelocation$msi exists"}
else
   {write-host "The file $sourcelocation$msi does not exist"} 

Provided that that file exist the result of the code above is:

The file C:\Deploy_script\My_BizTalk_application.msi exists

The code above works because the variable $sourcelocation is declared like “C:\Deploy_script\" and not like “C:\Deploy_script". So if we reset the variable $sourcelocation to "C:\Deploy_script" like this:

$sourcelocation = “C:\Deploy_script" 

And rerun the code above we will get the following result:

The file C:\Deploy_scriptMy_BizTalk_application.msi does not exist

Obviously just combining both variables to combine the file location and filename is not the smartest solution. We do not want tell our users that they must end every file location with a “\”, but rather handle that in the code. That is where the join-path cmdlet comes in. I use the following code to combine the file location and filename:

$locationAndFile = join-path -path $sourcelocation -childpath $msi

If we run the following code:

$sourcelocation = “C:\Deploy_script"
$msi = "My_BizTalk_Application.msi"
$LocationAndFile = join-path -path $sourcelocation -childpath $msi
if (Test-Path $LocationAndFile)
   {write-host "The file $LocationAndFile exists"}
else
   {write-host "The file $LocationAndFile does not exist"} 

We will get the following result:

The file C:\Deploy_script\My_BizTalk_application.msi exists

This is a much more elegant solution.

I will post more problems, ideas and useful code while writing the deployment script. Hopefully I will finish the script somewhere next week and post it to the blog.


Read 9430 times Last modified on Tuesday, 31 January 2012 08:09
Jeroen Hendriks

Jeroen Hendriks started his career in 2005 as a middleware administrator. One of the products he was responsible for was BizTalk Server 2004. Since that time he mainly focusses on designing, implementing and supporting BizTalk Server infrastructures. Currently he works as a consultant for Axon Olympus. He hopes that his day-to-day experiences will result in useful and practical blog posts. His certifications are MCSE, Certified Ethical Hacker, MCITP: Enterprise Administrator, Server Administrator and Database Administrator 2008.

twitterlinkedin

Website: www.axonolympus.nl

5 comments

Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.