Functions/Convert-CmdletToSplat.ps1
function Convert-CmdletToSplat { [CmdletBinding()] param ( [Parameter(Mandatory)] [string] $CommandToProcess, [Parameter()] [switch] $CopyToClipBoard ) function Find-LongestStringLength { [CmdletBinding()] param ( [Parameter(Mandatory)] $Array ) # $Array $StrLen = @() foreach ($a in $Array) { $StrLen += $a.Length } $Return = $StrLen | Sort-Object | Select-Object -Last 1 return $Return } # Ignore the following Parameters $CmdletBindings = @("Verbose", "Debug", "ErrorAction", "WarningAction", "InformationAction", "ErrorVariable", "WarningVariable", "InformationVariable", "OutVariable", "OutBuffer", "PipelineVariable") $CmdLet = $CommandToProcess.Trim().Split(" ")[0] $Verbouwen = @{} $i = 1 $Verbouwen.$i = $CommandToProcess $i++; $Verbouwen.$i = ($Verbouwen.($i - 1)).replace($CmdLet, "") $i++; $Verbouwen.$i = ($Verbouwen.($i - 1)).replace("``", "") # $i++; $Verbouwen.$i = ($Verbouwen.($i - 1)).replace("`n", "") # $i++; $Verbouwen.$i = ($Verbouwen.($i - 1)).replace(" ", "") # $Verbouwen | Format-List $ParamArray = $Verbouwen.$i -Split " -" if ($ParamArray.Count -gt 1) { $Params = @() $ParamArray | ForEach-Object { $p = $_.Trim().split(" ") if ($p) { $obj = [PSCustomObject]@{ Parameter = $p[0] Value = $p[1..50] } $Params += $obj } } # $Params $Output = "`n" if ($Params) { $Output += "`$Params = @{`n" $MaxKeyLength = Find-LongestStringLength ($Params).Parameter $MaxKeyLength $Params | ForEach-Object { if ($_.Value) { $Value = $_.Value } else { $Value = "`$true" } if ($Value -like "*`$*") { $quotes = "" } elseif ($Value -like "*`"*") { $quotes = "" } else { $quotes = "`"" } $Output += " $($_.Parameter)" $FormatFillSpaces = $MaxKeyLength - ($_.Parameter).Length 0..$FormatFillSpaces | ForEach-Object { $output += " " } $Output += "= $($quotes)$($Value)$($quotes)`n" } $Output += "}`n`n" } $Output += "$($CmdLet) @Params`n`n" Write-Host $Output -ForegroundColor Yellow if ($CopyToClipBoard) { $Output | clip } } # Write-Host "##################################################" -ForegroundColor Green $Command = Get-Command $Cmdlet -ea 0 if ($Command) { # $Command $Output = "`n" $Output += "`$Params = @{`n" foreach ($c in $Command) { $MaxKeyLength = Find-LongestStringLength ($c.Parameters).Keys # $MaxKeyLength ($c.Parameters).getenumerator() | ForEach-Object { if ($CmdletBindings -notcontains $_.Key) { $Output += " $($_.Key)" $FormatFillSpaces = $MaxKeyLength - ($_.Key).Length 0..$FormatFillSpaces | ForEach-Object { $output += " " } $Output += "= " if ($_.Value.SwitchParameter) { $Output += "`$true" } elseif ($_.Value.Attributes.ValidValues) { $Output += "`"$(($_.Value.Attributes.ValidValues) -join ",")`"" } else { $Output += "`"`"" } $Output += "`n" } } } $Output += "}`n`n" $Output += "$($CmdLet) @Params`n`n" Write-Host $Output -ForegroundColor Yellow } } $scriptblock = { param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters) $o = Get-Command $wordToComplete* $o.Name | ForEach-Object { if ($_ -match " ") { "'$_'" } else { $_ } } } Register-ArgumentCompleter -CommandName Convert-CmdletToSplat -ParameterName CommandToProcess -ScriptBlock $scriptblock |