Created
June 10, 2020 23:30
-
-
Save mabster/27019f04bacd48c76e16754a1b702a2e to your computer and use it in GitHub Desktop.
PowerShell Parameter Madness
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This function takes a user and a course, but it should be possible to specify either argument | |
# as an ID rather than an instance of the class. | |
# Named parameters work fine, but positional parameters don't always parse correctly. | |
# All the commands at the bottom of this file *should* work. | |
class User { [int] $Id } | |
class Course { [int] $Id } | |
function New-Enrolment { | |
[CmdletBinding(SupportsShouldProcess,DefaultParameterSetName='id')] | |
param ( | |
[Parameter(Mandatory,Position=0,ParameterSetName='userid course')] | |
[Parameter(Mandatory,Position=0,ParameterSetName='userid courseid')] | |
[Parameter(Mandatory,Position=0,ParameterSetName='course|userid')] | |
[int] | |
$UserId, | |
[Parameter(Mandatory,Position=0,ParameterSetName='user course')] | |
[Parameter(Mandatory,Position=0,ParameterSetName='user courseid')] | |
[Parameter(Mandatory,Position=0,ParameterSetName='course|user')] | |
[User] | |
$User, | |
[Parameter(Mandatory,ValueFromPipeline,ParameterSetName='user|course')] | |
[Parameter(Mandatory,ValueFromPipeline,ParameterSetName='user|courseid')] | |
[User] | |
$PipelineUser, | |
[Parameter(Mandatory,Position=1,ParameterSetName='userid course')] | |
[Parameter(Mandatory,Position=1,ParameterSetName='user course')] | |
[Parameter(Mandatory,Position=0,ParameterSetName='user|course')] | |
[Course] | |
$Course, | |
[Parameter(Mandatory,Position=1,ParameterSetName='user courseid')] | |
[Parameter(Mandatory,Position=1,ParameterSetName='userid courseid')] | |
[Parameter(Mandatory,Position=0,ParameterSetName='user|courseid')] | |
[int] | |
$CourseId, | |
[Parameter(Mandatory,ValueFromPipeline,ParameterSetName='course|user')] | |
[Parameter(Mandatory,ValueFromPipeline,ParameterSetName='course|userid')] | |
[Course] | |
$PipelineCourse | |
) | |
if ($PipelineCourse) { | |
$CourseId = $PipelineCourse.Id | |
} | |
elseif ($Course) { | |
$CourseId = $Course.Id | |
} | |
if ($PipelineUser) { | |
$UserId = $PipelineUser.Id | |
} | |
elseif ($User) { | |
$UserId = $User.Id | |
} | |
write-output "User $UserId`tCourse $CourseId" | |
} | |
$user = [User] @{ Id = 1 } | |
$course = [Course] @{ Id = 2 } | |
"Named:" | |
"userid courseid" | |
New-Enrolment -UserId $user.Id -CourseId $course.Id | |
"user courseid" | |
New-Enrolment -User $user -CourseId $course.Id | |
"userid course" | |
New-Enrolment -UserId $user.Id -Course $course | |
"user course" | |
New-Enrolment -User $user -Course $course | |
"user|courseid" | |
$user | New-Enrolment -CourseId $course.Id | |
"user|course" | |
$user | New-Enrolment -Course $course | |
"course|user" | |
$course | New-Enrolment -User $user | |
"course|userid" | |
$course | New-Enrolment -UserId $user.id | |
"`nPositional:" | |
"userid courseid" | |
New-Enrolment $user.Id $course.Id | |
"user courseid" | |
New-Enrolment $user $course.Id | |
"userid course" | |
New-Enrolment $user.Id $course | |
"user course" | |
New-Enrolment $user $course | |
"user|courseid" | |
$user | New-Enrolment $course.Id | |
"user|course" | |
$user | New-Enrolment $course | |
"course|user" | |
$course | New-Enrolment $user | |
"course|userid" | |
$course | New-Enrolment $user.id | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've tried it without dedicated "pipeline" parameters too, but still can't get every combination to work.