Live data from Hacker News

In defense of PowerShell

benwilson.me

71–80 of 82 posts

Re: In defense of PowerShell

#71
post #15

Earlier quoted context omitted.

As a Unix person, 'Restart-Service' seems a lot easier to remember than 'systemctl'. Edit: Restart-Service is also easier to remember than /etc/init.d was. Edit 2 (rate limit): `service` on RHEL is great, but it's a Red Hat-ism. Which is fine, I used to work at Red Hat, but still. It's also pretty limited: you use service to start/stop/restart, but you still use systemctl to list services, or list /etc/init.d (techni…

Regarding your edit: service works just fine on ubuntu as well

As do stop, start, and restart (as long as you have /sbin on your path)

Re: In defense of PowerShell

#72

The tab completion in powershell is frustrating to me. I never want to cycle through every available option (especially when many PSH commands have literally hundreds of switches). I want the tab-completion to only show options for which I've started typing the prefix. I want double-tab to print a listing of said options so that I can see them all at once. Is this possible? Right now it feels like there's a major lac…

Agreed regarding the default tab completion. You might check out PSReadLine at some point - see https://github.com/lzybkr/PSReadLine. Its goal is to try to emulate readline except in PowerShell (this includes tab completion as well as emacs shortcut keys... vim shortcut keys are in the works AFAIK). It does require some additional configuration and it isn't installed by default, but I think it helps with this specific scenario quite a bit.

Re: In defense of PowerShell

#73
post #52
post #11

I dislike windows (and by extension, powershell) because a solution such as changing the port the webserver runs on looks something like: New-ItemProperty IIS:\sites\DemoSite -name bindings -value @{protocol="http";bindingInformation=":8081:"} http://www.iis.net/learn/manage/powershell/powershell-snap-i... What did I just modify? Is it persisted to the hard disk? How do i back that up? How do I revert the change? How…

So how would you accomplish the same task on Linux using bash? What script would you write to set the web server's port to 1234?

With conventional Linux distributions it is tricky to do this in a clean and idempotent way while preserving the entirety of the existing server configuration. The best solution of which I am aware is Augeas, which supports multiple file formats, and tools of similar type that specialize in a single file format (e.g., jq).

What these generally do is parse the entire configuration file into an abstract syntax tree, apply the modifications you have asked for to the tree, then serialize the result back into the configuration file format. This ensures that the input and the output are both at least syntactically valid; as a result it is less error-prone than sed/awk. Unlike with configuration file templates (e.g., Ansible's Jinja2 templates) the changes can be applied on top of the distribution's default configuration if desirable and reapplied when that default configuration is updated.

Re: In defense of PowerShell

#74

The tab completion in powershell is frustrating to me. I never want to cycle through every available option (especially when many PSH commands have literally hundreds of switches). I want the tab-completion to only show options for which I've started typing the prefix. I want double-tab to print a listing of said options so that I can see them all at once. Is this possible? Right now it feels like there's a major lac…

> I want double-tab to print a listing of said options so that I can see them all at once. Is this possible?

In PowerShell 5 (at least in Windows 10), Ctrl-Space yields a list of matching completions. You can then use the arrow keys (ip, down, left, right) to pick one and hit enter or space to select.

It works for commands as well as for options/parameters and parameter values.

I.e. you can type get-pr[ctrl-space] and powershell responds with:

    Get-PrintConfiguration      Get-PrinterDriver           Get-PrinterProperty         Get-Process
    Get-Printer                 Get-PrinterPort             Get-PrintJob                Get-ProvisionedAppxPackage
Choose Get-Process (right,right,right,space). Now the cmdline shows

    Get-Process _
Hit [-][ctrl-space]. Powershell now shows:

    PS C:\Users\zaphod> Get-Process -Name
    Name                 IncludeUserName      FileVersionInfo      ErrorAction          ErrorVariable        OutVariable
    Id                   ComputerName         Verbose              WarningAction        WarningVariable      OutBuffer
    InputObject          Module               Debug                InformationAction    InformationVariable  PipelineVariable
Pick "Name" and hit space:

    PS C:\Users\zaphod> Get-Process -Name _
Now hit [ctrl-space] again. PowerShell now completes on running processes on the system, offering the list of all of the process names.

Re: In defense of PowerShell

#75

The tab completion in powershell is frustrating to me. I never want to cycle through every available option (especially when many PSH commands have literally hundreds of switches). I want the tab-completion to only show options for which I've started typing the prefix. I want double-tab to print a listing of said options so that I can see them all at once. Is this possible? Right now it feels like there's a major lac…

The usual PowerShell guru's response to your complaint is 'This is what PowerShell ISE is for'. Personally I don't think this is elegant, but that's what I usually encounter in the wild.

For what it's worth, Windows 10 enhances the basic command host to support some more things such as nicer tab completion directly out of the box (for both traditional cmd.exe and PowerShell) without having to switch to ISE.

Re: In defense of PowerShell

#76
post #51

I started reading this article not knowing anything about powershell, and finished horrified by the issues he tried to dispel in it. No remoting, no scripting? Are they serious?

You didn't read deep enough. Remoting and Scripting are off by default but available. Remoting is not great, but getting better soon (as in now-ish) with SSH support getting baked into PowerShell. (Yeah, that SSH, OpenSSH.) Scripting IS great, you just have to turn it on. (It's off by default because Microsoft didn't want it to be a virus vector for your Grandfather's machine who doesn't need shell scripting and likes to click on every attachment he sees in his email.)

Get-Help about_Execution_Policies # Tells you everything you need to know about

So the first thing I typically do in PowerShell in user accounts I control is:

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

After that I can run all the scripts I want to run. (From there it's a quick jump over to Chocolatey for application installs and PsGet for useful PowerShell scripts like PoshGit that adds your friendly Git branch info to the PowerShell prompt.)

Re: In defense of PowerShell

#77
post #48
post #7

I work primarily in a Microsoft environment, although I have Linux shell experience as well, and I totally don't get Powershell. What is a cmdlet? Why does it need a stupid made-up name like that? Why are all the commands incredibly verbose and hard to remember? I avoid using it at every opportunity.

A cmdlet is a PowerShell native command. It reads and writes objects, where as Unix commands read and write plain text. Try this in PowerShell: get-command get-command | where CommandType -eq "alias" Notice the lack of parsing. In Unix you'd typically do this kind of thing with grep, but then you'd have to watch out that you don't accidently include the wrong rows because some other column happened to contain the str…

PowerGUI is a godsend. Sure PowerShell ISE ships for free, but PowerGUI is a much more capable editor/debugger. It's free as well.

Re: In defense of PowerShell

#78
post #11

I dislike windows (and by extension, powershell) because a solution such as changing the port the webserver runs on looks something like: New-ItemProperty IIS:\sites\DemoSite -name bindings -value @{protocol="http";bindingInformation=":8081:"} http://www.iis.net/learn/manage/powershell/powershell-snap-i... What did I just modify? Is it persisted to the hard disk? How do i back that up? How do I revert the change? How…

Why the colons surrounding the port !?

Basically you're modifying a string that consists of:

   IP Address:Port:Virtual Host
For example:

   172.16.1.1:80:www.example.com
The raw values reside in an XML config file called "applicationHost.config" which replaces the old school and rather opaque IIS6 metabase.

Admittedly that example is a bit old school, there's better commandlets available now that wrap having to manipulate these string values directly. e.g. the New-WebBinding and Set-WebBinding examples shown in sibling comments in this sub-thread.

A site config looks like:

  
    
       
    
    
      
      
      
    
   
None of this is particularly mysterious, and the documentation is pretty good.

Re: In defense of PowerShell

#80
post #69

Earlier quoted context omitted.

Or /etc/rc.d/apache2 restart, as one would find in the BSD world (and Slackware, with a bit of modification). This isn't to mention that it should be relatively trivial to map "restart-service $foo" to "/etc/rc.d/$foo restart" or "systemctl $foo.service restart" or whatever if one so chooses.

FreeBSD uses the service command these days too.

Good to know. In that case, "BSD Land minus FreeBSD and possibly its descendants".
Post reply on HN