Commands/Format-OpenPackage.ps1
|
function Format-OpenPackage { <# .SYNOPSIS Formats Open Package .DESCRIPTION Formats Open Packages using any view. #> [CmdletBinding(PositionalBinding=$false)] [Alias('Format-OP', 'fop', 'fOpenPackage')] param( # The name of the view, or a view command or scriptblock [Parameter(Mandatory,Position=0)] [ArgumentCompleter({ param ( $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters ) $typeData = Get-TypeData -TypeName OpenPackage.View if (-not $wordToComplete) { $typeData.Members.Keys } else { $typeData.Members.Keys -match ([Regex]::Escape($wordTocomplete)) } })] [PSObject] $View, # The Any Positional arguments for the view [Parameter(Position=1,ValueFromRemainingArguments)] [Alias('Argument','Arguments','Args')] [PSObject[]] $ArgumentList, # Any input objects. [Parameter(ValueFromPipeline)] [Alias('Package')] [PSObject[]] $InputObject, # Any options or parameters to pass to the View [Alias('Options','Parameter','Parameters')] [Collections.IDictionary] $Option = [Ordered]@{} ) # Gather all input $allInput = @($input) if (-not $allInput -and $InputObject) { $allInput += $InputObject } # Get the typedata that describes Formatters $typeData = Get-TypeData -TypeName OpenPackage.View $commandName = '' # If the view is a string if ($View -is [string]) { # check for a view with that name if ($typeData.Members[$View].Script) { # if one is found, use that $commandName = $View $view = $typeData.Members[$View].Script } else { # Othewise, try to find a Formatter command $ViewCommand = $ExecutionContext.SessionState.InvokeCommand.GetCommand( $View,'Cmdlet,Alias,Function' ) # If we found one, try to use it if ($ViewCommand) { $commandName = $View $View = $ViewCommand } else { # Otherwise, warn that Formatter is unknown Write-Warning "Unknown View $view" # and break out of the loop. return } } } # If the view is not a script or command if ($view -isnot [ScriptBlock] -and $view -isnot [Management.Automation.CommandInfo] ) { # return Write-Error "View must be a Name of view, ScriptBlock, or Command" return } # Get our command metadata. $commandMetaData = if ($view -is [ScriptBlock]) { # If the view a scriptblock # make a temporary function $function:View = $view # get its metadata $ExecutionContext.SessionState.InvokeCommand.GetCommand('view', 'Function') -as [Management.Automation.CommandMetaData] # and remove the temporary function Remove-Item 'function:view' } else { # otherwise, just cast to command metadata $commandName = $view.Name $view -as [Management.Automation.CommandMetaData] } # Once we have commandmetadata, we can find parameter names. $validParameterNames = @( $commandMetaData.Parameters.Keys $commandMetaData.Parameters.Values.Aliases ) # We want to be forgiving with input, so copy the options $commandParameters = [Ordered]@{} + $Option # Check each key foreach ($key in @($commandParameters.Keys)) { # If we have valid parameter names if ($validParameterNames -and # and this isn't one of them, $validParameterNames -notcontains $key ) { # write a warning Write-Warning "Option $key not supported by $($commandName)" # and remove the key. $commandParameters.Remove($key) } } if ($allInput) { if ($ArgumentList) { $allInput | & $view @ArgumentList @commandParameters } else { $allInput | & $view @commandParameters } } else { if ($ArgumentList) { & $view @ArgumentList @commandParameters } else { & $view @commandParameters } } } |