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.
