Help! Custom properties and piping

This topic contains 8 replies, has 3 voices, and was last updated by  Leonard Kong 3 years, 6 months ago.

  • Author
    Posts
  • #12963

    Leonard Kong
    Participant

    Hi Guys,

    Pretty new to powershell here and was just learning it using the Morelunches book on Powershell and came upon one of the labs in chapter 9 that got me a bit confused. The commands are supposed to filter out computers on the domain and retrieve all hot-fixes on them. In the lab, it says that the follow command lines are supposed to be equivalent but when I run the second string, it only reports the hotfixes on the local machine... Can anyone shed some light on why this is?

    First command:

    Get-Hotfix -computerName (get-adcomputer -filter * | Select-Object -expand name)
    

    Second command:

    get-adcomputer -filter * | Select-Object @{l='computername';e={$_.name}} | Get-Hotfix
    

    Thanks in advance for all your help!!
    Leonard

  • #12964

    Don Jones
    Keymaster

    There's a kinda-bug in the Get-Hotfix documentation; it turns out it won't accept pipeline input, although the original docs tag it as doing so. So in theory both commands work, but in practice you have to do the first one because the cmdlet doesn't accept pipeline input as advertised.

  • #12966

    Leonard Kong
    Participant

    Ahh. No wonder! That left me scratching my head a bit. That's good to know. Is there any place where I would have been able to find this information on my own or am I pretty much limited to googling "Get-hotfix pipeline input"?

    Also, it hasn't really mentioned it in the book so far, so I figure I'll ask here. In the second command line what does this part of the it do exactly?

    @{l='computername';e={e={$_.name}}

    I know it's general function is to create a custom object with the computername property so that Get-Hotfix (if it worked correctly) would accept the parameter through it's pipeline, but I was hoping you could break down each part of that line so I could understand it better...

    Thanks again for your help!

  • #12967

    Don Jones
    Keymaster

    Oddly, the documentation for Get-Hotfix mentions this anomaly. You'd just have to read the full help file.

    That is a hash table. The technique is actually described in the first Lunches book. In this case, it's being used with Select-Object to create a new property named ComputerName that contains the value from the input object's Name property. Similar examples are in the help file for Select-Object. There's a pretty descriptive breakdown in "Learn PowerShell 3 in a Month of Lunches," as I use the technique pretty extensively in that book. It's also used with Format-Table, for example.

  • #12968

    Leonard Kong
    Participant

    Oh, I guess I should read the -full helps more often.

    For the hash table, what does the "l='computername'" do? I'm assumed it assigned the 'l' variable something but turns out I was incorrect because I tried replacing it with another letter.

    I'm scanning through the select-object file as we speak. Thanks again Don!

  • #12970

    Don Jones
    Keymaster

    "L" isn't a variable.

    Hashtables consist of one or more "key=value" pairs. In this case, two, with the keys being "L" and "E." The Select-Object command is hardcoded to look for specific keys: L, l, n, N, name, and label all define the name of the new property it will create; e or expression defines a script block that will be run to produce the values for the new property. So "l" is short for "label." I've been trying to get better about using "n" or "name" instead, because lowercase L is so easy to mistake for the number 1, but sometimes I fall into my old habit of using "l" for "label." The command will accept either, though.

    A plain old hash table can use whatever you want for keys. In this case, the hash table has to conform to what the Select-Object command is prepared to deal with.

  • #12971

    Leonard Kong
    Participant

    That all made perfect sense. Thanks again for the guidance, it's helped a lot!

  • #12980

    Doug Morrison
    Participant

    Can I jump in here and suggest checking out these videos?

    I watched them at the end of last year and they really helped me out. Looking forward to delving into your Powershell in a month of lunches, Don. Do you touch on AD administration at all or should I grab a copy of Jason's book afterward?

  • #12981

    Leonard Kong
    Participant

    Oh, those videos look great! I'll definitely give them a look. Thanks for the suggestion, Doug.

You must be logged in to reply to this topic.