Behind the PowerShell Pipeline logo

Behind the PowerShell Pipeline

Subscribe
Archives
October 5, 2023

Getting Cultured with String Data

Hello again. In the last article, I began demonstrating techniques and concepts that you could use to centralize string data through localization. You can reference a single external file instead of relying on hard-coded verbose, error, or warning messages in your code. Need to change the text of a verbose message? Don’t edit your code. Edit the data file. I’ve written in the past about the value and importance of separating the data you need to run your code from the code itself. Using localized string data can be part of that process.

Don’t let the localization feature scare you off. I don’t have a need to create non-English strings. Instead, I’m relying on the feature to let me centralize the message data. It just happens to be in English.

In the last article, I demonstrated how to use imported string data in a stand-alone function. I added a property to the output from the previous version of this command.

Import-LocalizedData -BaseDirectory $PSScriptRoot -FileName samplestrings.psd1 -BindingVariable strings
Function Get-PSRuntime {
    [cmdletbinding()]
    Param()

    Write-Verbose ($strings.start -f $MyInvocation.MyCommand)
    Write-Verbose ($strings.get -f $PID)

    $PSProcess = Get-Process -Id $PID

    Write-Verbose $strings.create
    [PSCustomObject]@{
        ProcessID   = $PID
        Path        = $PSProcess.Path
        CommandLine = $PSProcess.CommandLine
        StartTime   = $PSProcess.StartTime
        RunTime     = (Get-Date) - $PSProcess.StartTime
    }

    Write-Verbose ($strings.end -f $MyInvocation.MyCommand)
}
Want to read the full issue?
GitHub Bluesky LinkedIn About Jeff
Powered by Buttondown, the easiest way to start and grow your newsletter.