"Inject" partial scripts

Jun 30, 2014 at 8:19 PM

For the sake of reusability, i would like to have one boxstarter script, include several others, at different locations in the main script.
An example:
#do stuff to join the domain first
cinst xxx
cinst yyy
cinst zzz

#do finalization ie. install updates. 
For now i have solved it by calling
invoke-boxstarter {(new-object net.webclient).DownloadString('http://path/to/partial') | iex}

But i am wundering if this is right approach? or there is a more "Powershelly" way of doing it, perhaps even a boxstarter way... :)

Also if there are any problems using this approach. I am suspecting to that i need to pay close attention to restarts in such a scenario...

Jul 5, 2014 at 8:25 AM
That will certainly work to an extent but there is a better way and it also provides more protections when it comes to restarts. Invoke-Boxstarter is a somewhat low level Boxstarter command that does not provide some of the protections of the higher level Install-BoxstarterPackage command that the documentation tries to steer people to use.

In your case, there are a couple ways you could accomplish the same basic end of your script. Probably the best approach based on your script would be:
Install-BoxstarterPackage -Package http://path/to/partial
If package is a http/s endpoint, Boxstarter will download it and create an on the fly nuget package from the content and then run chocolatey install on the package. Further, Boxstarter will detect pending reboots whenever cinst is called in your script and then restart and resume the script if one is detected.

You could also save the script to a file and pase the file path to the Package parameter or make it a Chocolatey package and pass it the package name. Since a chocolatey package is just a powershell script that also understands chocolatey commands, you can include any cinst calls and any valid powershell and it will be executed by boxstarter.
Jul 29, 2014 at 7:44 AM
Hi, sorry for my late response, vacation hit me :)

just some clarifications

if the included script forces a reboot midway, will boxstarter handle that and only do the remaining of the included script?
Jul 29, 2014 at 8:13 AM
Boxstarter always starts from the beginning of the outermost package when beginning a install. However, given the nature of chocolatey packages, if any of the inner packages have already been installed, they will be skipped. The thing to watch out for is custom powershell scripts that you may sprinkle into the outer package. If thede are not in their own package they will be rerun. Depending on what the script does, this may be perfectly fine but if you want to ensure the script is not rerun, you should place it inside an if block that checks for the presence of a condition under which the script should not run.

For example, if you have a loose script that joins the computer to a domain and then needs a reboot, place the domain join and reboot inside an if block that checks if the computer is already part of the domain.
Jul 29, 2014 at 8:47 AM
ok, kind of figured that ... and got some ideas to how to manage that.

this includes chocolatey packages and cinst with custom sources...
Aug 4, 2014 at 5:58 PM
using the URL way of installing a package and including other packages by install-boxstarterpackage -packagename http://path/to/ps1 does not work.

when installing the depend script, it creates a package with the same name as the first package, which it thinks is already installed


starting install with
in bar.txt i have
install-boxstarterpackage -packagename http://bla/foo/sub_bar.txt
this command creates a temp_BoxstarterPackage which the initial command also does.. and hence the second completes with already installed...

An solution would be to create a chocolatey package and host that on a networkshare (which i think i am going to do)
another solution would be to have boxstarter use the actual filename as a package instead of temp_BoxstarterPackage

the latter solution would create some package with rather odd names, IF the script is hosted as a gist, and that solution isn't very good as well.

Now i'm off to added creator of chocolatey packages to my resumé :D