Repo44
Repo44

Reputation: 75

Node-Powershell Azure function app will not read commands

I am running Powershell commands in a node.js script to connect to Microsoft-Exchange with a certificate thumbprint.I am using the node-powershell npm package. All works as expected locally. When deploying to Azure the Powershell commands dont seem to run. Has anyone else had success deploying a similar script?

   let ps = new shell({
      executionPolicy: "Bypass",
      noProfile: true
   });
  ps.addCommand('Connect-ExchangeOnline -CertificateThumbPrint "thumbprintString" -AppId "APP_ID"
-Organization "example.onmicrosoft.com"');
            ps.invoke()
               .then((output) => {
                  console.log(output);
                  resolve();
               })
               .catch((err) => {
                  console.log(err);
                  reject(err);
               });

Upvotes: 1

Views: 236

Answers (1)

Hury Shen
Hury Shen

Reputation: 15734

The problem was caused by async of the function, so you can see the result was print in local but can't see it on azure portal.

Please notice the logs in local, we can find the output which print in logs comes after the log Executed 'Functions.HttpTrigger1' (Succeeded, Id=70bbb908-fddf-4f4d-8115-aa1523e04361, Duration=48ms). So the output was print when the function had been completed. If you deploy it to azure, it will just print the logs to Executed 'Functions.HttpTrigger1' (Succeeded, Id=70bbb908-fddf-4f4d-8115-aa1523e04361, Duration=48ms) and will not continue to print the output.

To solve this problem, you can modify your code from:

ps.addCommand('Connect-ExchangeOnline -CertificateThumbPrint "thumbprintString" -AppId "APP_ID"
-Organization "example.onmicrosoft.com"');
            ps.invoke()
               .then((output) => {
                  console.log(output);
                  resolve();
               })
               .catch((err) => {
                  console.log(err);
                  reject(err);
               });

to

ps.addCommand('Connect-ExchangeOnline -CertificateThumbPrint "thumbprintString" -AppId "APP_ID"
-Organization "example.onmicrosoft.com"');
try {
    const result = await ps.invoke();
    context.log(result);
} catch (error) {
 
}

Upvotes: 1

Related Questions