Ezeq
Ezeq

Reputation: 9

powershell square brackets meaning

I'm transforming a script, but I not sure what the following means:

$valueData = (Get-ItemProperty $key).digitalproductid[52..66]

Is $valueData storing the values from 52 to 66, or is it storing the the 52th through the 66th value?

The long explanation: I'm trying to get the activation keys from Microsoft office, that is resting in "dead" PCs. I have accessed the reg and copied the encrypted value. I have a .txt with:

"DigitalProductID"=hex:a4,00,00,aa,...

Now I need to parse it to $valueData so it can processed by this script:

<# this part was hand made 
function Search-RegistryKeyValues {
  param(
    [string]$path,
    [string]$valueName
  )
  Get-ChildItem $path -recurse -ea SilentlyContinue | % {
    if ((Get-ItemProperty -Path $_.PsPath -ea SilentlyContinue) -match $valueName) {
      $_.PsPath
    }
  }
}

# find registry key that has value "digitalproductid"
# 32-bit versions
$key = Search-RegistryKeyValues "hklm:\software\microsoft\office" "digitalproductid"
if ($key -eq $null) {
  # 64-bit versions
  $key = Search-RegistryKeyValues "hklm:\software\Wow6432Node\microsoft\office" "digitalproductid"
  if ($key -eq $null) {Write-Host "MS Office is not installed."}
}
#end of hand made search #>
#begins doubt:
$valueData = (Get-ItemProperty $key).digitalproductid[52..66]

# decrypt base24 encoded binary data
$productKey = ""
$chars = "BCDFGHJKMPQRTVWXY2346789"
for ($i = 24; $i -ge 0; $i--) {
  $r = 0
  for ($j = 14; $j -ge 0; $j--) {
    $r = ($r * 256) -bxor $valueData[$j]
    $valueData[$j] = [math]::Truncate($r / 24)
    $r = $r % 24
  }
  $productKey = $chars[$r] + $productKey
  if (($i % 5) -eq 0 -and $i -ne 0) {
    $productKey = "-" + $productKey
  }
}

Write-Host "MS Office Product Key:" $productKey

Upvotes: 0

Views: 2177

Answers (1)

user4003407
user4003407

Reputation: 22122

Since indexing starts from zero, it returns elements from 53th to 67th.

You have two independent operations here:

  • Range operation 52..66 which return array with integers from 52 to 66.
  • Indexing operations [] which can accept array of indexes, it that case it return array of corresponding elements

    $Array=write 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th # array with ten elements
    
    $a=1,5,2,7
    $b=2..6 # equivalent to 2,3,4,5,6.
    
    $Array[$a] # returns 2nd, 6th, 3rd, 8th.
    $Array[$b] # returns 3rd, 4th, 5th, 6th, 7th.
    

Upvotes: 3

Related Questions