Author Posts

May 19, 2018 at 9:33 am

I've written a module that uses this little bit of code

$keyValues = @()
$sigParams.GetEnumerator() | Sort-Object Name | ForEach-Object {
    $keyValues += "$($_.Key)$($_.Value)"
    Write-Verbose "$keyValues"
}

$string = $keyValues -join ''

Even though I use $keyValues to create the $string variable PSScriptAnalyzer freaks out and tells me $keyValues is not being used. The only way I've figured out to quiet the rule was to add the 'Write-Verbose "$keyValues"' line.

Is there a better way to code this or should I just ignore PSScriptAnalyzer in this case and move on?

May 19, 2018 at 12:47 pm

Walking away for a couple hours helped. This is what I ended up coming up with. It keeps PSScriptAnalyzer happy and I think it looks cleaner too, so win win.

$keyValues = $sigParams.GetEnumerator() | Sort-Object Name | ForEach-Object {
    "$($_.Key)$($_.Value)"
}

$string = $keyValues -join ''