我正在 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/