Skip to content

Instantly share code, notes, and snippets.

#Set my hostname based on my internal IP address
$instanceName = (((Invoke-WebRequest -UseBasicParsing -Uri http://169.254.169.254/latest/meta-data/hostname).Content).split(".")[0]).replace("ip-172-31","TCG")
#Change the hostname in the unattend.xml file
$filePath = "C:\Windows\Panther\Unattend.xml"
$AnswerFile = [xml](Get-Content -Path $filePath)
$ns = New-Object System.Xml.XmlNamespaceManager($answerFile.NameTable)
$ns.AddNamespace("ns", $AnswerFile.DocumentElement.NamespaceURI)
$ComputerName = $AnswerFile.SelectSingleNode('/ns:unattend/ns:settings[@pass="specialize"]/ns:component[@name="Microsoft-Windows-Shell-Setup"]/ns:ComputerName', $ns)
$ComputerName.InnerText = $InstanceName
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "",
"Parameters": {
},
"Resources": {
"RDSDBI3N4HT": {
"Type": "AWS::RDS::DBInstance",
"Properties": {
"AllocatedStorage": "200",
# Finally, perform sysprep.
# Start sysprep but quit instead of shutdown
Start-Process -FilePath $sysprepPath -ArgumentList ("/oobe /quit /generalize `"/unattend:{0}`"" -f $answerFilePath) -Wait -NoNewWindow
# Replace windeploy.exe with the custom PowerShell startup script
Set-ItemProperty -Path HKLM:\System\setup -Name CmdLine -Value "powershell.exe -command c:\Scripts\startup.ps1"
# Delete the existing route to query instance metadata
route delete 169.254.169.254
"EC2" : {
"Type": "AWS::EC2::Instance",
"Metadata": {
"AWS::CloudFormation::Init": {
"configSets": {
"config": [
"monitoring",
"setup",
"rename",
"install",
"Flows": {
"Flows": [
"(ApplicationEventLog,SystemEventLog,DSCLog),CloudWatchEventLogs",
"EC2ConfigLog,EC2ConfigLogs",
"cfninitLog,cfninitLogs"
]
}
{
"Id": "CloudWatchEventLogs",
"FullName": "AWS.EC2.Windows.CloudWatch.CloudWatchLogsOutput,AWS.EC2.Windows.CloudWatch",
"Parameters": {
"AccessKey": "",
"SecretKey": "",
"Region": "ap-southeast-2",
"LogGroup": "Event-Log-Group",
"LogStream": "{instance_id}"
}
{
"Id": "cfninitLog",
"FullName": "AWS.EC2.Windows.CloudWatch.CustomLog.CustomLogInputComponent,AWS.EC2.Windows.CloudWatch",
"Parameters": {
"LogDirectoryPath": "C:\\cfn\\log",
"TimestampFormat": "yyyy-MM-dd HH:mm:ss,fff",
"Encoding": "UTF-8",
"Filter": "cfn-init.log",
"CultureName": "en-US",
"TimeZoneKind": "Local"
{
"Id": "ApplicationEventLog",
"FullName": "AWS.EC2.Windows.CloudWatch.EventLog.EventLogInputComponent,AWS.EC2.Windows.CloudWatch",
"Parameters": {
"LogName": "Application",
"Levels": "7"
}
},
{
"Id": "SystemEventLog",
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:DescribeLogGroups",
"logs:DescribeLogStreams",
{
"EngineConfiguration": {
"PollInterval": "00:00:15",
"Components": [
{
"Id": "ApplicationEventLog",
"FullName": "AWS.EC2.Windows.CloudWatch.EventLog.EventLogInputComponent,AWS.EC2.Windows.CloudWatch",
"Parameters": {
"LogName": "Application",
"Levels": "7"