问题: 在运行Powershell脚本时,我无法使Packer正常工作,该脚本最终在安装软件后重新启动Windows VM。
脚本完成后(scripts / watchMaker_stigVM.ps1),计算机将重新启动,并且Packer移至下一个Provisioner。 在这种情况下,下一个供应商是sysprep的Powershell内联供应商
打包机挂起,然后引发错误,并删除资源进行清理
我尝试过的事情:”
1)在Powershell脚本配置程序之后使用“ Windows-Restart”打包程序配置程序,该重启程序会导致VM重新启动(scripts / watchMaker_stigVM.ps1)。
2)添加“ restart_check_command”以检查“ RebootPending”注册表项
3)在Powershell脚本配置器的之后和Windows重启配置器的之前中添加“ Pause_before”,以使计算机有时间恢复在线状态,并允许WinRM重新连接。
提供者经过以上尝试:
"provisioners": [
{
"type": "powershell",
"scripts": [
"scripts/watchMaker_stigVM.ps1"
]
},
{
"type": "powershell",
"pause_before": "240s",
"inline": ["dir c:\\"]
},
{
"type": "windows-restart",
"timeout": "2h",
"restart_check_command": "HKLM:SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Component Based Servicing\\RebootPending"
},
{
"type": "powershell",
"pause_before": "120s",
"inline": ["dir c:\\"]
},
{
"type": "powershell",
"inline": [
"if( Test-Path $Env:SystemRoot\\windows\\system32\\Sysprep\\unattend.xml ){ rm $Env:SystemRoot\\windows\\system32\\Sysprep\\unattend.xml -Force}",
"& $env:SystemRoot\\System32\\Sysprep\\Sysprep.exe /oobe /generalize /quiet /quit",
"while($true) { $imageState = Get-ItemProperty HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Setup\\State | Select ImageState; if($imageState.ImageState -ne 'IMAGE_STATE_GENERALIZE_RESEAL_TO_OOBE') { Write-Output $imageState.ImageState; Start-Sleep -s 10 } else { break } }"
]
}
] }
在没有任何Windows重新启动或暂停配置程序的情况下,在以上方案中使用的构建脚本:
{
"variables": {
"client_id": "",
"client_secret": "",
"object_id": "",
"tenant_id": "",
"subscription_id": "",
"managed_image_name": "",
"managed_image_resource_group_name": "",
"location": "",
"os_type": "",
"image_publisher": "",
"image_offer": "",
"image_sku": "",
"dept": "",
"task": ""
},
"builders": [
{
"type": "azure-arm",
"client_id": "{{user `client_id`}}",
"client_secret": "{{user `client_secret`}}",
"tenant_id": "{{user `tenant_id`}}",
"subscription_id": "{{user `subscription_id`}}",
"managed_image_resource_group_name": "{{user `managed_image_resource_group_name`}}",
"managed_image_name": "{{user `managed_image_name`}}",
"os_type": "{{user `os_type`}}",
"image_publisher": "{{user `image_publisher`}}",
"image_offer": "{{user `image_offer`}}",
"image_sku": "{{user `image_sku`}}",
"communicator": "winrm",
"winrm_use_ssl": true,
"winrm_insecure": true,
"winrm_timeout": "2h",
"winrm_username": "packer",
"azure_tags": {
"dept": "{{user `dept`}}",
"task": "{{user `task`}}"
},
"location": "{{user `location`}}",
"vm_size": "Standard_B4ms"
}
],
"provisioners": [
{
"type": "powershell",
"timeout": "2h",
"script":
"scripts/watchMaker_stigVM.ps1"
},
{
"type": "powershell",
"inline": [
"if( Test-Path $Env:SystemRoot\\windows\\system32\\Sysprep\\unattend.xml ){ rm $Env:SystemRoot\\windows\\system32\\Sysprep\\unattend.xml -Force}",
"& $env:SystemRoot\\System32\\Sysprep\\Sysprep.exe /oobe /generalize /quiet /quit",
"while($true) { $imageState = Get-ItemProperty HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Setup\\State | Select ImageState; if($imageState.ImageState -ne 'IMAGE_STATE_GENERALIZE_RESEAL_TO_OOBE') { Write-Output $imageState.ImageState; Start-Sleep -s 10 } else { break } }"
]
}
]
}
打包程序日志,显示Powershell脚本“ scripts / watchMaker_stigVM.ps1”的成功完成,下一个试图运行,挂起,然后打包程序失败并删除资源的Powershell内联供应程序
==> azure-arm: 2019-11-08 17:36:51,030 [watchmaker.workers.base.SaltWindows][INFO ][3392]: Applying the salt "highstate", states=highstate
==> azure-arm: 2019-11-08 17:36:51,030 [watchmaker.workers.base.SaltWindows][DEBUG][3392]: Command: C:\Salt\salt-call.bat --local --retcode-passthrough --no-color --config-dir C:\Watchmaker\Salt\conf --log-file C:\Watchmaker\Logs\salt_call.debug.log --log-file-level debug --log-level error --out quiet --return local state.highstate
==> azure-arm: 2019-11-08 17:38:04,492 [watchmaker.workers.base.SaltWindows][DEBUG][3392]: Command retcode: 0
==> azure-arm: 2019-11-08 17:38:04,492 [watchmaker.workers.base.SaltWindows][INFO ][3392]: Salt states all applied successfully!
==> azure-arm: 2019-11-08 17:38:04,492 [watchmaker.workers.base.SaltWindows][INFO ][3392]: Cleanup Time...
==> azure-arm: 2019-11-08 17:38:04,492 [watchmaker.workers.base.SaltWindows][DEBUG][3392]: working_dir=C:\Watchmaker\WorkingFiles\Salt-1_5kqmta
==> azure-arm: 2019-11-08 17:38:04,508 [watchmaker.workers.base.SaltWindows][INFO ][3392]: Deleted working directory...
==> azure-arm: 2019-11-08 17:38:04,508 [watchmaker.workers.base.SaltWindows][INFO ][3392]: Exiting cleanup routine...
==> azure-arm: 2019-11-08 17:38:04,508 [watchmaker.Client][INFO ][3392]: Reboot scheduled. System will reboot after the script exits.
==> azure-arm: 2019-11-08 17:38:04,648 [watchmaker.Client][INFO ][3392]: Stop time: 2019-11-08 17:38:04.648938
==> azure-arm: Provisioning with Powershell...
==> azure-arm: Provisioning with powershell script: C:\Users\Giann\AppData\Local\Temp\powershell-provisioner582533063
==> azure-arm: Provisioning step had errors: Running the cleanup provisioner, if present...
==> azure-arm:
==> azure-arm: Cleanup requested, deleting resource group ...
如您所见,Powershell脚本最终使VM重新引导,而Packer本身并未启动VM。
==> azure-arm:2019-11-08 17:38:04,508 [watchmaker.Client] [INFO] [3392]:计划重新启动。脚本退出后,系统将重新启动。
上下文的先前Packer成功:
我已经成功地使用Packer构建了Windows VM,并通过Powershell脚本供应器安装了软件,然后通过Powershell内联供应器进行了系统准备。
谢谢您的帮助。