我正在使用 Powershell 和 JSON 文件来创建新的虚拟机。 JSON 文件的最后部分指示新创建的虚拟机运行文件。我可以验证(通过 Azure 门户和 Powershell)该文件是否存在于 JSON 文件中给定的 URL 处。
但 VM 仍以结果 ProvisioningState:Failed
结束,因为 VM 在处理扩展“CustomScriptExtension”时报告了失败。错误消息:“已完成执行命令”
,这是令人沮丧的矛盾。 (我们也遇到了 domainjoin
CustomScriptExtension 的问题,它也“失败”并显示“成功”消息。)
该脚本是一个简单的 CMD 文件,应该创建文件 C:\Users\Public\Documents\runonce.log
,但这不会发生,所以我猜该文件不会运行.
但是,虚拟机已创建,然后我可以手动登录并运行该文件。所以这不应该是用户权限之类的问题。
编辑: This comment声明“CustomScriptExtension 不以本地管理员权限运行”,但我认为这不是问题所在。
我错过了什么?
这是 JSON 的相关部分:
...
"resources": [
...
{
"type": "Microsoft.Compute/virtualMachines/extensions",
"name": "[concat(parameters('vmName'),'/CustomScriptExtension')]",
"apiVersion": "2015-05-01-preview",
"location": "[resourceGroup().location]",
"dependsOn": ["[concat('Microsoft.Compute/virtualMachines/', parameters('vmName'))]"],
"properties": {
"publisher": "Microsoft.Compute",
"type": "CustomScriptExtension",
"typeHandlerVersion": "1.2",
"settings": {
"fileUris": ["[concat(parameters('scriptFilePath'),parameters('scriptFileName'))]"],
"commandToExecute": "[concat('powershell -ExecutionPolicy Unrestricted -file ',parameters('scriptFileName'))]"
}
}
}]
并且我已验证 scriptFilePath
和 scriptFileName
是 https://euwest2766.blob.core.windows.net/script/
并且run-once.cmd
对应于 Azure 中显示的 blob URL。我什至尝试将 Azure Blob URL 放入 JSON,得到相同的结果。
最佳答案
这不起作用,因为您正在尝试通过 Powershell 运行批处理文件。
您需要更改此设置 -
"commandToExecute": "[concat('powershell -ExecutionPolicy Unrestricted -file ',parameters('scriptFileName'))]"
到此
"commandToExecute": "[concat('cmd /s',parameters('scriptFileName'))]"
这应该按预期执行。
关于json - Azure customscriptextension找不到脚本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35339054/