Skip to content

An experiment in creating maintainable PowerShell code using classes, unit tests and a task runner.

License

Notifications You must be signed in to change notification settings

WouterDeKort/MaintainablePowerShellDemo

Repository files navigation

Writing maintainable PowerShell

The goal of this code sample is to research the usage of PowerShell classes, unit tests and a build system.

The following is the original script that I've refactored.

Param(
    [string]$ConnectionString,
    [string]$SqlLogin,
    [string]$SqlPassword
)

$propFile = Get-ChildItem 'sonar.properties' -Recurse
$configContents = Get-Content -Path $propFile.FullName -Raw

$configContents = $configContents -ireplace '#sonar.jdbc.username=', "sonar.jdbc.username=$SqlLogin"
$configContents = $configContents -ireplace '#sonar.jdbc.password=', "sonar.jdbc.password=$SqlPassword"
$configContents = $configContents -ireplace '#sonar.jdbc.url=jdbc:sqlserver://localhost;databaseName=sonar;integratedSecurity=true', "#xxx"
$configContents = $configContents -ireplace '#sonar.jdbc.url=jdbc:sqlserver://localhost;databaseName=sonar', "sonar.jdbc.url=$ConnectionString"

Set-Content -Path $propFile.FullName -Value $configContents

More details are available in this blog post: https://wouterdekort.com/2019/04/07/why-i-dont-like-powershell-the-search-for-maintainable-code/

About

An experiment in creating maintainable PowerShell code using classes, unit tests and a task runner.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published