News for hackers. Stuff that matters.
Re:PowerShell is open sourced and is available on Linux by wvenable on Thursday August 18, 2016 @12:02PM (#12313563)
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.
Re:PowerShell is open sourced and is available on Linux by AimHere on Thursday August 18, 2016 @12:05PM (#12313628)
As 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.
Re:PowerShell is open sourced and is available on Linux by wvenable on Thursday August 18, 2016 @12:21PM (#12313816)
Except 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#.

Re:PowerShell is open sourced and is available on Linux by emodendroket on Thursday August 18, 2016 @12:26PM (#12313886)
The point of PowerShell isn't just to write scripts but to work as an interactive shell.
Re:PowerShell is open sourced and is available on Linux by JeffreySnover on Thursday August 18, 2016 @01:01PM (#12314254)
That 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]

Re:PowerShell is open sourced and is available on Linux by lucb1e on Thursday August 18, 2016 @03:26PM (#12315627)
That 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).

Re:PowerShell is open sourced and is available on Linux by Jayku1 on Thursday August 18, 2016 @06:13PM (#12316705)
This is a really strange argument -- he's saying that PowerShell was designed so that you could more easily move from shell to script to library ...

Your counter is -- you don't do that? Of course you don't, because your shell is bash. That's exactly the point. If your shell is bash, when you write a script you use a different language. What if there was a shell where you could actually script ...

You're absolutely right, PowerShell is made for both, and it has made compromises (like using comparison operators like -eq instead of ==).

Compromises are not inherently bad.

Re:PowerShell is open sourced and is available on Linux by dragonwriter on Saturday August 20, 2016 @01:33AM (#12325354)
> If your shell is bash, when you write a script you use a different language.

The number of bash scripts I see for various -n-x related tasks suggests that this is substantially untrue; people do, in fact, do scripts in bash.

> What if there was a shell where you could actually script ...

I can't think of any shell that you can't script. OTOH, interactive use and scripting are substantially different use cases, so it really makes sense to have both interactive-optimized languages that may be usable in script and scripting languages that may be usable interactively, but focus use of each in their optimized role.

Re:PowerShell is open sourced and is available on Linux by lucb1e on Friday August 19, 2016 @05:45PM (#12323743)
> Your counter is -- you don't do that? Of course you don't, because your shell is bash.

Yeah of course I'd use Bash because Powershell only just became available for other platforms, but that's not my point.

I understood his point like this: the grandparent of his post said powershell has some bad things for scripting. Someone responded to that by saying it's not just for scripting. He said something which I understand as 'that's not quite right, I see a command as a stepping stone to writing a script', To which I finally responded 'I enter a lot of commands but rarely write scripts in [my interpreter of choice's language] because that language just isn't that great, and that's fine because the language is great for its purpose: command line usage'.

And compromises are inherently bad (in a way), that's the definition of a compromise: there are two or more mutually exclusive options and different parties have different first choices. Since they're mutually exclusive, a compromise must be made which is acceptable for both, but it's the first choice of neither (or at most one of them). Compromises are necessary for a dual-use language, but they don't make it prettier for both usages either.

Re:PowerShell is open sourced and is available on Linux by Graphon1 on Saturday August 20, 2016 @01:27AM (#12325345)
> it has made compromises (like using comparison operators like -eq instead of ==).

wat.

The use of -eq instead of == is a compromise?

I don't get that.

Re:PowerShell is open sourced and is available on Linux by brazzledazzle on Thursday August 18, 2016 @06:45PM (#12316832)
You're correcting Jeffrey Snover about the point of Powershell. I suspect that the guy that invented it and championed it through years of resistance at Microsoft probably knows what is or isn't intended with Powershell.
Re:PowerShell is open sourced and is available on Linux by lucb1e on Friday August 19, 2016 @06:15PM (#12323894)
Besides mirroring the condescending tone, I was mentioning that in my (GNU/Linux) experience -- which is a lot more command-line oriented than Windows -- it's not true what he's saying. I use a lot of commands and very few of them become scripts, thus the conclusion that it's not merely a stepping stone. A command line language can have different features than a good scripting language. Further I was remarking on the limitation of Windows which ships with a "one size must fit all" language: the language used by their shell (and now there's a 2.0 version of that, called powershell).

I'm sure he knows a lot more than me about a lot of powershell-related things. Still, beyond him being (according to you) the inventor and "champion" of powershell, there's nothing that tells me they did UX research into this, so I added my experience because it differs from his viewpoint.

Re:PowerShell is open sourced and is available on Linux by brazzledazzle on Friday August 19, 2016 @06:35PM (#12324011)
I think you're misreading what was exchanged. That is unless you're arguing with him about the intent behind the design of a product you had no part in creating. He was replying to "The point of PowerShell..." not, "The point of shells...". He was simply correcting someone who misinterpreted the intent.

I think you're also reading tone into his message that's not there. He's been very chill and helpful in this thread even in reply to open hostility.

I'm not sure what "according to you" means, but you can verify it yourself. Feel free to read Wikipedia or you can probably find one of his talks about it on youtube.

Re:PowerShell is open sourced and is available on Linux by aeorgnoieang on Thursday August 18, 2016 @05:27PM (#12316441)
Windows did ship with VBScript (and probably does still). You can also install languages via a package manager; an example:

- https://chocolatey.org/packages?q=python

Re:PowerShell is open sourced and is available on Linux by lucb1e on Friday August 19, 2016 @06:20PM (#12323930)
After installing a third party bootstrapping program. I suppose that's as close as it gets.

Windows still ships with VBScript, but it's not a language I'd want to look into either. Everyone that has worked with it sighs when it comes up and nobody actually uses it for automation as far as I know (everything's either powershell, good old batch scripts, or custom .NET software).

Re:PowerShell is open sourced and is available on Linux by blakeyrat on Thursday August 18, 2016 @06:26PM (#12316757)
You could just use WSH (Windows Scripting Host) with either VBScript or JScript the way you've always been able to. AFAIK, Windows has shipped with that scripting solution since like Windows 2000 or possibly even earlier.
Re:PowerShell is open sourced and is available on Linux by lucb1e on Friday August 19, 2016 @06:27PM (#12323959)
I noticed that on old computers (Windows 95, I think, and certainly 2000 and XP) where it would classify .js files as executables. I was about 11-14 so I didn't understand much of Windows at the time, let alone in a foreign language (I'm Dutch) when I wasn't even allowed to use the Internet (dialing up was expensive per-minute stuff), but yeah I remember that.

Still, I've never heard of anyone using Javascript or VBScript to automate anything. It's either powershell (these days), cmd scripts or custom .NET software. I've never even heard anyone mention using javascript in Windows (until apps came around in Windows 8) and vbs is only ever patchwork for legacy third-party software products as far as I've seen in my sysadmin days.

Re:PowerShell is open sourced and is available on Linux by Graphon1 on Saturday August 20, 2016 @01:29AM (#12325348)
> I've never heard of anyone using Javascript or VBScript to automate anything.

as just one example, It's common to include JavaScript or VBScript steps in Windows installers. You would never know it, if you ran an installer that used a script to automate a few things.

Re:PowerShell is open sourced and is available on Linux by blakeyrat on Friday August 19, 2016 @10:00PM (#12324724)
> Still, I've never heard of anyone using Javascript or VBScript to automate anything.

Well thousands of companies have done it, so. I don't know what to tell you.

Re:PowerShell is open sourced and is available on Linux by omaranto on Thursday August 18, 2016 @05:14PM (#12316381)
I thought Windows shipped with .NET which includes a command line C# compiler, csc.exe.
Re:PowerShell is open sourced and is available on Linux by ygra on Friday August 19, 2016 @02:11AM (#12318244)
The current C# compiler is no longer distributed as part of .NET. They're separate again (as is MSBuild).
Re:PowerShell is open sourced and is available on Linux by toyg on Thursday August 18, 2016 @03:43PM (#12315727)
As one of those enterprise pseudo-sysadmin-scripting-guy, I do agree with the sentiment; and I think the fundamental object-ness of PS was nice and the emphasis on docstrings is nice. However, I still think a python-like syntax would have been miles better. As much as I tried, I just can't get into Powershell -- way too many special characters look awfully "dirty", and $vars give me PHP/Perl-induced PTSD. Deployment is also a bit of a pain.
Re:PowerShell is open sourced and is available on Linux by wvenable on Thursday August 18, 2016 @12:41PM (#12314042)
I'm not at all convinced that the syntax is any better for typing in as commands in an interactive shell.
Re:PowerShell is open sourced and is available on Linux by emodendroket on Thursday August 18, 2016 @12:50PM (#12314126)
I think it works pretty well for that beyond the weird omission of anything like &&
Re:PowerShell is open sourced and is available on Linux by wvenable on Thursday August 18, 2016 @12:59PM (#12314221)
It 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.
Re:PowerShell is open sourced and is available on Linux by emodendroket on Thursday August 18, 2016 @03:08PM (#12315449)
True enough.
Re:PowerShell is open sourced and is available on Linux by Senji on Thursday August 18, 2016 @10:42PM (#12317652)
It sorely needs an && and a &
Re:PowerShell is open sourced and is available on Linux by cptskippy on Thursday August 18, 2016 @12:24PM (#12313851)
I would hope it's also meant to put vbscript out to pasture as well.
Re:PowerShell is open sourced and is available on Linux by retox on Thursday August 18, 2016 @12:51PM (#12314139)
In 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.
Re:PowerShell is open sourced and is available on Linux by emodendroket on Thursday August 18, 2016 @12:54PM (#12314165)
You're supposed to avoid the aliases in scripts. Or that's what's recommended anyway.
Re:PowerShell is open sourced and is available on Linux by libraryatnight on Thursday August 18, 2016 @01:12PM (#12314373)
Yes, 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.'

Re:PowerShell is open sourced and is available on Linux by viraptor on Thursday August 18, 2016 @05:20PM (#12316402)
There 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
Re:PowerShell is open sourced and is available on Linux by libraryatnight on Friday August 19, 2016 @12:42PM (#12321351)
This is cool, I'd not seen this before! Thank you for the link.
Re:PowerShell is open sourced and is available on Linux by emodendroket on Thursday August 18, 2016 @12:26PM (#12313877)
The 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).
Re:PowerShell is open sourced and is available on Linux by JeffreySnover on Thursday August 18, 2016 @12:46PM (#12314091)
I know the problem you are talking about - it has bitten all of us.

The new language mode you use when writing classes in PowerShell shifts the needle more towards programming semantics than shell semantics and addresses this.

Give it a try!

Jeffrey Snover [MSFT]

Re:PowerShell is open sourced and is available on Linux by new_hackers on Thursday August 18, 2016 @03:19PM (#12315555)
I really like the functional approach of PowerShell. The pipeline, while not the end-all-be-all of perfection, is still very, very good and very flexible. Once you get into the functional mindset, it is very easy to write terse, powerful code.

My real wish is that there was a syntax for writing function decorators and function generators. This would be similar to the [cmdletbinding()] syntax, but would allow me to write my own.

Re:PowerShell is open sourced and is available on Linux by emodendroket on Thursday August 18, 2016 @12:52PM (#12314150)
Interesting. I'll have to look into it. Thanks.
Re:PowerShell is open sourced and is available on Linux by JeffreySnover on Thursday August 18, 2016 @01:03PM (#12314274)
Our goal is to be incapable of sustained error. :-)

Jeffrey Snover[MSFT]

Re:PowerShell is open sourced and is available on Linux by new_hackers on Thursday August 18, 2016 @12:31PM (#12313939)
insane in a good way right? You aren't "returning" you are emitting to the pipeline
Re:PowerShell is open sourced and is available on Linux by emodendroket on Thursday August 18, 2016 @12:37PM (#12314003)
Not 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.
Re:PowerShell is open sourced and is available on Linux by new_hackers on Thursday August 18, 2016 @12:39PM (#12314021)
ArrayList.Add ... whatever ... | Out-Null

Its not implicitly returning. The return value wasn't assigned so it is emitted to the pipeline.

Re:PowerShell is open sourced and is available on Linux by emodendroket on Thursday August 18, 2016 @12:50PM (#12314134)
OK, 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.
Re:PowerShell is open sourced and is available on Linux by ygra on Thursday August 18, 2016 @02:11PM (#12314973)

    return {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.
Re:PowerShell is open sourced and is available on Linux by emodendroket on Thursday August 18, 2016 @03:10PM (#12315465)
Most people writing PowerShell scripts are likely dabblers though. Anyway, I have to admit I don't really appreciate the whole pipeline idea; I just know that `$something = Some-Function` sometimes wouldn't do what I thought it would because of these semantics.
Re:PowerShell is open sourced and is available on Linux by mschuetz on Thursday August 18, 2016 @12:36PM (#12313998)
Yeah, 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.
Re:PowerShell is open sourced and is available on Linux by ygra on Thursday August 18, 2016 @02:20PM (#12315043)
Usually 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

)
Re:PowerShell is open sourced and is available on Linux by emodendroket on Thursday August 18, 2016 @12:55PM (#12314173)
Couldn't you use $() for this?
Re:PowerShell is open sourced and is available on Linux by Jayku1 on Thursday August 18, 2016 @06:18PM (#12316724)
They fixed this in later versions with the literal: --%

But you can also always use start-process like:

start-process app @'

your arguments and parameters go here

'@

Re:PowerShell is open sourced and is available on Linux by 3minus1 on Thursday August 18, 2016 @02:11PM (#12314968)
I 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.
Re:PowerShell is open sourced and is available on Linux by vicda on Thursday August 18, 2016 @01:24PM (#12314502)
This was the hardest thing to figure out. I have a program just to ensure I'm sending the arguments that I think I am.
Re:PowerShell is open sourced and is available on Linux by vicda on Thursday August 18, 2016 @01:22PM (#12314478)
Yeah, it's a mixture of Perl and Bash with sudden C# calls interspersed.