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.

Feature Request / enable the creation of alias with cmdlet and its params together

See original GitHub issue

Summary of the new feature/enhancement

Currenly, we can only create alias to a cmdlet name only, it would be nice to be able to create alias to the cmdlet and with its params also.

The workaround that I could find till now is to create in advance a new custom cmdlet (via function{}) which calls the target cmdlet with the expected params, and then create an alias that points to this new cmdlet.

# The workaround by creating a new cmdlet, 
# ok for complexe functions, 
# but not cool for calling just a cmdlet with params in one line.

function Out-StringWithStream {
    Out-String -Stream
}

Set-Alias oss Out-StringWithStream

Proposed technical implementation details (optional)

Set-Alias oss Out-String -Stream

# or with ScriptBlock
Set-Alias oss {Out-String -Stream}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
KirkMunrocommented, Sep 11, 2019

My old pspx module makes this easier. I could refresh that for PowerShell v.Current. It’s one of those things where I felt like a rather extreme propeller head and wondered if anyone was actually using the functionality it provided, but it would allow for something like this:

New-ProxyFunction `
    -CommandName Out-String `
    -ProxyName Out-WithStringStream `
    -RemoveParameter Stream `
    -Begin {
        $PSPassThruParameters.Add('Stream',$true) > $null
    } `
    -DefineNow

I don’t know if you can actually get the code for that module from that link anymore, but I have it local, and started refreshing it a little while ago. I would need to think about how I would do the same thing if I were to design it for PowerShell today.

Even with that though, there is definitely something to be said about associating an identifier (like an alias, but I think functions are better for this) with a command plus a default set of parameter values, and having PowerShell know exactly what to do. It’s quite a simple concept, really. My New-ProxyFunction command could do much, much more than that, but the simple case would be quite useful.

What if the identifier was not an alias? What if we added a New-Function command that had one parameter set to simply define a function (like you can with New-Item, today, taking a name and a script block), and another parameter set that accepts a name, the name of a command you want to proxy, and a hashtable of parameters you want passed in by default. I wouldn’t call that New-ProxyFunction – the fact that it creates a proxy is a little beside the point, and the name shouldn’t add complexity to what it does IMHO. I would just call it New-Function, and let the -CommandToInvoke parameter indicate that it’s actually proxying something. That would allow for an even simpler syntax than what pspx provided, like this:

New-Function `
    -Name Out-WithStringStream `
    -CommandToInvoke Out-String `
    -Parameters @{
        Stream = $true
    }

Since you can create proxy cmdlets, I wonder if I should just do that but create proxy cmdlets instead for better performance than proxy functions. 🤔

2reactions
KirkMunrocommented, Sep 13, 2019

@iSazonov I disagree with the resolution of this issue. Answering a request by saying “just go do this really complicated thing” (proxy commands are not trivial) is not an answer. At a minimum, it should be left open to see if others have similar interest or to continue the discussion. Having it marked as closed and answered signals that there isn’t a discussion to be had here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Set-Alias (Microsoft.PowerShell.Utility)
The Set-Alias cmdlet creates an alias to the function in the current PowerShell session. The Name parameter specifies the alias's name, Go ....
Read more >
PowerShell - Is there a way to create a sort of 'alias' that ...
I recently created a PowerShell script to automate and test PowerCLI scripts before running them on vCenters/ESXi hosts. At the moment, I use...
Read more >
What is PowerShell and How to Use It: The Ultimate Tutorial
This comprehensive guide explains Windows PowerShell's key uses and features. Learn more about the flexible command-line interface and automation tool.
Read more >
Designing Professional Parameters - powershell.one
Parameters are a way for the caller to submit information to code. They are a fundamental feature of PowerShell scriptblocks: a param() ...
Read more >
Create or update alias API | Elasticsearch Guide [8.9]
Path parametersedit · <alias>: (Required, string) Alias to update. If the alias doesn't exist, the request creates it. Index alias names support date...
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