1

I create register app with node.js and Express.js

So it has name form and password form and submit button.

I want to make it, When I clicked the submit button, run the powershell script. This script means, add local windows user and set the password.

In PowerShell this script works well. And this powershell script should be run as administrator.

$PASSWORD= ConvertTo-SecureString –AsPlainText -Force -String kamisama123@@
New-LocalUser -Name "gohan" -Description "sleepy" -Password $PASSWORD

After this command the local user is created.

enter image description here

I use node-powershell package in my code. This code means When I submit the my information and run the PowerShell script and add information in to mongodb

router.post(`/submit`, async function (req, res) {
  let name = req.body.name; 
  let password = req.body.password; 
  let passwordCheck = req.body.passwordCheck;
  let company = req.body.company; 
  let registerStatus = false; 
  let executives = req.body.executives; 
  let registerDate = Date.now();


  let ps = new Shell();

  let cmd = new PSCommand(`$PASSWORD= ConvertTo-SecureString ?AsPlainText -Force -String ${password}`)
  let script = new PSCommand(`New-LocalUser -Name "${name}" -FullName "${name}" -Description "${name}" -PasswordNeverExpires -Password $PASSWORD`)
  ps.addCommand(cmd);
  ps.addCommand(script);
ㅡㅛ
  try {
    if (password !== passwordCheck) { 
      res.redirect('/') 
    } else {
      let encodedPassword = Base64.encode(password);

      await User.create({
        name,
        password: encodedPassword,
        company,
        registerStatus,
        executives,
        registerDate
      }, (err, result) => {
        if (err) throw err;
      })
      res.redirect(`/success/${name}`)
    }
  } catch (err) {
    throw err;
  }
})

But the error throws

(node:21596) UnhandledPromiseRejectionWarning: TypeError: Shell is not a constructor

I don't know where the error comes from.

3
  • 1
    Love the DBZ references :). Can you paste the entire script though? We don't see the User function defined here. Be sure also that you're loading the type with const Shell = require('node-powershell'); Commented Feb 1, 2021 at 14:58
  • @FoxDeploy Full code is in my github gist. gist.github.com/writingdeveloper/… Commented Feb 1, 2021 at 15:02
  • Is there any reason that you're bound to use node-powershell? If not, you can use child_process built-in to Node. Commented Feb 1, 2021 at 15:37

1 Answer 1

3

Constructor is a term from object-oriented programming (OOP), every object in OOP has a constructor function.

For the shell object the constructor can not be empty (shell() has empty brackets)

Normally the constructor of shell has two arguments: execution policy and noProfile

   let ps = new shell({
  executionPolicy: 'Bypass',
  noProfile: true
});

https://rannn505.gitbook.io/node-powershell/start https://www.jeansnyman.com/posts/executing-powershell-commands-in-a-nodejs-api/

In How to execute Powershell script function with arguments using Node JS? is possibly a solution for this issue. The powershell script has to be wrapped in a promise in nodejs (and possibly be dot-sourced) :

How to execute Powershell script function with arguments using Node JS?

Powershell and potentially also node.js implements the OOP (object-oriented programming) design paradigms and semantics i.e. when writing object-oriented code each class has to have a constructor (new()) and a destructor (remove...) and it should have get() and set() methods to access (read and write) the fields (or attributes) of a class. In ps this is straighlty implemented

It is also possible to use object-oriented design patterns in PS https://dfinke.github.io/powershell,%20design%20patterns/2018/04/13/PowerShell-And-Design-Patterns.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.