Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make WslDefaultParameterValues dynamic not static #32

Closed
Ergamon opened this issue Dec 9, 2021 · 3 comments
Closed

Make WslDefaultParameterValues dynamic not static #32

Ergamon opened this issue Dec 9, 2021 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@Ergamon
Copy link

Ergamon commented Dec 9, 2021

Is your feature request related to a problem? Please describe.
Currently we can set the default parameter values in a hashtable named WslDefaultParameterValues.

This is basically a Dictionary<string, string>.

Describe the solution you'd like
I would like to see a modification, e.g. $WslDefaultParameters.

This should check the type. If it is a string, then work like before.

If it is a scriptblock, then get the value at runtime by executiong the function.

Additional context
In case you are asking why I want this:

I am using a commandline tool accepting passwords and want to use PowerShell´s secret store for that.

To combine these, I can to the following:

ansible-playbook playbook.yaml -i /hosts --extra-vars "ansible_become_pass=$((Get-Secret MySecret).Password | ConvertFrom-SecureString -AsPlainText)"

Of course the last parameter is almost always needed and very clumsy to write in this notation.

So what I want to do, is store this expression as a default parameter.

But this does not work as when I put it this way in the dictionary, the $(xxx) part is not evaluated.

I can do the following:
$WslDefaultParameterValues = @{ "ansible-playbook" = "--extra-vars ansible_become_pass=$((Get-Secret MySecret).Password | ConvertFrom-SecureString -AsPlainText)" }

This works of course, but now

$WslDefaultParameterValues[ "ansible-playbook"]

shows my password.

To make the usage conveniant, I would suggest accepting strings and functions. So the typical case works like before, but there is an additional option to make the default parameter generation dynamic.

@mikebattista mikebattista self-assigned this Jan 25, 2022
@mikebattista
Copy link
Owner

Could you try this?

  • Open (gmo WslInterop).Path to edit
  • Add if (`$defaultArgs -is [scriptblock]) { `$defaultargs = . `$defaultargs } between these lines
  • Restart PowerShell to reload the module
  • Set $WslDefaultParameterValues["ansible-playbook"] = { "--extra-vars ansible_become_pass=$((Get-Secret MySecret).Password | ConvertFrom-SecureString -AsPlainText)" }
  • Run ansible-playbook

Let me know if that works for you.

@mikebattista mikebattista added the enhancement New feature or request label Jan 25, 2022
@Ergamon
Copy link
Author

Ergamon commented Jan 25, 2022

Hello Mike,
this works like a charm.

I am no exception to the typical user who only addresses the developers, if they face a problem. So I want at least to take this post here to thank you for your great module. Probably not too many are aware of this little gem, but this module makes working with PowerShell and WSL so much better.

@Ergamon Ergamon closed this as completed Jan 25, 2022
@mikebattista
Copy link
Owner

Thanks for testing it and the kind words. I'm glad you like the module.

I'll add this feature to the module and publish an update. Reopening to track that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants