Private/ConvertFrom-PlaceholderString.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
Function ConvertFrom-PlaceholderString {
    [cmdletbinding()]
    param(
        [parameter(Mandatory, ValueFromPipeline)]
        [string[]]$InputObject,

        [parameter(Mandatory)]
        [ValidateScript({ $_.PSObject.TypeNames[0] -eq 'OperationValidationResult' })]
        [pscustomobject]$Results
    )

    begin {
        Write-Debug -Message "Entering: $($PSCmdlet.MyInvocation.MyCommand.Name)"
        Assert-InWatchmen -Command $PSCmdlet.MyInvocation.MyCommand.Name
    }

    process {

        foreach($string in $InputObject) {
            # replace any variables
            $string = $string.Replace('#{computername}', $env:COMPUTERNAME)
            $string = $string.Replace('#{module}', $results.module)
            $string = $string.Replace('#{file}', $results.FileName)
            $string = $string.Replace('#{describe}', $results.RawResult.Describe)
            $string = $string.Replace('#{context}', $results.RawResult.Context)
            $string = $string.Replace('#{test}', $results.RawResult.Name)
            $string = $string.Replace('#{result}', $results.Result)
            $string = $string.Replace('#{failureMessage}', $results.RawResult.FailureMessage)
            $string = $string.Replace('#{duration}', $results.RawResult.Time.ToString())

            return $string
        }
    }

    end {
        Write-Debug -Message "Exiting: $($PSCmdlet.MyInvocation.MyCommand.Name)"
    }
}