powershell方括号含义

时间:2015-07-04 09:42:13

标签: powershell syntax square-bracket

我正在转换脚本,但我不确定以下含义:

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

$valueData是否存储52到66之间的值,还是存储第52到第66个值?

长长的解释:我试图从微软办公室获取激活密钥,这是在"死了"电脑。我已经访问了reg并复制了加密值。我有一个.txt:

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

现在我需要将其解析为$valueData,以便它可以由此脚本处理:

<# 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

1 个答案:

答案 0 :(得分:3)

由于索引从零开始,因此它返回从第53位到第67位的元素。

您在这里有两个独立的操作:

  • 范围操作52..66,返回数组,整数为52到66。
  • 可以接受索引数组的索引操作[],它会返回相应元素的数组

    $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.