1

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);
               });
2
  • When you say "deploying to Azure" are you talking about an Azure Functions? Or an Azure IaaS VM? Where/what are you deploying this script to? Commented May 13, 2021 at 21:46
  • @Mike Azure function Commented May 13, 2021 at 21:47

1 Answer 1

1

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) {
 
}
Sign up to request clarification or add additional context in comments.

1 Comment

Worked like a charm. Thank you!!

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.