13 ms·
Is this really so useful in practice? I have never used PowerShell before (nor do I have plans to do so in the near future), but I would imagine the downside is
by amenod 10y ago
Is this really so useful in practice? I have never used PowerShell before (nor do I have plans to do so in the near future), but I would imagine the downside is complexity, both for user and especially for developers. With text you just read and parse, while with objects you need to know the object type, its fields,... Whatever happend, I hope text stays here (and I have a bad feeling about it ever since I met systemd).
- UK-AL 10y agoObjects are discoverable in powershell. Enter the object, and lists the object out as text.
- vvanders 10y agoYes, this is so awesome. Can't remember the exact parameter/field? Just dump it to the console.
- UK-AL 10y agoThat what you would do with text? If you have no idea what its outputting, explore it by putting it to the console.
- vvanders 10y agoSure, but with text you might have empty fields that are just omitted. With Powershell you can see that the field exists but is empty. There's a bunch of subtle distinctions like that which don't sound impressive but end up being really nice when working with it.
- WorldMaker 10y agoWhat you can't do so easily with text is also just auto-convert that object directly to JSON or YAML or CSV, for that matter, and forward that easily into your favorite JSON/YAML/CSV exploration tool.
- amenod 10y agoOk, that sounds good. But what if some developer decided that some field would have prefix "ABC" in all values, then the value I am interested in, then some value I am not interested in at the end? (you say this can't happen? Clue: WMI.) Can I take the value, convert it to string, make some magic on it and convert it to another object entirely? I don't know, I never had any problems with text parsing. I love strong types in programming languages, but in system administration I think it would only get in the way. But as I said, never tried it, so I might be missing something.
- ZenoArrow 10y ago> "But what if some developer decided that some field would have prefix "ABC" in all values, then the value I am interested in, then some value I am not interested in at the end?" I don't see why this would be a problem for PowerShell. Just use Select-String to strip away the characters you're not interested in. The advantages that derive from passing around objects aren't based on all data being automatically in the format you want.
- CodexArcanum 10y agoYep! There's a few ways to accomplish such a thing, but probably the most straight forward would be the Foreach-Object command (aliased as "%") to do a mapping from source object to something else. Free form objects/dictionaries are easy to make and consume.
- new_hackers 10y agoSelect-Object also makes it very, very easy to make new objects
- dsp1234 10y agoThere are many ways to accomplish this (in general). My favorite is that you can create 'computed' properties on the fly which means you can continue to keep the strongly typed object in the pipeline, and still have access to the data you care about. https://technet.microsoft.com/en-us/library/ff730948.aspx https://technet.microsoft.com/en-us/library/ff730948.aspx You use the exact same functions (first, last, substring, replace, etc) as you would normally. Then the rest of the pipeline has it available if needed.
- dsp1234 10y agoMore specifically, you can find out all of the type information about anything you get access to by piping it to 'get-member' get-netadapter | get-member Which gives the following output http://pastebin.com/bM0cBeXb http://pastebin.com/bM0cBeXb about the type information for the result of 'get-netadapter'. In addition to this, tab completion for properties is available. Alternately, you can get all of the current properties of any existing object by piping to 'select' (to get a general subset) or 'select * ' to get all properties. Note that this gives all information for the objects in the pipeline. get-netadapter | select get-netadapter | select * The first will provide a short list of fields for all network adapters, and the second will provide all properties. 'Select' can also be used to easily filter the properties of items in the pipeline to ones you care about. get-netadapter | select Name, MacAddress You can also just generally filter the pipeline based on generic filters (give me all of the network adapters that are currently not enabled, and have a driver provided by Mellanox). And ultimately, if you want to use powershell to do the stuff that typed objects are good at, and then leverage command line tools to do the rest, you can if you like. get-netadapter | select Name, MacAddress | exportto-csv Which will take all of the network adapters, gather their name and mac address, and spit it out to stdout as CSV which can then be sent to any number of external tools, or using the '-expandproperty' flag to 'select' can give just a bare list of properties one on each line.
- philsnow 10y ago> With text you just read and parse, while with objects you need to know the object type, its fields parsing is actually a huge pain in general. a lot of unixy tools produce different output when their stdout is not a terminal to be more pipeline-friendly. That output is generally easier to parse (often tab-separated or so), but you still need to know the same things about the output (the "type" could be whether it came from `ps` or from `ls`, and the fields are just in some static order that was usually chosen decades ago, and if it's extensible it's cumbersome). If `ps`, `ls`, etc produced typed output, I would be sooo happy. When I worked at google I thought about what it would be like to have a unix userland where all the parts communicate with protobufs instead of raw text. There are some problems (like portability of message definitions, which was a solved problem within google, but which is harder when you have multiple parties/entities/companies contributing message definitions). edit: accidentally a word
- Klathmon 10y agoI wrote a bash script to sort processes by age on an old redhat 4 machine. What sounded like a very simple thing, quickly ended up being a complete nightmare of bash, python, and an unholy bunch of linux commands. It was ugly, and a terrible way of doing it, but we got it working. It would have been amazing if i could pipe PS into something that could sort by the dates on the 4th column, and return the last column in the sorted order.
- PeCaN 10y agoYou could probably hack something together with awk, but this sort of thing is exactly why I like powershell a lot.
- Klathmon 10y agoYeah i ended up using awk, some python for the date parsing and sorting, back to bash for grep and another thing i can't remember, then into the program that actually needed the data. It was a mess, and I remember thinking it would be such a simple thing that was so much more complicated than i thought it would be.
- PeCaN 10y agoI find it massively useful in practice, even for mundane tasks. For example, the other day I was trying to search the columns of a very large SQL Server table¹ but also needed information about the column type. …\dbo.site> ls Columns | select Name, DataType, {$_.Properties['Length'].Value} | where {$_.Name -like '*int*'} Name DataType $_.Properties['Length'].Value ---- -------- ----------------------------- internal_end datetime 8 internal_start datetime 8 interval_code varchar 5 prod_intv_start int 4 prod_intv_stop int 4 working_int decimal 5 Quick, easy. In a *nix shell that would be very tricky. ¹ SQL Server installs a Powershell extension so that you can essentially treat databases like a fancy object filesystem. It's wicked cool actually.
- schoen 10y ago> Quick, easy. In a *nix shell that would be very tricky. You'd probably need to make an explicit SQL query since you wouldn't have the database integration.
- PeCaN 10y agoWell, yeah, but I mean the concept (grepping in tabular data). If, hypothetically, bash had database integration, ls Columns would probably return something like foo varchar 30 NOT NULL bar decimal 8 baz int 4 Which would be a royal PITA to search for anything in—if I were to search for ‘int’ I'd get all int columns and any columns with int in the name…. I could've used an example involving process lists, but I didn't want to trigger PTSD in anyone who's had to grep the output of ‘ps’ for anything non-trivial. (Plus, the database thing was something I actually did a few days ago.)
- stepvhen 10y agoBut thats why there are tools like awk.
- djsumdog 10y agoI've done some Powershell work in my brief time as a Windows sys-admin. I personally struggled with it. The object interaction can be complex and the naming feels less scriptable and more directly out of the C# world. That being said, I can see the appeal and I know people who love it. I have seen some amazing shit written in it, like this security tool that was just open sourced this year: https://github.com/adaptivethreat/bloodhound https://github.com/adaptivethreat/bloodhound I have used Mono/GTK# for years on Linux and prefer it over other GTK wrappers. Powershell might be worth a try on Linux, but it will take time to build up the libraries and wrapper needed to interface with most of the Linux subsystems (unless Microsoft as ported a lot of useful stuff in this initial release). People don't (or shouldn't) use bash/zsh/fish for really complicated stuff anyway. I have a feeling most sys admins/devops people will continue to use Python/Ruby for complicated tasks; and they both have a ton of libraries that help avoid the stdin/out/parsing situation you have with chaining together regular shell scripts.
- qwertyuiop924 10y agoPython and ruby for the big stuff, bash and the Holy Trinity (sed, grep, and find) for the small stuff. AWK for anything in between.
- iso-8859-1 10y agoYou can just use the Mono bindings for the Linux-specific API's, I hope.
- ktRolster 10y agoIt's nice when it works, but it's a pain to write a command-line utility that outputs into objects. Whereas in Unix, everything automatically outputs into text, so you have to work hard to prevent it from being chainable to the system. There are also details of Powershell that are very annoying, such as the lack of < redirect, and the difficulties it has on binary output sometimes.
- new_hackers 10y agodifficulties with jumping through hoops to consume and produce binary output are really the only "problem" I've had with powershell
- emodendroket 10y agoYes, it's quite useful in practice, because the alternative is knowing exactly the format of text some command is going to produce and munging it, which is hardly better
- noveltyaccount 10y agoPipe anything to Get-Member. For example: (ls)[0] | gm will list every field and its type on a File object. Wicked easy.
- ygra 10y agoYou can even just do ls|gm and get the lists for both FileInfo and DirectoryInfo. There will be no duplicate types listed because Get-Member already sees to that there won't.
- deleted 10y ago[deleted]
- ksk 10y agoCould you elaborate on what the actual complexity is, rather than what you imagine? For a developer writing a tool, exposing an interface as objects is far easier than having to constantly serialize data into a string. Keep in mind that once you serialize data into a string, you can never change the order of the data or else other tools will break. Not so with a object based interface. In other system-admin type tasks PowerShell hides complexity. For e.g. finding the IP address of a network interface is simply Get-NetIPAddress | select IPAddress compared to something horrendous like ifconfig | perl -nle 's/dr:(\S+)/print $1/e'
- se6 10y agoWhat about: ip addr show | grep inet Edit: wrote back original command and replied below instead
- ksk 10y agoThat doesn't work on my CentOS box. [kapil@localhost ~]$ ip addr show | grep inet inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host inet 192.168.128.236/24 brd 192.168.128.255 scope global dynamic eth0 inet6 fe80::215:5dff:fe80:501/64 scope link [kapil@localhost ~]$
- se6 10y agoCorrection then: ip addr | grep "inet " | cut -f 3 -d " "
- ksk 10y agoIt still doesn't work (you forgot to add the tr back in your reply after your edit) [kapil@localhost ~]$ ip addr | grep "inet " | tr -s " " | cut -f 3 -d " " 127.0.0.1/8 192.168.128.236/24 [kapil@localhost ~]$ To be honest, I don't consider this to be that bad, I was just replying to a person who thought that bash was "simple", and powershell was "complex". Also, I don't have a problem with either tool. I'm very open to the idea that Microsoft can and does create awesome tech, despite having other shitty products.
- IshKebab 10y agoIt's not about complexity; it's about robustness, and avoiding insane hacks like this: REASON=$(echo "$line"|tr ' ' '\n'|grep reason= |cut -d= -f2)