Skip to content

Instantly share code, notes, and snippets.

@ashdar
Created February 22, 2019 15:19
Show Gist options
  • Save ashdar/a9256d2ea130c48113eeb1fb913d8cea to your computer and use it in GitHub Desktop.
Save ashdar/a9256d2ea130c48113eeb1fb913d8cea to your computer and use it in GitHub Desktop.
I wanted a simple way to demo how pipeline and array input works for a parameter and I thought it might be worth saving.
function foo {
[cmdletbinding()]
param (
[Parameter(Mandatory = $true,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True
)]
[string[]] $DestinationDatabase,
$TestName
)
begin {
$i = [int] 1
}
process {
foreach ($DestinationDb in $DestinationDatabase) {
[pscustomobject] @{ID = $i; DestinationDatabase = $DestinationDb; TestName = $TestName}
$i++
}
}
}
$stringPiped = 'justastring-piped'
$stringPassed = 'justastring-passed'
$arrayPiped = @('bar-array-piped','hello-array-piped','world-array-piped')
$arrayPassed = @('bar-array-passed','hello-array-passed','world-array-passed')
foo -DestinationDatabase $arrayPassed -TestName 'An array of 3 objects, passed as a parameter'
$arrayPiped | foo -TestName 'An array of 3 objects, piped'
foo -DestinationDatabase $stringPassed -TestName 'A simple string, passed as a parameter'
$stringPiped | foo -TestName 'A simple string, piped'
[pscustomobject] @{ID = 99; DestinationDatabase = 'pscustomobjet-NameByPipeline'} | foo -TestName 'A complex object, piped'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment