Skip to content

Instantly share code, notes, and snippets.

@OCram85
Created April 20, 2018 06:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OCram85/6579153ba5bc36505cb70ccd286fbb71 to your computer and use it in GitHub Desktop.
Save OCram85/6579153ba5bc36505cb70ccd286fbb71 to your computer and use it in GitHub Desktop.
PowerShell Advanced Function: Parameter binding by TypeName
function Get-MyAwesomeCustomType {
[CmdletBinding()]
[OutputType('ModuleName.Context.Identifier')]
param()
$returnObj = [PSCustomObject]@{
somePropertyKey = 'awesome value'
foo = 'bar'
}
$returnObj.PsObject.TypeNames.Insert(0, 'ModuleName.Context.Identifier')
Write-Output $returnObj
}
function Invoke-AwesomeStuff {
param(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[PSTypeName('ModuleName.Context.Identifier')]$InputObj
)
Write-Host $InputObj.foo
}
. .\Get-MyAwesomeCustomType.ps1
. .\Invoke-AwesomeStuff.ps1
$foo = Get-MyAwesomeCustomType
Invoke-AwesomeStuff -InputObj $foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment