Skip to content

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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

Stars

7 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages