Am running a node js service which internally calling a PowerShell script to execute few commands. I am providing some parameters to the node js service from postman, and I want to pass them to the PowerShell script so that it will execute based on the parameter provided.
I am trying to pass them to CmdletBinding
block of the PowerShell, but I am ok if I can pass it outside the block as well.
Nodejs Service:
var express = require("express");
var bodyParser = require("body-parser");
var app = express();
const shell = require('shelljs');
const { stdout, stderr } = require("process");
//Varaible Declaration
let dbListQuote = "";
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.get('/health', function (req, res) {
console.log("Request for health service");
res.send('OK');
});
app.get('/triggerDatabaseBackup', function (req, res) {
//Validation for both the parameters like 'dbList' & 'target folder'
if (req.query.DatabaseList != null && req.query.TargetFolder != null && req.query.DatabaseList != "" && req.query.TargetFolder != "") {
console.log("Required parameters for database back-up are provided");
//validation for missing "" for the DBList
if (req.query.DatabaseList.startsWith('"') && req.query.DatabaseList.slice(-1) === '"') {
// console.log("The double quotes is present");
}
else {
// console.log("The double quotes is missing, updating the value ");
dbListQuote = '"' req.query.DatabaseList '"';
// console.log("The updated value ", dbListQuote);
}
var dbListTemp = dbListQuote.split(",");
var dbList = dbListTemp.join("\",\"");
console.log('Request of database backup for', dbList, 'has been triggered sucessfully !!!');
let executePowerShellCommand = shell.exec('./powerTest.ps1', {'shell': 'powershell.exe'}, (error,stdout,stderr) => {
})
res.status(200).json('Success! Database-backup Completed!')
}
else {
console.log("Parameter Missing");
}
})
var server = app.listen(9045, function () {
var host = server.address().address;
var port = server.address().port;
console.log("Database Automation Service is listening on :", port);
});
PowerShell Script
[CmdletBinding()]
Param(
[Parameter(Mandatory = $false)] [string] $IPAddress = "SQL Server",
[Parameter(Mandatory = $false)] [string] $Username = "LocalUser",
[Parameter(Mandatory = $false)] [string] $Password = "<Password>",
[Parameter(Mandatory = $false)] [string[]][AllowNull()][AllowEmptyCollection()] $DatabaseList=@(<dbList>),
[Parameter(Mandatory = $false)] [string] $source_folder = "req.query.TargetFolder"
#[Parameter(Mandatory = $false)] [Int32] $QueryTimeout=600
)
$Status = Get-Service -ComputerName $IPAddress | where {($_.name -like "MSSQL$*" -or $_.name -like "MSSQLSERVER" -or $_.name -like "SQL Server (*") }
$object = $Status | SELECT Name,Status
Write-Host "Current State Of The SQL Server " $object.Status
Write-Host "List Of database is :" $DatabaseList
Write-Host "Location Provided is :" $source_folder
Prameter From Postman:
How to do it ?
CodePudding user response:
You need to change the following,
let executePowerShellCommand = shell.exec('./powerTest.ps1', {'shell': 'powershell.exe'}, (error,stdout,stderr) => {
to
let executePowerShellCommand = shell.exec(`./powerTest.ps1 ${arg0} ${arg1}`, {'shell': 'powershell.exe'}, (error,stdout,stderr) => {
Pass any number of argument in arg0, arg1....argn. You need to handle it from PowerShell with $args[0],$args[1]...etc