PoshCode Archive  Artifact [9cd8355a4f]

Artifact 9cd8355a4fc56f79549d13f221711317fabdfe66540c6d5a6d1eceb07ea8e37b:

  • File Get-FormatData.ps1 — part of check-in [90c25f0b77] at 2018-06-10 14:10:07 on branch trunk — A wrapper around Get-FormatData that allows you to pipe types or objects into it. e.g. (user: Public Domain size: 2457)

# encoding: ascii
# api: powershell
# title: Get-FormatData
# description: A wrapper around Get-FormatData that allows you to pipe types or objects into it. e.g.
# version: 0.1
# type: function
# author: Public Domain
# license: CC0
# function: Get-FormatData
# x-poshcode-id: 6196
# x-archived: 2016-05-19T09:21:24
# x-published: 2016-01-30T21:53:00
#
# Get-Service | Get-FormatData
# [System.Management.Automation.AliasInfo] | Get-FormatData
#
function Get-FormatData {
	[CmdletBinding(HelpUri='http://go.microsoft.com/fwlink/?LinkID=144303', DefaultParameterSetName='S')]
	param(
		[Parameter(Position=0, ParameterSetName='S')]
		[ValidateNotNullOrEmpty()]
		[string[]]$TypeName = '*'
,
		[Parameter(ValueFromPipeline, ParameterSetName='I')]
		$InputObject
	)
	begin {
		if ($PSCmdlet.ParameterSetName -ceq 'I') {
			$seen = @{}
		} else {
			try {
				$outBuffer = $null
				if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer)) {
					$PSBoundParameters['OutBuffer'] = 1
				}
				$wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Microsoft.PowerShell.Utility\Get-FormatData', [System.Management.Automation.CommandTypes]::Cmdlet)
				$scriptCmd = {& $wrappedCmd @PSBoundParameters }
				$steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
				$steppablePipeline.Begin($PSCmdlet)
			} catch {
				throw
			}
		}
	}
	process {
		if ($PSCmdlet.ParameterSetName -ceq 'I') {
			if ($null -cne $InputObject) {
				$names = [string[]]@()
				if ($InputObject -is [type]) {
					$t = $InputObject
					do {
						if (-not $seen.ContainsKey($t.FullName)) {
							$seen[$t.FullName] = 0
							$names += [System.Management.Automation.WildcardPattern]::Escape($t.FullName)
						} 
						$t = $t.BaseType
					} while ($null -cne $t)
				} else {
					foreach ($n in $InputObject.pstypenames) {
						if (-not $seen.ContainsKey($n)) {
							$seen[$n] = 0
							$names += [System.Management.Automation.WildcardPattern]::Escape($n)
						}
					}
				}
				if (0 -lt $names.Length) {
					Get-FormatData -TypeName $names
				}
			}
		} else {
			try {
				$steppablePipeline.Process($_)
			} catch {
				throw
			}
		}
	}
	end {
		if ($PSCmdlet.ParameterSetName -ceq 'S') {
			try {
				$steppablePipeline.End()
			} catch {
				throw
			}
		}
	}
}
<#
.ForwardHelpTargetName Get-FormatData
.ForwardHelpCategory Cmdlet
#>