4 ms·
I love the strongly-typed cookies but I absolutely hate the powershell syntax. It's like C# and Bash got together and had a really ugly baby.
by wvenable 10y ago
I love the strongly-typed cookies but I absolutely hate the powershell syntax. It's like C# and Bash got together and had a really ugly baby.
- AimHere 10y agoAs long as you bear in mind that it's main purpose is a usable replacement for the CMD.EXE batch language, then you should be willing to forgive Powershell it's warts.
- wvenable 10y agoExcept that they added a bunch of new warts that don't have any relation to CMD.EXE. They took the opportunity to re-invent how a shell works from the strongly-typed object perspective but then they copied some of terrible syntax of 40 year old unix shells. I just don't understand it. I'd rather write scripts in pure C#.
- emodendroket 10y agoThe point of PowerShell isn't just to write scripts but to work as an interactive shell.
- wvenable 10y agoI'm not at all convinced that the syntax is any better for typing in as commands in an interactive shell.
- emodendroket 10y agoI think it works pretty well for that beyond the weird omission of anything like &&
- wvenable 10y agoIt works fine as an interactive shell but that's not exactly high praise. It could be better for that and for scripting but instead tries to be more familiar to bash users. As a bash user, I'm terribly happy about it's syntax either but it's the price to pay for 40 years of backwards compatibility. Powershell has no such excuse.
- emodendroket 10y agoTrue enough.
- Senji 10y agoIt sorely needs an && and a &
- JeffreySnover 10y agoThat is not quite right. At the end of the day it is all about automation. But we took the position that the way Admins automate is by scripting the things that they do in an interactive shell. Bruce Payette had a great way of saying it - he said that the lifecycle of 99% of scripts starts at the prompt and ends at the carriage return. Then, somethings you want to codify things so you put it into a script. Then as the function becomes more used and more important, you want to make it more and more formal without having to throw everything away and start with a new language. So YES and interactive shell is important but it is important as a stepping stone to automation. That is the PowerShell mindset. Jeffrey Snover [MSFT]
- lucb1e 10y agoThat is not quite right either. I enter about 200 commands on a normal workday and write maybe two scripts a month in a shell language. A shell language should be a shell language, it's not merely a stepping stone to write a script after using a command just once. If that's what I'm looking for I'll use Python. And from the other side, Python would be a terrible shell (by default), and that's fine because that's not what it's used for. If Powershell is made for both, it has to make compromises both as a shell language and as a scripting language. This is one of the things that annoy me about the Windows ecosystem: it ships with no languages beyond a shell, which makes scripting for it very annoying, and you can't even install a language of choice via a package manager (one has to go find a download somewhere on the web).
- omaranto 10y agoI thought Windows shipped with .NET which includes a command line C# compiler, csc.exe.
- ygra 10y agoThe current C# compiler is no longer distributed as part of .NET. They're separate again (as is MSBuild).
- aeorgnoieang 10y ago
- cptskippy 10y agoI would hope it's also meant to put vbscript out to pasture as well.
- emodendroket 10y agoThe implicit return semantics are insane, since you can implicitly early-exit when you do things like call ArrayList.Add (which returns a bool to indicate whether the thing was added).
- new_hackers 10y agoinsane in a good way right? You aren't "returning" you are emitting to the pipeline
- emodendroket 10y agoNot if you're in a script and declaring a function. So, no, it's a source of confusing bugs. Implicit return works better in, like, Ruby, where it has to be at the end and can't just randomly exit in the middle of a function declaration.
- new_hackers 10y agoArrayList.Add ... whatever ... | Out-Null Its not implicitly returning. The return value wasn't assigned so it is emitted to the pipeline.
- emodendroket 10y agoOK, sorry (there is an explicit "return" keyword that I thought worked the same way). Either way, yes, I know you can avoid it by piping to Out-Null, but that behavior is surprising.
- ygra 10y agoreturn {expression} is equivalent to {expression} return the only place you have an exit from a function, script block or similar is either at the end or at a return statement. Every value that isn't assigned or piped into nirvana gets pushed to the pipeline. I actually can't remember having used return statements more often than once or twice so far (and I use PowerShell daily, heavily). Then I rarely found the need to use ArrayList. Sure, you can use it, but you can just as well do $array = @() $array += $item A personal pet peeve of mine is actually that lots of people write PowerShell very similar to equivalent VBScript or C# code. And that makes for horrible PowerShell code (in my eyes). The pipeline is immensely powerful and not using it often makes for pretty crappy code.
- mschuetz 10y agoYeah, I wanted to switch to PowerShell but then I had to call an exe with variable arguments from the PowerShell script. That was a quick way back to cmd.
- emodendroket 10y agoCouldn't you use $() for this?
- vicda 10y agoThis was the hardest thing to figure out. I have a program just to ensure I'm sending the arguments that I think I am.
- 3minus1 10y agoI was frustrated as well by this. There's a program echoargs.exe you can use to find out what you need to escape in your powershell args.
- ygra 10y agoUsually everything works as expected, e.g. someprogram.exe arg1 arg2 arg3 $foo (Get-Item bar).Length Variables get expanded in the argument list, arguments get quoted automatically based on their content. The only gotchas I can think of are: & "command with spaces.exe" args It's a bit unexpected that the & operator is needed. Arguments sometimes get quoted unexpectedly, or need (single) quotes depending on PowerShell syntax rules to avoid evaluating something. The worst (but sadly common) thing people can do when faced with problems here, though, is heading for Invoke-Expression, sometimes in combination with all sorts of wrappers, each of them making the problem worse, not better (it cannot get better that way). The most robust way I tend to use when necessary is to just prepare a single array with arguments and splat that when calling the program: program.exe @array No surprises with PowerShell's parsing in command mode because we prepare the array in expression mode. Things are generally more predictable. Automatic quoting of the arguments still happens, though. (Side note: My own little echoargs replacement is actually a batch file :-) @echo off echo.argv[0] = %~0 set cnt=1 :loop echo.argv[%cnt%] = %~1 set /a cnt+=1 shift if not [%1]==[] goto loop )
- 10y ago
- retox 10y agoIn my admittedly limited experience there seems to be at least 3 aliases for every keyword which makes reading someone else's code a real hassle sometimes. Our ops guys swear by it though.
- emodendroket 10y agoYou're supposed to avoid the aliases in scripts. Or that's what's recommended anyway.
- libraryatnight 10y agoYes, this makes for much easier to read code for people down the road :) I think OPS folks (myself included) sometimes create things thinking nobody but them will use them, then an occasion arises to share, and you're stuck with something functional but horrible to read. It took just one such occasion for me to convert to writing everything from the perspective that 'other people may need to use this.'
- viraptor 10y agoThere seem to be a few tools to do it for you automatically. ctrl+shift+a if you're using powershell studio for example. Or https://gist.github.com/DBremen/9db6632423d673ff18f6 https://gist.github.com/DBremen/9db6632423d673ff18f6
- libraryatnight 10y agoThis is cool, I'd not seen this before! Thank you for the link.
- vicda 10y agoYeah, it's a mixture of Perl and Bash with sudden C# calls interspersed.