r/PowerShell • u/oshout • Aug 19 '16
Curl author asks Microsoft to remove 'curl' and 'wget' aliases from PowerShell
https://github.com/PowerShell/PowerShell/pull/19019
u/jddunlap Aug 19 '16
These aliases are among those removed by my provisioning script on any new machine - I use the GnuWin toolkit with Powershell, and I want to be able to use the real curl from time to time, despite the power of Invoke-WebRequest.
5
u/da_chicken Aug 19 '16
You can just use
curl.exe. I usewhere.exefrom time to time, so I know your pain.5
u/omrsafetyo Aug 19 '16
I have the GnuUtils installed on most of my systems, but have phased most of them out of my scripts as much as possible, because honestly, Powershell's native equivalents are better the majority of the time (object oriented). Granted, I still use head everywhere, because there -c parameter is incredibly useful, but there isn't an equivalent alias in PS (which I just discovered by doing get-command head), so I haven't bothered looking for an equivalent PS native... but I'm sure I could do it with a .NET native function.
1
u/darkscrypt Aug 23 '16
gc sometext.txt | select -first 5 gc sometext.txt | select -last 5
1
u/omrsafetyo Aug 23 '16
GNU head does not have line count for the -c parameter, it's the number of bytes. That would be the equivalent to "head -n 5" or "tail -n 5".
Looks like the closest thing would be something like:
function head { param ( [int] $c, [string] $file ) $fs = [IO.File]::OpenRead($file) $Bytes = New-Object Byte[] $c For ($i=0; $i -lt $c;$i++) { $Bytes[$i] = $fs.ReadByte() } $fs.close() [System.Text.Encoding]::ASCII.GetString($Bytes) }This appears to be basically equivalent to
head -c 128 Filename1
u/darkscrypt Aug 23 '16
Oh, okay. I gotcha. I've never had to pull in number of bytes before, but I can see where that would be useful.
7
u/ipreferanothername Aug 19 '16
legitimate complaints from geeks have a tendency to devolve into pedantism and a desire to be bowed to when you think [and/or prove] you are correct: news at 11.
5
u/omrsafetyo Aug 19 '16 edited Aug 19 '16
How reliable, or possible would it be, for commands that have *nix aliases, to add a ParameterSet that includes the native parameters, and have that ParameterSet check the PATH for the executable, and if it exists, use that instead?
There are actually very few parameter/switch collisions between invoke-webrequest and curl, unless you count the shorthands in curl vs. the aliases in the powershell parameters:
PS > (get-command invoke-webrequest).Parameters.Keys | % {
>> [PSCustomObject] @{
>> Name = $_
>> Aliases = (get-command invoke-webrequest).Parameters[$_].Aliases
>> }
>> }
>>
Name Aliases
---- -------
UseBasicParsing {}
Uri {}
WebSession {}
SessionVariable {SV}
Credential {}
UseDefaultCredentials {}
CertificateThumbprint {}
Certificate {}
UserAgent {}
DisableKeepAlive {}
TimeoutSec {}
Headers {}
MaximumRedirection {}
Method {}
Proxy {}
ProxyCredential {}
ProxyUseDefaultCredentials {}
Body {}
ContentType {}
TransferEncoding {}
InFile {}
OutFile {}
PassThru {}
Verbose {vb}
Debug {db}
ErrorAction {ea}
WarningAction {wa}
ErrorVariable {ev}
WarningVariable {wv}
OutVariable {ov}
OutBuffer {ob}
PipelineVariable {pv}edit:
Seems like this should be fairly trivial honestly:
PS C> get-command ls -All
CommandType Name ModuleName
----------- ---- ----------
Alias ls -> Get-ChildItem
Application ls.exe7
u/Pandalicious Aug 19 '16
have that ParameterSet check the PATH for the executable, and if it exists, use that instead
That would allow a script to behave differently on different machines in a way not at all obvious to end users.
3
u/omrsafetyo Aug 19 '16
I'm not sure how so. On a system where the command is not present, the commandlet would have no logic to execute, and so would essentially warn the user, or error, etc., because right now that parameterset would be invalid anyway, so it wouldn't run on another system, or any system, and wouldn't be coded to begin with.
This makes it such that if you have the command installed, it works, and if you don't, a warning is issued. That seems fine, and doesn't break anything backward compatibility wise. FWIW, I posted my suggestions to that thread, and Jeffrey Snover said that these 2 ideas are 2 of the 3 he is mulling over. The 3rd being to add a function to your profile that removes UNIX aliases.
I think his 3rd solution leads to different behavior across machines - because I can then publish code that uses "curl" in my script, and if I have written the script under the assumption that other people are also using the Remove-UNIX-Aliases function in their own scripts, then it will error on their system; or for other commands where maybe the parameters do line up a little better, potentially give unexpected output. So the alternative solution to what I have proposed would definitely result in the behavior you're describing, whereas in my solutions, worst case scenario, you don't have required dependencies.
4
u/Pandalicious Aug 19 '16
I see what you're saying but you're still introducing a breaking change. Say there's a preexisting script where the curl alias was used with unsupported arguments and never worked, but the script had subsequent error handling code block that did the right thing plus other stuff. With your change the first call would suddenly start working causing the rest of the script to fail because it had come to depend on things done in the error handling block. Even worse, the script might be working just fine for years and then someone working on something completely different installs curl.exe into the machine's PATH and suddenly the script would start failing.
So, to be sure, that's a pretty contrived example and I think the fix you proposed is pretty darn elegant and would just work 99% of the time. But my point is that it's still a breaking change in certain circumstances.
2
u/omrsafetyo Aug 19 '16 edited Aug 19 '16
Those are both good examples, and definitely worth consideration, even though I agree, quite contrived. But, the second example where someone installs curl, etc. is probably not too far out there.
jpsnover commented an hour ago
@omrsafetyo wow - I was actually chewing on both of those as possibilities.
The third one I'm thinking about (my current favorite) is something along the lines of @codeape2 comment about "Remove-All-Aliases-That-Are-Linux-Commands" that you could put into your profile.
Having agreed to do something to address this - I don't feel that this is time critical and we have some time to weight the pros/cons of alternatives to get it right.
jps[MSFT]
Still pretty cool to be about on the same page as the creator of Powershell :D
5
u/Swarfega Aug 19 '16
I don't care for 'wget' or 'curl' but 'ls'...
2
u/Theratchetnclank Aug 19 '16
Same. I like having ls, i type it all the time, not so much in scripts, just when typing commands in the console.
wget and curl in powershell are broken.
2
u/Swarfega Aug 19 '16
I'm pretty sure that script analyser also highlights if you use an alias in a script.
1
1
u/creamersrealm Aug 19 '16
I have to admit I am hooked on using the ls alias in PowerShell but I use gci alot as well.
13
u/Vakieh Aug 20 '16
24 hours. It took 24 hours from the repo going public to this bullshit.
If the open source community doesn't nip this in the bud MS will go back to closed source, guaranteed, nobody needs to deal with this sort of stupidity.
The request is made, the process is engaged, now sit down and stfu, no more input is required.
This rebuttal was important though:
warrenrumak commented about 10 hours ago • edited about 9 hours ago
Lots of people around here are promoting ideas of /actually/ breaking backward compatibility, for the sake of /theoretically/ improving cross-platform compatibility.
How about no.
Powershell was /never/ designed to behave like a Bourne shell derivative with a GNU toolset underneath, and anyone who expects it to start behaving like one 10 years on is being unrealistic. Aliases preempt disk files. That's the design.
It isn't just curl and wget, either. There's also diff, start, kill, ps, cat, tee, and some others. These Powershell cmdlets all offer similar functionality to Unixy commands of the same name, but the inputs and outputs are completely different. Command names are a starting point for humans to understand what a function does, not an iron-clad contract with perfect fidelity across all platforms.
If at some point in the future, Microsoft were to release an update to Server 2016 that disables Powershell aliases by default, I'm going to have hundreds of broken servers to contend with. Microsoft's long-standing averseness to changing the behaviour of existing commands and APIs is exactly why so much software written 10, 20, 30 years ago continues to work in Windows 10. It's also why Powershell hasn't replaced cmd.exe. People depend on this ongoing commitment to backwards compatibility for business continuance. For all we know, removing aliases could cause random pieces of Exchange Server or some other critical business software to stop running or quietly behave incorrectly!
And yet there are people here who are demanding that I accept that risk because they want to type 'curl' instead of 'curl.exe'? Madness. Madness.
With that said, I believe adding a "-NoAliases" parameter to powershell.exe would be a good way to provide an alias-free environment for those who do not like this feature of Powershell.
8
Aug 20 '16
Microsoft open sources PowerShell; finds out that Linux users are absolutely insufferable.
2
3
u/creamersrealm Aug 19 '16
Thankfully with whatever happens with this debate every single script I actually save uses the full expanded name of the cmdlet and not an alias. Aliases should only be used for quick work.
1
Aug 20 '16
I agree, but unless Microsoft takes this as an official position, there is a reasonable expectation that using aliases in scripts is and will remain viable.
And regardless of what they decide to do moving forward, there is an expectation that things that used to work will continue to work.
9
Aug 19 '16
Textbook example of "do it right" vs "be backwards compatible".
17
u/Pandalicious Aug 19 '16
Textbook example of "do it right" vs "be backwards compatible".
But let's be very clear that if you're a platform provider, "be backwards compatible" is almost always the right choice. And if anybody thinks that attitude is limited to Microsoft, I urge them to look up Linus's many rants when people propose ABI breaking changes in order to fix something or when GLIBC improved memcpy in a way that broke programs that were using it incorrectly.
3
Aug 20 '16
This made me realize that I argue a lot like Torvalds, which in turn made me realize that I need to change my behavior.
His first couple of posts are mean as shit and basically just him blowing off steam at people he perceives to be wrong and dumb, and then basically all of the rest of his comments are him either contributing constructively or helping people out.
That's not a healthy way to deal with people.
2
u/Pandalicious Aug 20 '16
Torvalds's saving grace is that he has very good judgement on technical matters, so when he goes off on his rants he's at least usually correct on the substance. Well 'saving grace' might be too strong a term, since I agree with you that it's still not a healthy way to deal with people.
2
Aug 20 '16
Yeah, he's nearly always right, but he's also nearly always an asshole. The two don't equal out. Not to me, anyway.
He's a brilliant person and has made nearly immeasurable contributions to the open source community, but I'd still be happy to never have a conversation with him.
4
u/da_chicken Aug 19 '16
Yep.
PS C:\> $PSVersionTable.PSVersion Major Minor Build Revision ----- ----- ----- -------- 5 0 10586 494 PS C:\> where.exe powershell.exe C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe2
u/teejaded Aug 19 '16
Get-command works for binaries in the path too.
0
u/da_chicken Aug 19 '16
So it does. I thought it only returned cmdlets. I'm sure I tried it years ago. Oh well. ¯_(ツ)_/¯
2
Aug 19 '16
Which is also the reason for a lot of the things that powershell does in a way that is just objectively better than bash - powershell was created with 15+ years of hindsight and zero (at the time) tech debt to maintain compatibility with.
7
2
u/SaladProblems Aug 19 '16
Feels like they just need to add a '-NoAliases' parameter to powershell.exe
1
u/RICHUNCLEPENNYBAGS Aug 19 '16
That does have the problem that the same script won't behave the same way on all system. But then again, script execution isn't even enabled by default so maybe it's OK.
1
u/Nation_State_Tractor Aug 20 '16
That would disable a lot of operator-like functions such as
%{ }and?{ }(ForEach-ObjectandWhere-Object)1
u/VTi-R Aug 20 '16
It would, but you could easily get more granular with (say)
powershell -NoLinuxAliasesJust as an example.
2
u/Gerane Aug 19 '16
So much hate in that issue by people who don't even understand the issue. Many just assume that PowerShell is doing this on Linux, but that is not the case. The proper curl and wget is used on Linux. This issue only impacts Windows and has been the same for years. However, with the linux subsystem in windows 10, I agree that many of these aliases should be revisited.
You can tell that issue must have hit hacker news or Linux subreddits.
2
u/darkscrypt Aug 23 '16 edited Aug 23 '16
Just looking at that thread makes me wince. I can't stand how elitist some people are. Same usual gems like
Well. Aliases like these if you come from the unix world would never have been built in.
and
You really can't deny that you knew a long time ago that what you did amounted to hostile behaviour towards other people.
...
either microsoft users who use powershell alias for curl and wget are dumb or microsoft is dishonest
This gem is my favorite:
This thread shows that Microsoft is dysfunctional. Further, the bureaucracy that makes this a lengthy conversation instead of a simple "oops, you're right, let us fix that today" is laughable. "corporate governance" and whatever, good sense has gone out the window.
Just wow. No good deed goes unpunished. *edit: Lots of formatting edits.
9
u/dougsec Aug 19 '16
This pull request is a great example of why I've stayed away from OSS projects. A massive pedantic thread of people arguing incessantly.
I think it's clear that when the aliases were created, they were trying to provide basic functionality for things that people were used to from a *nix command line. I'd argue that most of the time when people misstype these on the PoSh command line they're trying to perform very basic functionality and certainly not harness the full capability. I personally have been very glad to have the 'ls' alias when using powershell because ls is more natural for me than dir.
4
u/RICHUNCLEPENNYBAGS Aug 19 '16
I couldn't agree more. And the people saying Microsoft should "just" break BC over this are more than a little obnoxious.
0
u/DoTheEvolution Aug 20 '16
This pull request is a great example of why I've stayed away from OSS projects.
1
u/wbedwards Aug 21 '16
These types of things is why it's always best practice in PowerShell to use the full cmdlet in scripts. I use aliases interactively, but never in scripts to preserve readability, and portability... I use PowerShell Studio for scripting, which has an option to expand aliases as part of its error checking/auto-formatting functionality to make this a little easier.
1
Aug 20 '16
Is this even a real issue for ANYONE other than the Microsoft hating linux purists?
1
u/darkscrypt Aug 23 '16
That's exactly what I was thinking! I mean, what's the likely-hood of these guys using powershell anyways?
Seriously, you now have the option of literally using bash! If you want linux compat and linux like functionality, then use bash! It's really simple.
0
Aug 19 '16
So far, Godwin's law has not been breached in the discussion, but I suspect it won't be for long.
0
u/_ralph_ Aug 20 '16
The words 'windows' and 'microsoft' were used often enough, they can be seen as synonyms in the context ;)
0
u/RICHUNCLEPENNYBAGS Aug 20 '16
The main takeaway I have from a lot of the more hysterical comments is that the authors of them never use PowerShell and don't understand its most basic features (I don't see how else the suggestion that Invoke-WebRequest be made to function exactly like wget could make any sense at all).
0
Aug 19 '16
[deleted]
10
u/omrsafetyo Aug 19 '16
This is also a good solution. But honestly, who would actually use curl or wget as aliases for Invoke-WebRequest? Anyone that knows what curl and wget are will probably go "oh, that doesn't work as expected, or even have the correct parameters" and then not use it.
2
u/RICHUNCLEPENNYBAGS Aug 19 '16
It's less trouble than searching the Web for "PowerShell equivalent to wget."
1
u/omrsafetyo Aug 19 '16
Doubtful, unless I'm misunderstanding you. The native parameters aren't supported, so if you're trying to use the alias, you already have to research, and you'll learn that the command is an alias for invoke-webrequest.
If you want to use curl or wget you'll download and install them, and then hardcore the path or extension in your script. But using the commands as an alias leaves you researching it either way you look at it.
2
u/RICHUNCLEPENNYBAGS Aug 20 '16
You can just type Get-Help wget, if the default behavior wasn't what you wanted.
2
u/midnightFreddie Aug 21 '16
From a Powershell point of view, sure. But if you're new to Powershell and
curlorwgetdont' behave the same, you're even more confused.I've always thought it odd to have those aliases. At least with
diryou sometimes get expected behavior (of the original command). Withcurlandwgetyou never do.Edit: And, having the alias precludes using
curlorwgetif I previously had the Windows executables installed (without explicitly usingcurl.exeorwget.exe)1
Aug 21 '16
but for simple enough things such as "wget $url/file", it works as intended. it's when you get into all the different flags that it can go tits up.
2
u/midnightFreddie Aug 21 '16
??? I actually double-checked and did both
curl <url>andwget <url>before posting. They don't do what I might expect from having used both before.
curl.exe http://midnightfreddie.comreturns the source code of the page to stdout.
curl http://midnightfreddie.comreturns an object; if run bare it looks like this (truncated):StatusCode : 200 StatusDescription : OK Content : <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>midni... RawContent : HTTP/1.1 200 OK Connection: keep-aliveAt best, native curl's
curl http://midnightfreddie.comis represented by(curl http://midnightfreddie.com).Content.It just doesn't make sense to me to have the aliases since they don't work at all like the original commands and block the original commands use if they're already installed. And if you're having to look it up, anyway,
iwris a more sensible alias than the others.0
Aug 20 '16
[deleted]
2
u/RICHUNCLEPENNYBAGS Aug 20 '16
How would that work if you removed the alias?
1
Aug 21 '16
It still displays what it would go to but removes it from the list of usable aliases. I just tried it on WMF 5.0
0
Aug 20 '16
[deleted]
0
Aug 20 '16
[deleted]
1
u/VTi-R Aug 20 '16
In my experience people who are used to text manipulation only (is this an example of "I HAZ A HAMMER SO ALL THE THINGZ IS NAILZ" or something?) really don't get it. They do not understand why object manipulation is so awesome. They get hung up on "the text format isn't consistent" or "where's cut, I want characters 14-22" or something. It really takes a complete mental rewrite to "get" it.
Hell, I've got far too much scripting experience, back to the DOS 3 days; and I've written lots of code despite being 98% Infrastructure focused. It still takes me a while to get into the PoSH spirit (I'm probably just old).
101
u/replicaJunction Aug 19 '16
Jeffrey Snover handled that like a boss, in my humble opinion.
He stopped the back-and-forth arguing right away by acknowledging that it's a real issue that needs to be addressed (one way or another). No way Microsoft would blindly dismiss it after a comment like that from him.
He provided an immediate triage solution for affected users.
He also pointed out the counter-argument - regardless of whether the aliases should be there or not, they're there now, and people's production code could be relying on them.
Just saying...I think his was possibly the best response Microsoft could have had to this PR.