Reading Time: < 1 minute

Let me first tell you a bit about Boxstarter. It leverages Chocolatey packages to automate the installation of software and create repeatable, reboot resilient, scripted Windows environments. Chocolatey makes installing software very easy with no user intervention.

When installing SQL Server on a machine the installer uses DPAPI to encrypt/decrypt data for the installation. Due to the way Boxstarter is currently setup, DPAPI is not available inside a boxstarter script. This also happens when running Ansible over WinRM.

To get around this during Boxstarter I have used a PowerShell module called Invoke-CommandAs. During your Boxstarter script, you need to check if it is installed. If it is, you don’t want to install it again. After you have it installed, you can execute the Chocolatey package commands.

$PSversion = $PSVersionTable.PSVersion.Major
if ($PSversion -lt "5") {
    Write-Warning "  ** PowerShell < v5 detected."
	Write-Warning "  ** This scripts installs software via the PowerShell Gallery and thus requires PowerShell v5+."
	Write-Warning "  ** If PowerShell v5 was installed as a dependency, you need to reboot and reinstall this package."
$invokeCommandAs = Get-Command -Name 'Invoke-CommandAs' -Module 'Invoke-CommandAs' -ErrorAction 'SilentlyContinue'
if ( $null -eq $invokeCommandAs ){
    Get-PackageProvider -Name NuGet -Force
    Install-Module -Name Invoke-CommandAs -Force -Scope AllUsers
Write-Output "Installing SQL Server Express"
Invoke-CommandAs -ScriptBlock { choco upgrade sql-server-express } -ComputerName $env:COMPUTERNAME -AsInteractive $env:USERNAME

I will be using this method in my Boxstarter scripts that I am writing for an upcoming project. As I needed help from one of the Chocolatey core team members, and I could not find anything about this on the internet, I wanted to share this with you before releasing the scripts.