News for hackers. Stuff that matters.
Re:PowerShell is open sourced and is available on Linux by UK-AL on Thursday August 18, 2016 @11:49AM (#12313433)
Objects are discoverable in powershell. Enter the object, and lists the object out as text.
Re:PowerShell is open sourced and is available on Linux by dsp1234 on Thursday August 18, 2016 @12:10PM (#12313690)
More 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 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.
Re:PowerShell is open sourced and is available on Linux by vvanders on Thursday August 18, 2016 @11:53AM (#12313473)
Yes, this is so awesome. Can't remember the exact parameter/field? Just dump it to the console.
Re:PowerShell is open sourced and is available on Linux by UK-AL on Thursday August 18, 2016 @12:05PM (#12313623)
That what you would do with text? If you have no idea what its outputting, explore it by putting it to the console.
Re:PowerShell is open sourced and is available on Linux by vvanders on Thursday August 18, 2016 @12:08PM (#12313657)
Sure, 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.

Re:PowerShell is open sourced and is available on Linux by WorldMaker on Thursday August 18, 2016 @12:21PM (#12313814)
What 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.
Re:PowerShell is open sourced and is available on Linux by amenod on Thursday August 18, 2016 @11:57AM (#12313508)
Ok, 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.

Re:PowerShell is open sourced and is available on Linux by zamalek on Thursday August 18, 2016 @02:16PM (#12315010)
The default regex functionality is absolutely terrible (maybe this can now be fixed with a PR). You'd write your own cmdlet[1]. A few things are demonstrated in the cmdlet: functions that take pipes, functions that return pipes, anonymous functions, actions to perform before processing a pipe and dynamic objects. The Powershell syntax gets ridiculed for being ugly, but it's beautifully expressive.

> Can I take the value, convert it to string, make some magic on it and convert it to another object entirely?

    Get-ChildItem *.js | Get-Content | Select-Regex 'require\("(?<Require>.*?)"\)' | Select-Object -ExpandProperty Require
In other words: find JS files | read lines | turn regex capture groups into an object (another object entirely) | select a single capture

Ultimately, Microsoft provides WMI cmdlets[2]. You'd have strong types from the get-go and wouldn't need resort to this string silliness.

[1]: https://gist.github.com/jcdickinson/cee4582448300c0d404bbff1... [2]: https://technet.microsoft.com/en-us/library/ee176860.aspx

Re:PowerShell is open sourced and is available on Linux by jodrellblank on Friday August 19, 2016 @03:24AM (#12318450)
The default regex functionality is absolutely terrible

How so?

Re:PowerShell is open sourced and is available on Linux by zamalek on Friday August 19, 2016 @05:48AM (#12318930)
Replicate the above without using [Regex]:: (i.e. use -matches and $matches). It forces you to radically depart from a typical Powershell mindset and approach.
Re:PowerShell is open sourced and is available on Linux by jodrellblank on Friday August 19, 2016 @04:33PM (#12323258)
I'm not certain what you're getting at; -match doesn't handle multiple matches so you can't replicate the above just with it? Yes, that's a bit annoying. You could match line by line:

    gci *.js | foreach { gc $_ | foreach { if ($_ -match ..) { $Matches... 
which is ugly, but doesn't use [regex]::Matches. But you can do:

    sls 'require\("(.*?)"\)' *.js | select { $_.Matches.groups[1] }
or other variants of Select-String, depending on exactly what data you want, without departing into the .Net Framework too far.
Re:PowerShell is open sourced and is available on Linux by ZenoArrow on Thursday August 18, 2016 @12:05PM (#12313601)
> "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.

Re:PowerShell is open sourced and is available on Linux by dsp1234 on Thursday August 18, 2016 @12:36PM (#12313991)
There 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

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.

Re:PowerShell is open sourced and is available on Linux by jocro on Thursday August 18, 2016 @08:05PM (#12317153)
I spend about half my job inside Powershell and had no idea bout this. Very cool.
Re:PowerShell is open sourced and is available on Linux by CodexArcanum on Thursday August 18, 2016 @12:07PM (#12313652)
Yep! 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.
Re:PowerShell is open sourced and is available on Linux by new_hackers on Thursday August 18, 2016 @12:34PM (#12313974)
Select-Object also makes it very, very easy to make new objects