Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't get Invoke-Command to work using node-powershell #125

Open
Cliff-R-K opened this issue Oct 5, 2020 · 1 comment
Open

Can't get Invoke-Command to work using node-powershell #125

Cliff-R-K opened this issue Oct 5, 2020 · 1 comment

Comments

@Cliff-R-K
Copy link

Cliff-R-K commented Oct 5, 2020

I am trying to get some powershell commands to run on an remote computer when I make a POST-request to a andpoint but cant figure out how to get it to work. I can run the same commands from a powershell prompt/ISE and it works

But when I try the same using node-powershell I get an errormessage: "Principal Gäst was not found."

This works running from a remote using powershell:

$Hostname = "HOSTNAME"
$Username = "LANTM\admuser"
$Password = ConvertTo-SecureString "Password" -AsPlainText -Force
$Member = "Gäst"

$Credentials = New-Object -TypeName System.Management.Automation.PSCredential($Username, $Password)
$Session = New-PSSession –ComputerName $Hostname -Credential $Credentials

Invoke-Command -Session $Session -ScriptBlock {Add-LocalGroupMember -Group "Administratörer" -Member $Using:Member}
Remove-PSSession $session

This does not work running from a node applikation:

const shell = require("node-powershell");
const config = require("./config");

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

let username = config.AK_USER;
let password = config.AK_PASSWORD;

const runPowerShell = async (typeOfOperation, operations) => {

  try {
   if (typeOfOperation === "LocalAdmin") {
      operations.scriptname = "addUserToLocalAdminGroup";
      const response = await addUserToLocalAdminGroup(operations);
      return response
    } else {
      return;
    }
  } catch (error) {
    console.error(error.message);
    ps.dispose();
  }
};

const addUserToLocalAdminGroup = async (operations) => {
 
  username = config.LOCAL_ADMIN_USER
  password = config.LOCAL_ADMIN_PASSWORD
  console.log(`LocalAdmin ${username} ${password} ${operations.hostname} ${operations.member}`)
  ps.addCommand(`../../Scripts/${operations.scriptname}.ps1`);
  ps.addArgument(operations.hostname);
  ps.addArgument(username);
  ps.addArgument(password);
  ps.addArgument(operations.member);

  const output = (await ps.invoke()).trim();
    return output;
};

module.exports = { runPowerShell };

addUserToLocalAdminGroup.ps1

$Hostname = $args[0]
$Username = $args[1]
$Password = ConvertTo-SecureString $args[2] -AsPlainText -Force
$Member = $args[3]

$Credentials = New-Object -TypeName System.Management.Automation.PSCredential($Username, $Password)
$Session = New-PSSession –ComputerName $Hostname -Credential $Credentials

#Invoke-Command -Session $Session -ScriptBlock {(Add-LocalGroupMember -Group "Administratörer" -Member $Member), (${function:ScheduleServiceRemoval} | Out-Null)}
Invoke-Command -Session $Session -ScriptBlock {(Add-LocalGroupMember -Group "Administratörer" -Member $Using:Member)}
Remove-PSSession $session
@robinmalik
Copy link

@burton666 Did you solve this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants