Skip to content

Instantly share code, notes, and snippets.

@giuliocalzolari
Last active February 8, 2019 10:47
Show Gist options
  • Save giuliocalzolari/1506237272f42d581d035cbf69b2eb29 to your computer and use it in GitHub Desktop.
Save giuliocalzolari/1506237272f42d581d035cbf69b2eb29 to your computer and use it in GitHub Desktop.
EC2 UserData - AD autojoin
<powershell>
$password = (Get-SSMParameter -Name /ds/svc_password -WithDecryption $true).Value | ConvertTo-SecureString -asPlainText -Force
$username = (Get-SSMParameter -Name /ds/svc_user -WithDecryption $true).Value
$credential = New-Object System.Management.Automation.PSCredential($username,$password)
$instanceID = invoke-restmethod -uri http://169.254.169.254/latest/meta-data/instance-id
Add-Computer -domainname example.com -ComputerName $instanceID -Credential $credential -Passthru -Verbose -Force -Restart
</powershell>
#!/bin/bash
PASSWORD="$(aws ssm get-parameter --name "/ds/svc_password" --with-decryption --output text --query Parameter.Value)"
USERNAME="$(aws ssm get-parameter --name "/ds/svc_user" --with-decryption --output text --query Parameter.Value)"
INSTANCEID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
echo -n "${PASSWORD}" | adcli join --stdin-password --domain-ou=OU=Computers,DC=example,DC=com --login-user="${USERNAME}" --computer-name="${INSTANCEID}" example.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment