views:

171

answers:

2

Hi there, a newbie powershell question:

I'd like to make an alias in powershell exactly equivalent to this Bash alias:

alias django-admin-jy="jython /path/to/jython-dev/dist/bin/django-admin.py"

In tinkering with it so far, I've found this to be very difficult.

-Powershell Aliases only work with Powershell commands + function calls

-No clear way to allow for an unlimited number of args on a powershell function call

-Powershell seems to block stdout


UPDATE: Bounty of 150 pts added for a working implementation of bash alias simulation in powershell.

It's worth noting that I've tried the solution put forth here: http://huddledmasses.org/powershell-power-user-tips-bash-style-alias-command/

And have gotten the following syntax-related error on loading up powershell:


The term 'which' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spell
ing of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\Dan\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:9 char:27

+             $cmd = @(which <<<<  $_.Content)[0]
    + CategoryInfo          : ObjectNotFound: (which:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

I'll accept a fix to this as an answer as well.

A: 

Functions can have arbitrarily many arguments. You just need to use $args to access them.

As for the stdout issue: What exactly are you experiencing?

Joey
+1  A: 

PowerShell aliases can be used for any "command" including EXEs:

new-alias n notepad.exe

Update: as mentioned in the comments, PowerShell aliases do not allow for arguments. They are simple "aliases" for a command name and nothing more. To get what you after (I think since I'm not familiar with Bash aliases) try this:

function django-admin-jy {
    jython.exe /path/to/jython-dev/dist/bin/django-admin.py @args
}

It uses a feature of PowerShell 2.0 called argument splatting. You can apply @ to a variable name that references either an array or a hashtable. In this case, we apply it to the variable named args which contains all the unnamed parameters.

If you want a truly generic way to create aliases functions that take parameters, try this:

function New-BashStyleAlias([string]$name, [string]$command)
{
    $sb = [scriptblock]::Create($command)
    New-Item "Function:\global:$name" -Value $sb | Out-Null
}

New-BashStyleAlias django-admin-jy 'jython.exe /path/to/jython-dev/dist/bin/django-admin.py @args'

Regarding the issue with Joel's approach, I suspect he has which aliased to Get-Command. Try replacing which with Get-Command.

Keith Hill
Hi Kieth,The New-Alias command does not appear to allow arguments to be specified for the executable. (Maybe I'm wrong?)Also, I'm not worried about getting the prompt back immediately, I just want "django-admin-jy help"to behave exactly like"jython /path/to/jython-dev/dist/bin/django-admin.py help"(And, of course, have the ability to specify any other argument as well.)Not to seem lazy (I've been tinkering with this for some time), but I think that seeing a complete function for bash alias simulation might be the most helpful for everyone.
RightFullRudder
Kieth,Your first answer worked! The second one resulted in an error (too big to paste here), but I'm content with just using the first (not much extra syntax), and am awarding you the bounty.
RightFullRudder
I fixed the issue with the second approach (had some left-over code from a previous try).
Keith Hill