Guest User

Untitled

a guest
Mar 19th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. /*
  2. Gets new available Windows license from a .json with a list of MAK keys, updates that db, and executes Windows licensing process with it
  3. Dependencies:
  4. - node.exe : executable only, no installation needed, put the exe in the same folder of this .js
  5. - .json : file with licenses:
  6.  
  7. example json DB:
  8. {
  9. "sinUsar": [
  10. "XXXXX-XXXXX-XXXXX-XXXXX-XXXX1",
  11. "XXXXX-XXXXX-XXXXX-XXXXX-XXXX2",
  12. ],
  13. "usadas": [
  14. "XXXXX-XXXXX-XXXXX-XXXXX-XXXX3"
  15. ]
  16. }
  17.  
  18. example of .bat file to execute in a client pc, from a shared network folder where this .js and node.exe are chared
  19. REM cd /d %~dp0/licenciarWindows
  20. net use k: \\SOME-COMPUTER\compartida
  21. cd /d k:\licenciarWindows
  22. node.exe licenciarWindows.js
  23. */
  24.  
  25.  
  26. async function main() {
  27.  
  28. const fs = require('fs')
  29.  
  30. // abrir db de licencias
  31. const licenciasFile = __dirname + '/licenciasWin7Pro2018.json'
  32. let licencias = JSON.parse( fs.readFileSync(licenciasFile, 'UTF8') )
  33.  
  34. // conseguir la siguiente licencia sinUSAR
  35. let unaLicencia = licencias.sinUsar.pop()
  36. // console.log(unaLicencia)
  37.  
  38. // meter la licencia en 'usadas' y guardar bd de licencias actualizada
  39. licencias.usadas.push( unaLicencia )
  40. fs.writeFileSync( licenciasFile, JSON.stringify(licencias,null,2).replace(/\n/g, '\r\n'), 'UTF8' ) // use windows CRLF to be notepad.exe friendly
  41.  
  42. // licenciar Windows
  43. execCmdSync(`slmgr /ipk ${unaLicencia}`)
  44. execCmdSync(`slmgr /ato`)
  45.  
  46. // si necesitas cosas asíncronas, así de facil
  47. // var respuesta = await readLine('¿pregunta?')
  48. // console.log('pues si', respuesta)
  49.  
  50. }
  51. main().then(process.exit) // así para poder usar await comodamente en main()
  52.  
  53.  
  54.  
  55. // --------------------------------------------------------- HELPERS
  56. function execCmdSync (command) {
  57. console.log(command)
  58. var child_process = require('child_process')
  59. let result = child_process.spawnSync(
  60. process.env.comspec,
  61. `/c ${command.trim()}`.split(' '),
  62. {stdio: 'inherit'}
  63. ).status
  64. console.log(result)
  65. return result
  66. }
  67.  
  68.  
  69. async function readLine (question) {
  70. const readline = require('readline')
  71.  
  72. const rl = readline.createInterface({ input: process.stdin, output: process.stdout })
  73.  
  74. let answer = await new Promise(resolve=>{
  75. rl.question(question + " ", (answer) => {
  76. rl.close();
  77. resolve(answer)
  78. })
  79. })
  80.  
  81. return answer
  82. }
Add Comment
Please, Sign In to add comment