0

currently I watch the course about IT support and encountered this script:

$FILE = $GET_HTML.Links | Select-Object @{Label='href';
Expression={@{$true=$_.href}[$_.href.EndsWith('win64.exe')]}} |
Select-Object -ExpandProperty href

I figured out that we somehow use hashtables to filter out the desired link but I want to know how it works in detail. Would appreciate your help.

mklement0
  • 382,024
  • 64
  • 607
  • 775
Mr. 13
  • 49
  • 5
  • 1
    This script won't do anything as long as `$GET_HTML` isn't defined :) – Mathias R. Jessen Feb 08 '23 at 17:04
  • 2
    [`Select-Object`, Example 11: Create calculated properties for each `InputObject`](https://learn.microsoft.com/en-gb/powershell/module/Microsoft.PowerShell.Utility/Select-Object?view=powershell-7.3#example-11-create-calculated-properties-for-each-inputobject) and [about_Calculated_Properties](https://learn.microsoft.com/en-gb/powershell/module/microsoft.powershell.core/about/about_calculated_properties)… – JosefZ Feb 08 '23 at 17:13
  • 1
    The code lists the URLs of all links from the given web page (propably obtained using `Invoke-WebRequest`) that end with `win64.exe`, in a pretty complicated way. An easier way might be: `$html.links.href -like '*win64.exe'` – zett42 Feb 08 '23 at 17:32

1 Answers1

0

@{$true=$_.href} is a hashtable, and [$_.href.EndsWith('win64.exe')] picks the hashtable element that has a true or false key, but there is no false key entry.

@{$true = 'foowin64.exe'}

Name                           Value
----                           -----
True                           foowin64.exe


@{$true = 'foowin64.exe'}[$true]

foowin64.exe


@{$true = 'foowin64.exe'}[$false] -eq $null

True
js2010
  • 23,033
  • 6
  • 64
  • 66