Automatic Software Installer for Cloud & DevOps Engineers on Windows

Automatic Software Installer for Cloud & DevOps Engineers on Windows Lately I’ve been putting a lot of focus on automating as much as possible. Following this line, I came up with the idea of simplifying the process of onboarding and initia

Source comparison and review notes
  • Automated pair-review warning (AI bilingual content review): Both copies describe $psModules as installed, but the shared install-pwsh-modules implementation only enters its install/update block when Get-Module already finds the module; a missing module is not installed by that code path.

Automatic Software Installer for Cloud & DevOps Engineers on Windows

Lately I’ve been putting a lot of focus on automating as much as possible. Following this line, I came up with the idea of simplifying the process of onboarding and initial configuration of a new desktop or laptop, automating the installation of all the essential tools that Cloud Engineers and DevOps Engineers use every day.

In this case, I developed the script in PowerShell using some native commands, and I also used the very powerful Chocolatey. First let’s introduce the script a little bit, then let’s talk about this tool.

Link to the GitHub repository:

https://github.com/mdiloreto/windows-desktop-devopstools-install-script

In short:

In summary, this script:

  • You will install the software listing specified in the $chocoSoftwareList variable using Chocolatey.
  • You’ll install WSL and install the distribution specified in the $distribution variable without initializing it.
  • You’ll install PowerShell7.
  • You’ll install the PowerShell modules specified in the $psModules variable.
  • You’ll install the Gcloud SDK by initializing the installer (since the Chocolatey package is outdated).

Script Components:

Below is a high-level description of the script’s components.

What software does the script install?

In principle, the script is set to install the following programs/tools:

  • Development Tools: git, docker-desktop, visualstudiocode, terraform, kubernetes-cli, python
  • Cloud CLI Tools: azure-cli, awscli, gcloud SDK, azurestorageexplorer
  • Database Tools: mysql.workbench
  • Remote Access Tools: anydesk, mremoteng
  • API Testing Tools: postman
  • Others: bitwarden, firefox, snagit

Installed packets can be modified using the list $chocoSoftwareList

This gives us a small hint that Packet Manager will be using… Yes, we will be using Chocolatey.

What is Chocolatey?

It’s an Open Source Packet Manager for Windows. It’s based on PowerShell and unattended installations. It’s similar to apt-get or yum on Linux. It allows us to manage software installation packages quickly and automatically on Windows.

It uses software packages packaged in .nupkg formats, which are software builds that include everything needed to install an application. It currently has 10035 Packages maintained by the community.

In this script we can modify/add/remove the software that is installed by modifying the variable named above $chocoSoftwareList:

PowerShell
# List of software to install with Chocolatey
$chocoSoftwareList = @(
"git",
"gh",
"docker-desktop",
"MicrosoftAzureStorageExplorer",
"azure-cli",
#"gcloudsdk", ### Package out of date 20-2-2024
"awscli",
"visualstudiocode",
"terraform",
"python",
"kubernetes-cli",
"mysql.workbench",
"mremoteng",
"postman",
"anydesk",
"snagit",
"bitwarden",
#"googlechrome", ### Package out of date 20-2-2024
"firefox"
)

WSL Installation

The script will also install and initialize the Windows Subsystem for Linux in this section of the code:

PowerShell
function install-wsl {
param (
[string[]]$distribution
)
try {
# Enable WSL and Install distribution
$wslInstalled = Get-WindowsOptionalFeature -FeatureName Microsoft-Windows-Subsystem-Linux -Online
if ($wslInstalled.State -ne "Enabled") {
Write-Host "Enabling WSL..."
Enable-WindowsOptionalFeature -FeatureName Microsoft-Windows-Subsystem-Linux -Online -NoRestart
Write-Host "Enabling $distribution distribution in WSL with no-launch..." -ForegroundColor DarkRed
wsl --install -d $distribution --no-launch
} else {
Write-Host "WSL is already enabled."
}
} catch {
Write-Error "Failed to enable or check WSL: $($_.Exception.Message)"
}
}

What is WSL?

WSL, or Windows Subsystem for Linux, is a feature of Windows 10 and Windows 11 that allows users to run a Linux environment directly on Windows, without the need for a virtual machine or double boot.

WSL has two main versions: WSL 1 and WSL 2. WSL 1 implements a Linux system call translator in the Windows kernel, allowing Linux binaries to run. WSL 2, on the other hand, uses lightweight virtualization to run a true Linux kernel, providing greater compatibility and performance for Linux applications on Windows.

In this case we implemented wsl2.

Installing PowerShell 7 and Modules

The script will install PowerShell 7. Additionally, it will install the modules specified in the $psModules variable.

PowerShell
List of the Pwsh Modules to install
$psModules = @(
"Az",
"AzureAD", # For Azure Active Directory
"MSOnline", # For Microsoft 365
"ExchangeOnlineManagement"
)

The Script

The script is located in the following repo:

https://github.com/mdiloreto/windows-desktop-devopstools-install-script

PowerShell
function RunAsAdmin {
try {
# Ensure the script is run as an Administrator
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Warning "Please run this script as an Administrator!"
Exit
}
} catch {
Write-Error "Failed to validate administrative privileges: $($_.Exception.Message)"
}
}
function install-choco {
try {
If (-Not (Get-Command choco -ErrorAction SilentlyContinue)) {
Write-Host "Installing Chocolatey..."
Set-ExecutionPolicy Bypass -Scope Process -Force
Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
}
} catch {
Write-Error "Chocolatey installation failed: $($_.Exception.Message)"
}
}
function install-choco-soft {
param (
[string[]]$chocoSoftwareList,
[bool]$update
)
foreach ($software in $chocoSoftwareList) {
try {
Write-Host "Checking and installing $software with Chocolatey..."
choco install $software -y
if ($update) {
choco upgrade $software -y
}
} catch {
Write-Error "Failed to install/upgrade $software : $($_.Exception.Message)"
}
}
if ($update) {
try {
Write-Host "Upgrading Chocolatey and all its packages..."
choco upgrade chocolatey -y
choco upgrade all -y
} catch {
Write-Error "Failed to upgrade Chocolatey and all packages: $($_.Exception.Message)"
}
}
}
function install-pwsh7 {
try {
Write-Host "Searching for the latest version of PowerShell using Winget..."
winget search Microsoft.PowerShell
Write-Host "Installing the latest stable version of PowerShell..."
winget install --id Microsoft.Powershell --source winget --accept-package-agreements --accept-source-agreements
winget install --id Microsoft.Powershell.Preview --source winget --accept-package-agreements --accept-source-agreements
} catch {
Write-Error "Failed to install PowerShell 7: $($_.Exception.Message)"
}
}
function install-wsl {
param (
[string[]]$distribution
)
try {
# Enable WSL and Install distribution
$wslInstalled = Get-WindowsOptionalFeature -FeatureName Microsoft-Windows-Subsystem-Linux -Online
if ($wslInstalled.State -ne "Enabled") {
Write-Host "Enabling WSL..."
Enable-WindowsOptionalFeature -FeatureName Microsoft-Windows-Subsystem-Linux -Online -NoRestart
Write-Host "Enabling $distribution distribution in WSL with no-launch..." -ForegroundColor DarkRed
wsl --install -d $distribution --no-launch
} else {
Write-Host "WSL is already enabled."
}
} catch {
Write-Error "Failed to enable or check WSL: $($_.Exception.Message)"
}
}
function install-gcloudsdk-manual {
try {
Get-Command gcloud
} catch {
Write-Host "Downloading Google Cloud SDK..." -ForegroundColor Yellow
(New-Object Net.WebClient).DownloadFile("https://dl.google.com/dl/cloudsdk/channels/rapid/GoogleCloudSDKInstaller.exe", "$env:Temp\GoogleCloudSDKInstaller.exe")
Write-Host "Installing Google Cloud SDK..." -ForegroundColor Blue
Write-Host "The installer will pop out in a second..." -ForegroundColor DarkRed
& $env:Temp\GoogleCloudSDKInstaller.exe
}
}
function install-pwsh-modules {
param (
[string[]]$psModules
)
Write-Host "Managing PowerShell Modules..." -ForegroundColor Yellow
foreach ($module in $psModules) {
$installedModule = Get-Module -ListAvailable -Name $module
if ($installedModule) {
try {
Write-Host "Updating PowerShell module: $module"
Update-Module -Name $module -Force -ErrorAction Stop
} catch {
Write-Host "Encountered an issue updating $module. Attempting to install..." -ForegroundColor Yellow
try {
Install-Module -Name $module -AllowClobber -Repository PSGallery -Force -SkipPublisherCheck -ErrorAction Stop
Write-Host "$module installed successfully." -ForegroundColor Green
} catch {
Write-Error "Failed to install $module. Error: $_"
}
}
}
}
Write-Host "PowerShell Modules management successful." -ForegroundColor Green
}
###### SET VARIABLES ########################################
# List of the Pwsh Modules to install
$psModules = @(
"Az",
"AzureAD", # For Azure Active Directory
"MSOnline", # For Microsoft 365
"ExchangeOnlineManagement"
)
# List of software to install with Chocolatey
$chocoSoftwareList = @(
"git",
"gh",
"docker-desktop",
"microsoftazurestorageexplorer"
"azure-cli",
#"gcloudsdk", ### Package out of date 20-2-2024
"awscli",
"visualstudiocode",
"terraform",
"python",
"kubernetes-cli",
"mysql.workbench",
"mremoteng",
"postman",
"anydesk",
"snagit",
"bitwarden",
#"googlechrome", ### Package out of date 20-2-2024
"firefox"
)
$distribution = "Ubuntu"
################### Main Script Execution Block ####################
try {
# 1. Step: Force script to run with elevated Administrator rights
RunAsAdmin
# 2. Step: Chocolatey Installation
install-choco
# 3. Set: Chocolatey-Powered Software Installation and Possible Upgrading
install-choco-soft -chocoSoftwareList $chocoSoftwareList -update $true
# 3.b Installing Gcloud SDK "manually" cause the chocolatly module is out of date.
install-gcloudsdk-manual
# 4. Installing or Checking for PowerShell 7 via Winget
install-pwsh7
# 5. Bootstrap: This will be determined if WSL installation is based on a name of the specific choice of OS
install-wsl -distribution $distribution
# 6. Operate: Manage Windows and PowerShell's Requisite Leading-Edge Commands (Modules)
install-pwsh-modules -psModules $psModules
} catch {
Write-Error "An unexpected event fatally halted the code with error: $($_.Exception.Message)"
exit 1
} finally {
Write-Host "******* Script run finished. Review the results above. For some software tools to be fully installed you will need to restart your PC...*******" -ForegroundColor Cyan
}

Thanks for reading. The learning continues in the next article.