Skip to content

Instantly share code, notes, and snippets.

@wsmelton
Last active February 8, 2019 21:38
Show Gist options
  • Save wsmelton/b8eb5a4f88e02e4702964305ab61fb4d to your computer and use it in GitHub Desktop.
Save wsmelton/b8eb5a4f88e02e4702964305ab61fb4d to your computer and use it in GitHub Desktop.
General script used in projects that allows you to validate an environment.

Purpose

On average I'm working with Docker for Windows on my machine when I need to analyze some data or say build reports from collected metric data of a given client. The environment.ps1 script provided simply does some validates for me and ensures everything is in order.

No Pester

I'm not using Pester for the testing of the environment because on average I don't have machines in client environments with that installed. Just making it more universal for right now but if you want to take it and go with using Pester...by all means.

Check for Docker

Without having to install other tools or PowerShell modules I'm just doing a string match for version 18 of the docker engine that comes with Docker for Windows.

dbatools always

The remainder of this script checks that dbatools is installed. I also hard code the version I'm coding against because to many changes can occur with the module and I'd prefer my scripts are stable. I leave the option up to the user if they want to continue using a higher or lower version of the module. I code against a given version of dbatools and only support that version in a given script/project until I've tested it on a higher version.

version: '3.7'
services:
db:
container_name: 'perfdata-container'
image:
mcr.microsoft.com/mssql/server:2017-latest
environment:
- ACCEPT_EULA=Y
- SA_PASSWORD=P@ssword12345
ports:
- '1420:1433'
volumes:
- C:\container\performancereport:/var/opt/mssql/data
[CmdletBinding()]
param (
[string]$SqlServer = 'localhost,1420',
[PSCredential]$SqlCred
)
#region ContainerDetails
$dockerFound = $false
if ( (docker version | Select-String "Version" | Select-Object -First 1 | ForEach-Object {$_ -match "18"}) ) {
Write-Output "Docker Engine 18 found"
$dockerFound = $true
}
if ($dockerFound) {
Write-Output "Rebuilding docker container"
[string]$ContainerLocalDir = 'C:\container\performancereport'
$pwd = "P@ssword12345" | ConvertTo-SecureString -AsPlainText -Force
$SqlCred = New-Object System.Management.Automation.PSCredential -ArgumentList 'sa', $pwd
$PSDefaultParameterValues = @{
"dbatools*:SqlInstance" = $SqlServer
"dbatools*:SqlCredential" = $SqlCred
}
<# Need to remove container directory prior to startup #>
if (Test-Path $containerLocalDir) {
Remove-Item $containerLocalDir\* -Force
}
else {
New-Item -Path $containerLocalDir -ItemType Directory -Force > $null
}
try {
docker-compose -f "docker-compose.yml" down
}
catch {
Write-Output "Issue with docker-compose down call"
}
try {
docker-compose -f "docker-compose.yml" up -d --build
}
catch {
Write-Output "Issue with docker-compose up call"
}
}
else {
Write-Output "Docker was not found, need to collect some user inputs"
$global::SqlInstance = Read-Host "Please enter SQL Server instance to host database"
}
$verSupported = "0.9.754"
if ($m = Get-Module dbatools) {
if ($m.Version -ne $verSupported) {
Write-Warning "An unsupported version of dbatools [$verSupported] found in use."
}
}
$m = Get-Module dbatools -ListAvailable
if ( -not $m ) {
throw "dbatools not found installed on this machine"
}
else {
if ($verSupported -notin $m.Version) {
Write-Output "dbatools $verSupported not found!"
}
else {
Remove-Module dbatools -ErrorAction SilentlyContinue
Import-Module dbatools -RequiredVersion $verSupported
Write-Output "dbatools $verSupported loaded"
}
}
if ($dockerFound) {
$count = 5
Write-Output "Testing connection to container instance! `nPausing for $count seconds to make sure SQL Server is accepting connections within the container"
Start-Sleep -Seconds $count
$original = Get-DbatoolsConfig -FullName sql.connection.timeout
Set-DbatoolsConfig -FullName sql.connection.timeout -Value 3
$testCn = Test-DbaConnection -SqlInstance $SqlServer -SqlCredential $SqlCred
Set-DbatoolsConfig -FullName sql.connection.timeout -Value $original.Value
if (-not $testCn.ConnectSuccess) {
throw "Testing connection to container failed"
}
else {
Write-Output "Connection to container instance successful!`nEnvironment is ready to go!"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment