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

标签 node.js azure powershell azure-functions

我正在 node.js 脚本中运行 Powershell 命令,以使用证书指纹连接到 Microsoft-Exchange。我正在使用 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 门户上看不到它。

请注意本地的日志,我们可以发现日志中打印的output出现在日志Executed 'Functions.HttpTrigger1' (Succeeded, Id=70bbb908-fddf-4f4d- 8115-aa1523e04361,持续时间=48ms)。因此,当函数完成时,会打印输出。如果将其部署到 azure,它只会将日志打印到 Executed 'Functions.HttpTrigger1' (Succeeded, Id=70bbb908-fddf-4f4d-8115-aa1523e04361, Duration=48ms) 并且不会继续打印输出

要解决此问题,您可以修改代码:

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

关于node.js - Node-Powershell Azure 函数应用程序不会读取命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67524772/

相关文章:

powershell - 当 AppService 运行/扩展到 2 个以上实例时的 Azure 部署

node.js - 将 Redis 用于 session 存储似乎不起作用

azure - 在 Visual Studio Team Services 中部署后运行集成测试

json - 使用 PowerShell 将 CSV 转换为 JSON 并将 JSON 转换为 CSV

.net - Cosmos DB 和 DevOps 自动化

azure - 错误 MSB4025 : The project file could not be loaded. 根级别的数据无效

node.js - Sequelize : query to include a parent model's data if it has associated children; don't return the child model's data

node.js - nginx代理传递 Node ,SSL?

javascript - Browserify 中的子模块

azure - 内部服务器错误 500 - 托管 Microsoft Bot Framework 的 Azure 应用服务