/ src / main-process / win-powershell.js
win-powershell.js
 1  let powershellPath;
 2  const path = require('path');
 3  const Spawner = require('./spawner');
 4  
 5  if (process.env.SystemRoot) {
 6    const system32Path = path.join(process.env.SystemRoot, 'System32');
 7    powershellPath = path.join(
 8      system32Path,
 9      'WindowsPowerShell',
10      'v1.0',
11      'powershell.exe'
12    );
13  } else {
14    powershellPath = 'powershell.exe';
15  }
16  
17  // Spawn powershell.exe and callback when it completes
18  const spawnPowershell = function(args, callback) {
19    // Set encoding and execute the command, capture the output, and return it
20    // via .NET's console in order to have consistent UTF-8 encoding.
21    // See http://stackoverflow.com/questions/22349139/utf-8-output-from-powershell
22    // to address https://github.com/atom/atom/issues/5063
23    args[0] = `\
24  [Console]::OutputEncoding=[System.Text.Encoding]::UTF8
25  $output=${args[0]}
26  [Console]::WriteLine($output)\
27  `;
28    args.unshift('-command');
29    args.unshift('RemoteSigned');
30    args.unshift('-ExecutionPolicy');
31    args.unshift('-noprofile');
32    Spawner.spawn(powershellPath, args, callback);
33  };
34  
35  // Get the user's PATH environment variable registry value.
36  //
37  // * `callback` The {Function} to call after registry operation is done.
38  //   It will be invoked with the same arguments provided by {Spawner.spawn}.
39  //
40  // Returns the user's path {String}.
41  exports.getPath = callback =>
42    spawnPowershell(
43      ["[environment]::GetEnvironmentVariable('Path','User')"],
44      function(error, stdout) {
45        if (error != null) {
46          return callback(error);
47        }
48  
49        const pathOutput = stdout.replace(/^\s+|\s+$/g, '');
50        return callback(null, pathOutput);
51      }
52    );