diff --git a/powershell/internal/utility/ConvertTo-MtCommandParameterValue.ps1 b/powershell/internal/utility/ConvertTo-MtCommandParameterValue.ps1 new file mode 100644 index 0000000000..394d9da647 --- /dev/null +++ b/powershell/internal/utility/ConvertTo-MtCommandParameterValue.ps1 @@ -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 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)>" +} diff --git a/powershell/public/run/Invoke-Maester.ps1 b/powershell/public/run/Invoke-Maester.ps1 index fa65675ee3..16c4f239df 100644 --- a/powershell/public/run/Invoke-Maester.ps1 +++ b/powershell/public/run/Invoke-Maester.ps1 @@ -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 @@ -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)" } } diff --git a/powershell/tests/functions/ConvertTo-MtCommandParameterValue.Tests.ps1 b/powershell/tests/functions/ConvertTo-MtCommandParameterValue.Tests.ps1 new file mode 100644 index 0000000000..35a51b0f9e --- /dev/null +++ b/powershell/tests/functions/ConvertTo-MtCommandParameterValue.Tests.ps1 @@ -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 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 '' + $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', '')" + $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', '')" + } +}