Skip to content

Latest commit

 

History

History
25 lines (18 loc) · 1.1 KB

File metadata and controls

25 lines (18 loc) · 1.1 KB

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/