Skip to content

Instantly share code, notes, and snippets.

@davidobrien1985
Created May 12, 2016 20:56
Show Gist options
  • Save davidobrien1985/fe92aeac51ba35926cabed315fa1c449 to your computer and use it in GitHub Desktop.
Save davidobrien1985/fe92aeac51ba35926cabed315fa1c449 to your computer and use it in GitHub Desktop.
Packer template that's supposed to, but doesn't sysprep.
$EC2SettingsFile="C:\\Program Files\\Amazon\\Ec2ConfigService\\Settings\\BundleConfig.xml"
$xml = [xml](get-content $EC2SettingsFile)
$xmlElement = $xml.get_DocumentElement()
foreach ($element in $xmlElement.Property)
{
if ($element.Name -eq "AutoSysprep")
{
$element.Value="Yes"
}
}
$xml.Save($EC2SettingsFile)
# https://github.com/mefellows/windows-machine-factory-tutorial/blob/master/machine-factory/scripts/Ec2Config.ps1
write-output "Updating Ec2 Config..."
$EC2SettingsFile="C:\\Program Files\\Amazon\\Ec2ConfigService\\Settings\\Config.xml"
$xml = [xml](get-content $EC2SettingsFile)
$xmlElement = $xml.get_DocumentElement()
$xmlElementToModify = $xmlElement.Plugins
foreach ($element in $xmlElementToModify.Plugin)
{
if ($element.name -eq "Ec2SetPassword")
{
$element.State="Enabled"
}
elseif ($element.name -eq "Ec2SetComputerName")
{
$element.State="Enabled"
}
elseif ($element.name -eq "Ec2HandleUserData")
{
$element.State="Enabled"
}
}
$xml.Save($EC2SettingsFile)
write-output "Updated Ec2 Config."
{
"variables": {
"dependency_s3_bucket_name": "",
"aws_source_ami": "ami-1af6d779",
"aws_instance_type": "t2.micro",
"aws_instance_profile": "",
"aws_vpc_id": "",
"aws_subnet_id": "",
"aws_region": "ap-southeast-2",
"soe_version": "",
"build_number": ""
},
"builders": [
{
"ami_name": "{{ user `soe_version` }}-{{ user `build_number` }}-ad-windows-soe-{{isotime | clean_ami_name}}",
"ami_description": "Autobuild of Windows Server 2012 R2 AMI with Active Directory features installed. Build Number: {{ user `build_number` }}",
"communicator": "winrm",
"iam_instance_profile": "{{ user `aws_instance_profile` }}",
"instance_type": "{{ user `aws_instance_type` }}",
"region": "{{ user `aws_region` }}",
"source_ami": "{{ user `aws_source_ami` }}",
"type": "amazon-ebs",
"user_data_file": "{{template_dir}}/scripts/user-data.ps1",
"windows_password_timeout": "60m",
"winrm_insecure": "true",
"winrm_port": "5986",
"winrm_use_ssl": "true",
"winrm_username": "administrator",
"vpc_id" : "{{ user `aws_vpc_id` }}",
"subnet_id" : "{{ user `aws_subnet_id` }}",
"run_tags": {
"DependencyS3BucketName": "{{user `dependency_s3_bucket_name`}}",
"BuildNumber": "{{user `build_number`}}"
},
"tags": {
"BuildNumber": "{{user `build_number`}}",
"BuildUUID": "{{user `build_uuid`}}"
}
}
],
"provisioners": [
{
"type": "shell-local",
"command": "aws s3 sync --region {{user `aws_region`}} {{template_dir}}/PSWindowsUpdate s3://{{user `dependency_s3_bucket_name`}}/windows_soe/PSWindowsUpdate"
},
{
"type": "powershell",
"scripts": [
"{{template_dir}}/scripts/create-packer-user.ps1"
]
},
{
"type": "powershell",
"inline": ["winrm quickconfig -quiet"]
},
{
"type": "windows-restart",
"restart_timeout": "20m"
},
{
"type": "powershell",
"scripts": [
"{{template_dir}}/scripts/dsc-adds.ps1",
"{{template_dir}}/scripts/delete-packer-user.ps1",
"{{template_dir}}/scripts/delete-packer-directory.ps1",
"{{template_dir}}/scripts/Ec2Config.ps1",
"{{template_dir}}/scripts/BundleConfig.ps1"
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment