Advertisement
Stewie410

mskeys.vbs

Feb 2nd, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.84 KB | None | 0 0
  1. Const HKLM = &H80000002
  2.  
  3. dim fso: set fso = CreateObject("Scripting.FileSystemObject")
  4. dim CurrentDirectory
  5. CurrentDirectory = fso.GetAbsolutePathName(".")
  6.  
  7. ' Create the wshNetwork object to pull username
  8. Set objNetwork = CreateObject("Wscript.Network")
  9.  
  10. ' Place keys in $CURRENT_DIR\OfficeKeys.txt == default
  11. Set objFileToWrite = CreateObject("Scripting.FileSystemObject").OpenTextFile(CurrentDirectory & "\" & objNetwork.UserName & "_OfficeKeys.txt",8,True)
  12.  
  13.  
  14.  
  15. ' Place keys in $CURRENT_USER\Desktop\OfficeKeys.txt
  16. 'Set objFileToWrite = CreateObject("Scripting.FileSystemObject").OpenTextFile(CreateObject("WScript.Shell").ExpandEnvironmentStrings("%UserProfile%") & "\Desktop\OfficeKeys.txt",8,True)
  17.  
  18. 'wscript.echo "View Product Keys | Microsoft Products" & vbCrLf
  19.  
  20. 'Install Date
  21. Computer = "."
  22. Set objWMIService = GetObject("winmgmts:\\" & Computer & "\root\cimv2")
  23. Set Obj = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
  24.  
  25. dim InsDate
  26.  
  27. For Each item in Obj
  28. InsDate = item.InstallDate
  29. ' Gather Operating System Information
  30. Caption = Item.Caption
  31. OSArchitecture = Item.OSArchitecture
  32. CSDVersion = Item.CSDVersion
  33. Version = Item.Version
  34. Next
  35.  
  36. dim NewDate
  37.  
  38. NewDate = mid(InsDate,9,2) & ":" & mid(InsDate,11,2) & ":" & mid(InsDate,13,2)
  39. NewDate = NewDate & " " & mid(InsDate,7,2) & "/" & mid(InsDate,5,2) & "/" & mid(InsDate,1,4)
  40.  
  41. QueryWindowsProductKeys()
  42.  
  43. 'wscript.echo 'vbCrLf & "Office Keys" & vbCrLf
  44.  
  45. QueryOfficeProductKeys()
  46.  
  47. Function DecodeProductKey(arrKey, intKeyOffset)
  48. If Not IsArray(arrKey) Then Exit Function
  49. intIsWin8 = BitShiftRight(arrKey(intKeyOffset + 14),3) And 1
  50. arrKey(intKeyOffset + 14) = arrKey(intKeyOffset + 14) And 247 Or BitShiftLeft(intIsWin8 And 2,2)
  51. i = 24
  52. strChars = "BCDFGHJKMPQRTVWXY2346789"
  53. strKeyOutput = ""
  54. While i > -1
  55. intCur = 0
  56. intX = 14
  57. While intX > -1
  58. intCur = BitShiftLeft(intCur,8)
  59. intCur = arrKey(intX + intKeyOffset) + intCur
  60. arrKey(intX + intKeyOffset) = Int(intCur / 24)
  61. intCur = intCur Mod 24
  62. intX = intX - 1
  63. Wend
  64. i = i - 1
  65. strKeyOutput = Mid(strChars,intCur + 1,1) & strKeyOutput
  66. intLast = intCur
  67. Wend
  68. If intIsWin8 = 1 Then
  69. strKeyOutput = Mid(strKeyOutput,2,intLast) & "N" & Right(strKeyOutput,Len(strKeyOutput) - (intLast + 1))
  70. End If
  71. strKeyGUIDOutput = Mid(strKeyOutput,1,5) & "-" & Mid(strKeyOutput,6,5) & "-" & Mid(strKeyOutput,11,5) & "-" & Mid(strKeyOutput,16,5) & "-" & Mid(strKeyOutput,21,5)
  72. DecodeProductKey = strKeyGUIDOutput
  73. End Function
  74.  
  75. Function RegReadBinary(strRegPath,strRegValue)
  76. Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
  77. objReg.GetBinaryValue HKLM,strRegPath,strRegValue,arrRegBinaryData
  78. RegReadBinary = arrRegBinaryData
  79. Set objReg = Nothing
  80. End Function
  81.  
  82. Function BitShiftLeft(intValue,intShift)
  83. BitShiftLeft = intValue * 2 ^ intShift
  84. End Function
  85.  
  86. Function BitShiftRight(intValue,intShift)
  87. BitShiftRight = Int(intValue / (2 ^ intShift))
  88. End Function
  89.  
  90. Function QueryOfficeProductKeys()
  91.  
  92. strBaseKey = "SOFTWARE\"
  93.  
  94. strOfficeKey = strBaseKey & "Microsoft\Office"
  95. Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
  96. objReg.EnumKey HKLM, strOfficeKey, arrOfficeVersionSubKeys
  97. intProductCount = 1
  98. If IsArray(arrOfficeVersionSubKeys) Then
  99.  
  100. For Each strOfficeVersionKey In arrOfficeVersionSubKeys
  101.  
  102. Select Case strOfficeVersionKey
  103. Case "11.0"
  104. CheckOfficeKey strOfficeKey & "\11.0\Registration",52,intProductCount
  105. Case "12.0"
  106. CheckOfficeKey strOfficeKey & "\12.0\Registration",52,intProductCount
  107. Case "14.0"
  108. CheckOfficeKey strOfficeKey & "\14.0\Registration",808,intProductCount
  109. Case "15.0"
  110. CheckOfficeKey strOfficeKey & "\15.0\Registration",808,intProductCount
  111. End Select
  112. Next
  113. End If
  114.  
  115. strBaseKey = "SOFTWARE\Wow6432Node\"
  116.  
  117. strOfficeKey = strBaseKey & "Microsoft\Office"
  118. Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
  119. objReg.EnumKey HKLM, strOfficeKey, arrOfficeVersionSubKeys
  120. intProductCount = 1
  121.  
  122. If IsArray(arrOfficeVersionSubKeys) Then
  123.  
  124. For Each strOfficeVersionKey In arrOfficeVersionSubKeys
  125.  
  126. Select Case strOfficeVersionKey
  127. Case "11.0"
  128. CheckOfficeKey strOfficeKey & "\11.0\Registration",52,intProductCount
  129. Case "12.0"
  130. CheckOfficeKey strOfficeKey & "\12.0\Registration",52,intProductCount
  131. Case "14.0"
  132. CheckOfficeKey strOfficeKey & "\14.0\Registration",808,intProductCount
  133. Case "15.0"
  134. CheckOfficeKey strOfficeKey & "\15.0\Registration",808,intProductCount
  135. End Select
  136. Next
  137. End If
  138. End Function
  139.  
  140. 'Office Product Key
  141. Sub CheckOfficeKey(strRegPath,intKeyOffset,intProductCount)
  142.  
  143. Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
  144. objReg.EnumKey HKLM, strRegPath, arrOfficeRegistrations
  145. If IsArray(arrOfficeRegistrations) Then
  146. For Each strOfficeRegistration In arrOfficeRegistrations
  147.  
  148. objReg.GetStringValue HKLM,strRegPath & "\" & strOfficeRegistration,"ConvertToEdition",strOfficeEdition
  149. objReg.GetBinaryValue HKLM,strRegPath & "\" & strOfficeRegistration,"DigitalProductID",arrProductID
  150. If strOfficeEdition <> "" And IsArray(arrProductID) Then
  151. WriteData "Product", strOfficeEdition
  152. WriteData "Key", DecodeProductKey(arrProductID,intKeyOffset) & vbCrLf
  153. intProductCount = intProductCount + 1
  154. End If
  155. Next
  156. End If
  157. End Sub
  158.  
  159.  
  160. 'Windows Product Key
  161. Sub QueryWindowsProductKeys()
  162. strWinKey = CheckWindowsKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion","DigitalProductId",52)
  163. If strWinKey <> "" Then
  164. WriteData "Product: " & Caption & Version & " (" & OSArchitecture & ")", ""
  165. 'WriteData "Installation Date: " & NewDate
  166. WriteData "Key", strWinKey & vbnewline
  167. Exit Sub
  168. End If
  169. strWinKey = CheckWindowsKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion","DigitalProductId4",808)
  170. If strWinKey <> "" Then
  171. WriteData "Product: " & Caption & Version & " (" & OSArchitecture & ")", ""
  172. 'WriteData "Installation Date: " & NewDate
  173. WriteData "Key", strWinKey & vbnewline
  174. Exit Sub
  175. End If
  176. strWinKey = CheckWindowsKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\DefaultProductKey","DigitalProductId",52)
  177. If strWinKey <> "" Then
  178. WriteData "Product: " & Caption & Version & " (" & OSArchitecture & ")", ""
  179. 'WriteData"Installation Date: " & NewDate
  180. WriteData "Key", strWinKey & vbnewline
  181. Exit Sub
  182. End If
  183. strWinKey = CheckWindowsKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\DefaultProductKey","DigitalProductId4",808)
  184. If strWinKey <> "" Then
  185. WriteData "Product: " & Caption & Version & " (" & OSArchitecture & ")", ""
  186. 'WriteData "Installation Date: " & NewDate
  187. WriteData "Key", strWinKey & vbnewline
  188. Exit Sub
  189. End If
  190.  
  191.  
  192.  
  193. End Sub
  194.  
  195.  
  196. Function CheckWindowsKey(strRegPath,strRegValue,intKeyOffset)
  197. strWinKey = DecodeProductKey(RegReadBinary(strRegPath,strRegValue),intKeyOffset)
  198. If strWinKey <> "BBBBB-BBBBB-BBBBB-BBBBB-BBBBB" And strWinKey <> "" Then
  199. CheckWindowsKey = strWinKey
  200. Else
  201. CheckWindowsKey = ""
  202. End If
  203. End Function
  204.  
  205. Function RegReadBinary(strRegPath,strRegValue)
  206. Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
  207. objReg.GetBinaryValue HKLM,strRegPath,strRegValue,arrRegBinaryData
  208. RegReadBinary = arrRegBinaryData
  209. Set objReg = Nothing
  210. End Function
  211.  
  212. Function OsArch()
  213. Set objShell = WScript.CreateObject("WScript.Shell")
  214. If objShell.ExpandEnvironmentStrings("%ProgramFiles(x86)%") = "%ProgramFiles(x86)%" Then
  215. OsArch = "x86"
  216. Else
  217. OsArch = "x64"
  218. End If
  219. Set objShell = Nothing
  220. End Function
  221.  
  222. Sub WriteData(strProperty,strValue)
  223.  
  224. objFileToWrite.WriteLine strProperty & ": " & Trim(strValue)
  225.  
  226. 'Set objShell = CreateObject("WScript.Shell")
  227. 'strKey = "HKLM\SOFTWARE\CentraStage\Custom\" & strProperty
  228. 'objShell.RegWrite strKey,Trim(strValue),"REG_SZ"
  229. 'Set objShell = Nothing
  230.  
  231. End Sub
  232.  
  233. wscript.echo "Successfully Saved"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement