I found a vbs script that reads a registry entry and converts the value to text for currently used Windows key. here’s the code
Set WshShell = CreateObject("WScript.Shell") MsgBox ConvertToKey(WshShell.RegRead("HKLMSOFTWAREMicrosoftWindows NTCurrentVersionDigitalProductId")) Function ConvertToKey(Key) Const KeyOffset = 52 i = 28 Chars = "BCDFGHJKMPQRTVWXY2346789" Do Cur = 0 x = 14 Do Cur = Cur * 256 Cur = Key(x + KeyOffset) + Cur Key(x + KeyOffset) = (Cur 24) And 255 Cur = Cur Mod 24 x = x -1 Loop While x >= 0 i = i -1 KeyOutput = Mid(Chars, Cur + 1, 1) & KeyOutput If (((29 - i) Mod 6) = 0) And (i <> -1) Then i = i -1 KeyOutput = "-" & KeyOutput End If Loop While i >= 0 ConvertToKey = KeyOutput End Function
I decided to try and convert it to powershell (because why not?). I’m just stuck on a few lines in the middle that I’ve been unable to find what the operations in vbs mean. Here is my current code:
function Convert-Key { param([System.Byte[]]$keyvalue) # static values $keyoffset = 52 $i = 28 $chars = "BCDFGHJKMPQRTVWXY2346789" Do { $curr = 0 $x = 14 Do { $curr *= 256 $curr += $keyvalue[$x + $keyoffset] #Key(x + KeyOffset) = (Cur 24) And 255 $keyvalue[$x + $keyoffset] = ($curr/24) -and 255 $curr %= 24 $x-- } while ($x -ge 0) $i-- #KeyOutput = Mid(Chars, Cur + 1, 1) & KeyOutput $keyoutput = $chars.Substring($curr+1,1) + $keyoutput #If (((29 - i) Mod 6) = 0) And (i <> -1) Then if ((((29 - $i) % 6) -eq 0) -and ($i -ne -1)) { $i-- $keyoutput = "-" + $keyoutput } } while ($i -ge 0) return $keyoutput } $key = 'HKLM:SOFTWAREMicrosoftWindows NTCurrentVersion' $value = (Get-ItemProperty -Path $key -Name DigitalProductID).DigitalProductID $keytext = Convert-Key $value | Out-Host
so I’ve left the original vbs line as a comment to reference above the corresponding powershell line. any pointers on what “cur 24” and “i <> -1” means? I’m also not sure of the substring index offset. it seems like substring is 0 indexed but mid is not. right now it outputs the correct format but does not give the same/correct key output as the vbs script.
submitted by /u/epsiblivion
[link] [comments]
The post Help with converting vbs to powershell appeared first on How to Code .NET.