A point in the right direction (book explanation)

This topic contains 6 replies, has 2 voices, and was last updated by  Ryan Zore 1 week, 6 days ago.

  • Author
    Posts
  • #81133

    Ryan Zore
    Participant

    Hello,

    I am going through Learn Powershell In A Month Of Lunches. So far the book has been easy to follow but has thrown something new in without enough explanation for me to understand. See Below

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

    NOTE I understand what it is doing but not how as there hasnt been an explanation of this type of syntax @{l='computername';e={$_.name}}

    Can someone point me towards a resource on this so I can understand what is going on and how to use it? I have tried googling around but must not be using the right words.

    Thank you in advance.

  • #81136

    Don Jones
    Keymaster

    The help file on Select-Object does explain that syntax, as does the similar Format-Table (you need to read the full help, not just the default short version). You don't mention which edition of the book, or where you are in it, but I'll assume you're in "The Pipeline, Deeper." The section, "When things don't line up: custom properties" explains the use and purpose of this syntax. In the 3rd edition, there's a bullet list on page 113 that actually breaks down the syntax bit by bit.

  • #81139

    Don Jones
    Keymaster

    Oh, and I'm guessing you aren't using the 3rd edition – there was a change in how Get-Hotfix works at some point – the Get-Hotfix example won't work on most computers, now. I'm pretty sure we changed the example in the 3rd edition.

    • #81164

      Ryan Zore
      Participant

      Hi Don,

      First, thank you very much for replying. I am using the 2nd edition of the book and am referring specifically page 109. I used another example in my first post but it is essentially the same point of confusion. There is a short breakdown of what the code is doing, i'm simply not familiar with @{name='samAccountName';expression={$_.login}}. This is the first time this is introduced and I am wanting to better understand exactly what is going on. I dont know what @ is doing or really exactly what an expression is. I did scour the full help files and it gives examples but not an explanation of what is doing what exactly.

      Do you have link to a resource that could possibly help me better understand?

      IN THE EXAMPLE (page 108-109)
      Import-csv .\newusers.csv |
      select-object -property *,
      @{name='samAccountName';expression={$_.login}},
      @{label='Name';expression={$_.login}},
      @{n='Department';e={$_.Dept}}

      Thank you in advance
      Ryan

  • #81167

    Don Jones
    Keymaster

    Yeah, so @{} is a hash table. It consists of key-value pairs, like 'label'='unicorn'. Select-Object and the Format- commands are simply hardcoded to look for hash tables that have specific keys, like label or l, and expression or e.

    An expression is, basically, code that PowerShell can evaluate. "5 -eq 5" is an expression, resulting in $True, for example.

    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-object?view=powershell-5.1 would be the official resource, specifically example 4, but it does assume some base knowledge like what an expression is, and it doesn't go into what a hash table is exactly.

    You see hash tables a lot.

    $props = @{'Property1'='Don'
               'Name'='Jones'
               'Location'='Vegas'}
    New-Object -Type PSObject -Prop $props
    

    In that case, the keys get turned into properties, populated with the corresponding values.

  • #81169

    Ryan Zore
    Participant

    Thanks, I will review this information to get a better understanding.

  • #81190

    Ryan Zore
    Participant

    Thanks again, i've read through the necessary information to understand the subject.

You must be logged in to reply to this topic.