question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PSCredential Parameterbinding quirks

See original GitHub issue

Hi,

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: image

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’s Transform method. This in return calls LanguagePrimitives’s T FromObjectAs<T> method to have it bake a PSCredential object. In that method it tries to cast to PSCredential which should be working, dangit! , returns null instead and the Transform 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:open
  • Created 6 years ago
  • Reactions:1
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
iSazonovcommented, Jan 15, 2021

I believe we could enhance binding to support IConvertible/PSTypeConverter

1reaction
FriedrichWeinmanncommented, Aug 17, 2017

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).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Chapter 12. Working with credentials - PowerShell in Depth
12.1. About credentials. Most of the time, you'll find that a –credential parameter will accept one of two objects: a string object or...
Read more >
Add Credential support to PowerShell functions
The PSCredential object represents a set of security credentials such as a user name and password. The object can be passed as a...
Read more >
PowerShell Quirks: Binding Required Argument Parameters
Case in point: Cannot bind argument to parameter 'SomeParam' because it is an empty string or null. What does this message mean?
Read more >
Find-Package has no -ScriptSourceLocation parameter
I think what you're seeing here is a bug, either with Find-Package or it could be some quirk/bug with named parameter sets.
Read more >
Using C# to Create PowerShell Cmdlets: Beyond the Basics
Get-NetworkAdapter :Cannot bind argument to parameter 'Name' because it is an empty string. While accurate, that message is less than ideal; ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found