Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
function ConvertTo-MtCommandParameterValue {
<#
.SYNOPSIS
Renders one bound parameter value for the recorded Invoke-Maester command string.

.DESCRIPTION
Strings are quoted and value types are written as they are. Anything else is rendered as
its type name in angle brackets, never its content: interpolating an arbitrary object calls
that object's own ToString(), and a type supplied by the caller can use that to place its
contents - a connection string, an API key, a token - into a command string that is stored
in the result file and shown in the report.

A $null element inside an array renders as an empty string, matching what the previous
inline join produced.

.EXAMPLE
ConvertTo-MtCommandParameterValue -Value 'Entra'

Returns 'Entra' in quotes.

.EXAMPLE
ConvertTo-MtCommandParameterValue -Value $config

Returns <PSCustomObject> rather than whatever $config says about itself.
#>
[CmdletBinding()]
[OutputType([string])]
param(
[Parameter()]
[AllowNull()]
[object] $Value
)

if ($null -eq $Value) { return '' }
if ($Value -is [string]) { return "'$Value'" }
if ($Value -is [ValueType]) { return "$Value" }

if ($Value -is [array]) {
$items = @($Value | ForEach-Object {
if ($null -eq $_) { '' }
elseif ($_ -is [string] -or $_ -is [ValueType]) { "$_" }
else { "<$($_.GetType().Name)>" }
})
return "@('$($items -join "', '")')"
}

return "<$($Value.GetType().Name)>"
}
12 changes: 6 additions & 6 deletions powershell/public/run/Invoke-Maester.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,11 @@
Set-MtConsolePhase 'Results'
Write-MtProgress -Activity 'Processing test results' -Status "$(@($pesterResults.Tests).Count + $nativeRows.Count) test(s)" -Force

# Build the Invoke-Maester command string from bound parameters
# Build the Invoke-Maester command string from bound parameters.
# Only strings and value types are written out. Any other object is recorded as its
# type name, because interpolating it calls its own ToString(), and a caller-supplied
# type can use that to place its contents - a connection string, a token - into a
# command string that is then stored in the result and the report.
$invokeMaesterCommand = "Invoke-Maester"
foreach ($param in $PSBoundParameters.GetEnumerator()) {
$paramName = $param.Key
Expand All @@ -1026,12 +1030,8 @@
if ($paramValue.IsPresent) {
$invokeMaesterCommand += " -$paramName"
}
} elseif ($paramValue -is [array]) {
$invokeMaesterCommand += " -$paramName @('$($paramValue -join "', '")')"
} elseif ($paramValue -is [string]) {
$invokeMaesterCommand += " -$paramName '$paramValue'"
} elseif ($null -ne $paramValue) {
$invokeMaesterCommand += " -$paramName $paramValue"
$invokeMaesterCommand += " -$paramName $(ConvertTo-MtCommandParameterValue -Value $paramValue)"
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
BeforeAll {
Import-Module $PSScriptRoot/../../Maester.psd1 -Force
. "$PSScriptRoot/../helpers/Use-MtModuleFunction.ps1"
Use-MtModuleFunction -Name ConvertTo-MtCommandParameterValue

class MtLeakyParameter {
[string] $ConnectionString = 'Server=db;Password=hunter2'
[string] ToString() { return "Config($($this.ConnectionString))" }
}
}

Describe 'ConvertTo-MtCommandParameterValue' {
It 'Quotes a string' {
ConvertTo-MtCommandParameterValue -Value 'Entra' | Should -BeExactly "'Entra'"
}

It 'Writes a <Kind> as it is' -ForEach @(
@{ Kind = 'number'; Value = 42; Expected = '42' }
@{ Kind = 'boolean'; Value = $true; Expected = 'True' }
) {
ConvertTo-MtCommandParameterValue -Value $Value | Should -BeExactly $Expected
}

It 'Renders an arbitrary object as its type name, never its ToString()' {
# The recorded command is written to the result file and shown in the report. Interpolating
# an object calls its own ToString(), so a caller-supplied type could place a secret there.
$result = ConvertTo-MtCommandParameterValue -Value ([MtLeakyParameter]::new())

$result | Should -BeExactly '<MtLeakyParameter>'
$result | Should -Not -Match 'hunter2'
$result | Should -Not -Match 'ConnectionString'
}

It 'Keeps the array form for a string array' {
ConvertTo-MtCommandParameterValue -Value @('Entra', 'Teams') | Should -BeExactly "@('Entra', 'Teams')"
}

It 'Renders an object inside an array as its type name too' {
$result = ConvertTo-MtCommandParameterValue -Value @('Entra', [MtLeakyParameter]::new())

$result | Should -BeExactly "@('Entra', '<MtLeakyParameter>')"
$result | Should -Not -Match 'hunter2'
}

It 'Returns an empty string for null' {
ConvertTo-MtCommandParameterValue -Value $null | Should -BeExactly ''
}

It 'Renders a null element inside an array as an empty string' {
ConvertTo-MtCommandParameterValue -Value @('Entra', $null) | Should -BeExactly "@('Entra', '')"
}
}
Loading