Guest User

Untitled

a guest
Apr 18th, 2019
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Main function
  2. Function GetWin10Key
  3. {
  4.     $Hklm = 2147483650
  5.     $Target = $env:COMPUTERNAME
  6.     $regPath = "Software\Microsoft\Windows NT\CurrentVersion"
  7.     $DigitalID = "DigitalProductId"
  8.     $wmi = [WMIClass]"\\$Target\root\default:stdRegProv"
  9.     #Get registry value
  10.     $Object = $wmi.GetBinaryValue($hklm,$regPath,$DigitalID)
  11.     [Array]$DigitalIDvalue = $Object.uValue
  12.     #If get successed
  13.     If($DigitalIDvalue)
  14.     {
  15.         #Get producnt name and product ID
  16.         $ProductName = (Get-itemproperty -Path "HKLM:Software\Microsoft\Windows NT\CurrentVersion" -Name "ProductName").ProductName
  17.         $ProductID =  (Get-itemproperty -Path "HKLM:Software\Microsoft\Windows NT\CurrentVersion" -Name "ProductId").ProductId
  18.         #Convert binary value to serial number
  19.         $Result = ConvertTokey $DigitalIDvalue
  20.         $OSInfo = (Get-WmiObject "Win32_OperatingSystem"  | select Caption).Caption
  21.         If($OSInfo -match "Windows 10")
  22.         {
  23.             if($Result)
  24.             {
  25.                
  26.                 [string]$value ="ProductName  : $ProductName `r`n" `
  27.                 + "ProductID    : $ProductID `r`n" `
  28.                 + "Installed Key: $Result"
  29.                 $value
  30.                 #Save Windows info to a file
  31.                 $Choice = GetChoice
  32.                 If( $Choice -eq 0 )
  33.                 {  
  34.                     $txtpath = "C:\Users\"+$env:USERNAME+"\Desktop"
  35.                     New-Item -Path $txtpath -Name "WindowsKeyInfo.txt" -Value $value   -ItemType File  -Force | Out-Null
  36.                 }
  37.                 Elseif($Choice -eq 1)
  38.                 {
  39.                     Exit
  40.                 }
  41.             }
  42.             Else
  43.             {
  44.                 Write-Warning "Запускайте скрипт в Windows 10"
  45.             }
  46.         }
  47.         Else
  48.         {
  49.             Write-Warning "Запускайте скрипт в Windows 10"
  50.         }
  51.        
  52.     }
  53.     Else
  54.     {
  55.         Write-Warning "Возникла ошибка, не удалось получить ключ"
  56.     }
  57.  
  58. }
  59. #Get user choice
  60. Function GetChoice
  61. {
  62.     $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes",""
  63.     $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No",""
  64.     $choices = [System.Management.Automation.Host.ChoiceDescription[]]($yes,$no)
  65.     $caption = "Подтверждение"
  66.     $message = "Сохранить ключ в текстовый файл?"
  67.     $result = $Host.UI.PromptForChoice($caption,$message,$choices,0)
  68.     $result
  69. }
  70. #Convert binary to serial number
  71. Function ConvertToKey($Key)
  72. {
  73.     $Keyoffset = 52
  74.     $isWin10 = [int]($Key[66]/6) -band 1
  75.     $HF7 = 0xF7
  76.     $Key[66] = ($Key[66] -band $HF7) -bOr (($isWin10 -band 2) * 4)
  77.     $i = 24
  78.     [String]$Chars = "BCDFGHJKMPQRTVWXY2346789"
  79.     do
  80.     {
  81.         $Cur = 0
  82.         $X = 14
  83.         Do
  84.         {
  85.             $Cur = $Cur * 256    
  86.             $Cur = $Key[$X + $Keyoffset] + $Cur
  87.             $Key[$X + $Keyoffset] = [math]::Floor([double]($Cur/24))
  88.             $Cur = $Cur % 24
  89.             $X = $X - 1
  90.         }while($X -ge 0)
  91.         $i = $i- 1
  92.         $KeyOutput = $Chars.SubString($Cur,1) + $KeyOutput
  93.         $last = $Cur
  94.     }while($i -ge 0)
  95.    
  96.     $Keypart1 = $KeyOutput.SubString(1,$last)
  97.     $Keypart2 = $KeyOutput.Substring(1,$KeyOutput.length-1)
  98.     if($last -eq 0 )
  99.     {
  100.         $KeyOutput = "N" + $Keypart2
  101.     }
  102.     else
  103.     {
  104.         $KeyOutput = $Keypart2.Insert($Keypart2.IndexOf($Keypart1)+$Keypart1.length,"N")
  105.     }
  106.     $a = $KeyOutput.Substring(0,5)
  107.     $b = $KeyOutput.substring(5,5)
  108.     $c = $KeyOutput.substring(10,5)
  109.     $d = $KeyOutput.substring(15,5)
  110.     $e = $KeyOutput.substring(20,5)
  111.     $keyproduct = $a + "-" + $b + "-"+ $c + "-"+ $d + "-"+ $e
  112.     $keyproduct
  113.    
  114.  
  115. }
  116. GetWin10Key
Add Comment
Please, Sign In to add comment