PSCredential Parameterbinding quirks
See original GitHub issueHi,
I’m trying to create a parameter class that can be directly bound to a PSCredential
parameter.
A simple example use of the objective:
$cred = [DbaCredential]"foo\bar"
Get-WmiObject -ComputerName "computer1" -Credential $cred -Class "Win32_OperatingSystem"
This parameter class is basically a functionality wrapper around the PSCredential object (So I didn’t try to reinvent security. Not good enough for that). When using it like this however I am rewarded by a seriously awesome message in red:
Now, this parameter class is for module-internal use only, so I suppose I could just call it like this:
$cred = [DbaCredential]"foo\bar"
Get-WmiObject -ComputerName "computer1" -Credential $cred.Credential -Class "Win32_OperatingSystem"
Which would work just fine. My queen and project leader however would appreciate to not confuse random contributors (because we already have a lot of fancy features and we don’t want to scare them away. They are trying to help after all). Every little thing adds up and so I’ve spent a few hours trying to make it work. By now I really want to know what the heck is happening here, out of sheer bloody-mindedness!
What I’ve tried so far:
- Set up implicit conversion
- Implemented
IConvertible
- Created a PSTypeConverter for the conversion and attached it to the input type.
Guess what … none of it worked. Giving up on it, I dug down into the exception and PowerShell code (well … it was more a parallel thing, alternating between the two):
- The parameter binding passes it to the
CredentialAttribute
’sTransform
method. This in return callsLanguagePrimitives
’sT FromObjectAs<T>
method to have it bake aPSCredential
object. In that method it tries to cast to PSCredential which should be working, dangit! , returns null instead and theTransform
method throws the exception which is then relayed by the parameter binding.
What is happening here and what did I forget? Any ideas?
The current implementation can be checked out on our dbatools project’s DbaCredentialParameter branch
Thanks in advance for any ideas, inspiration or suggestions, Cheers, Fred
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:11 (5 by maintainers)
I believe we could enhance binding to support IConvertible/PSTypeConverter
Indeed you could and in the long term it would be helpful. Basically, supporting consistency in the type coercion process would help. It’s extremely flexible and powerful, so hardcoding for a specific input type is not helpful as far as I can see it, so yes, I’d appreciate it. I don’t really know how much impact ‘fixing’ this would have compared to the effort to do it - I use parameter classes heavily, but I don’t know anybody else who does so - but even then it would make powershell behavior more uniform and consistent.
Btw, I solved the prompt-for-credential issue with a constructor that accepts string. So when you just pass a name, it’ll prompt as part of the default coercion process while binding the parameter (and I added a “remember” checkbox, so it’ll remember the credential matched to the name for the duration of the process).
The link above is dead, but that’s because the branch was merged into development. So if you want to check out our implementation, just download it and run
[DbaCredential]"foo"
(or build a dummy function to actually have it operate as parameter type).