Node-Powershell Azure 函数应用程序不会读取命令

Node-Powershell Azure function app will not read commands

我正在 运行 在 node.js 脚本中使用 Powershell 命令连接到带有证书的 Microsoft-Exchange thumbprint.I 我正在使用 node-powershell npm 包。一切都按预期在本地工作。部署到 Azure 时,Powershell 命令似乎没有 运行。有没有其他人成功部署了类似的脚本?

   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);
               });

问题是函数的异步导致的,所以你可以看到结果在本地打印,但在azure portal上看不到。

请注意本地的日志,我们可以发现日志中打印的output排在日志Executed 'Functions.HttpTrigger1' (Succeeded, Id=70bbb908-fddf-4f4d-8115-aa1523e04361, Duration=48ms)之后。所以 output 是在函数完成时打印的。如果将其部署到 Azure,它只会将日志打印到 Executed 'Functions.HttpTrigger1' (Succeeded, Id=70bbb908-fddf-4f4d-8115-aa1523e04361, Duration=48ms) 而不会继续打印 output.

要解决这个问题,您可以修改您的代码:

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);
               });

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