Background
我正在使用 ARM 模板在 Azure 中预配 VM,并创建了一个用于安装和配置 IIS 的所需状态配置 .ps1 文件。目前为止,一切都好。
然后,我在Node
块旁边添加了一个Script
块。
当前设置:
Configuration Main
{
Param ( [string] $nodeName )
Import-DscResource -ModuleName PSDesiredStateConfiguration
Node $nodeName
{
WindowsFeature WebServer
{
Name = "Web-Server"
Ensure = "Present"
}
#other WindowsFeatures
}
Script FormatDiskScript
{
SetScript =
{
#Powershell to format disks
}
TestScript = { return $false }
GetScript = { }
}
}
在我的 ARM 模板中,我已将 DSC 扩展添加到我的 VM,并指定了获取 zip 文件、要运行的script
和要调用function
url
。
"properties": {
"publisher": "Microsoft.Powershell",
"type": "DSC",
"typeHandlerVersion": "2.23",
"autoUpgradeMinorVersion": true,
"settings": {
"configuration": {
"url": "[concat(parameters('_artifactsLocation'), '/', variables('dscArchiveFolder'), '/', variables('webvm_dscZipFileName'))]",
"script": "webvm-dsc.ps1",
"function": "Main"
},
"configurationArguments": {
"nodeName": "[variables('webvm_name')]"
}
},
"protectedSettings": {
"configurationUrlSasToken": "[parameters('_artifactsLocationSasToken')]"
}
}
问题
它生成两个 .mof 文件并执行Node
和Script
部分,但只有Node
部分成功完成。
当我只用Script
运行时,这有效,所以Script
是有效的。我只是在运行它们时遇到问题。
这是我在 C:\Packages\Plugins\Microsoft.Powershell.DSC\2.23.0.0\Status\0.status 的输出中看到的
Settings handler status to 'transitioning'
Updating execution status
DSC configuration completed
No meta mof back up file exist to restore ...
Settings handler status to 'error'
答
在尝试了不同的方法之后,我终于偶然发现了一种有效的方法。我只是将Script
放在Node
内,而不是将其作为对等体:
Configuration Main
{
Param ( [string] $nodeName )
Import-DscResource -ModuleName PSDesiredStateConfiguration
Node $nodeName
{
WindowsFeature WebServer
{
Name = "Web-Server"
Ensure = "Present"
}
#other WindowsFeatures
Script FormatDiskScript
{
SetScript =
{
#Powershell to format disks
}
TestScript = { return $false }
GetScript = { }
}
}
}
您需要在 DSC 配置中创建 2 个配置(例如主配置和手动配置),并将要使用 ARM 模板执行的内容放入主配置中,将其他内容放入手动
或者在 2 个单独的文件中创建 2 个单独的配置。