8 ms·
Objects are discoverable in powershell. Enter the object, and lists the object out as text.
by UK-AL 10y ago
Objects 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.
- jocro 10y agoI spend about half my job inside Powershell and had no idea bout this. Very cool.
- zamalek 10y agoThe 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/cee4582448300c0d404bbff1f5669029 https://gist.github.com/jcdickinson/cee4582448300c0d404bbff1... [2]: https://technet.microsoft.com/en-us/library/ee176860.aspx https://technet.microsoft.com/en-us/library/ee176860.aspx
- 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.