Skip to content

Instantly share code, notes, and snippets.

@jacoelho
Created June 29, 2017 07:41
Show Gist options
  • Select an option

  • Save jacoelho/5bf71f54ec82e8ae319fe815f9a8d3e2 to your computer and use it in GitHub Desktop.

Select an option

Save jacoelho/5bf71f54ec82e8ae319fe815f9a8d3e2 to your computer and use it in GitHub Desktop.
amazon ec2 windows autologon sysprep
# Create user with random password
$password = -join ((48..57) + (97..122) | Get-Random -Count 14 | % {[char]$_})
$unattendPath = "$Env:ProgramData\Amazon\EC2-Windows\Launch\Sysprep\Unattend.xml"
$xml = [xml](Get-Content $unattendPath)
$targetElememt = $xml.unattend.settings.Where{($_.Pass -eq 'oobeSystem')}.component.Where{($_.name -eq 'Microsoft-Windows-Shell-Setup')}
$autoLogonElement = [xml]('<AutoLogon>
<Password>
<Value>{0}</Value>
<PlainText>true</PlainText>
</Password>
<Enabled>true</Enabled>
<Username>Administrator</Username>
</AutoLogon>' -f $password)
$targetElememt.appendchild($xml.ImportNode($autoLogonElement.DocumentElement, $true))
$userAccountElement = [xml]('<UserAccounts xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State">
<AdministratorPassword>
<Value>{0}</Value>
<PlainText>true</PlainText>
</AdministratorPassword>
<LocalAccounts>
<LocalAccount wcm:action="add">
<Password>
<Value>{0}</Value>
<PlainText>true</PlainText>
</Password>
<Group>administrators</Group>
<DisplayName>Administrator</DisplayName>
<Name>Administrator</Name>
<Description>Administrator User</Description>
</LocalAccount>
</LocalAccounts>
</UserAccounts>' -f $password)
$targetElememt.appendchild($xml.ImportNode($userAccountElement.DocumentElement, $true))
$xml.Save($unattendPath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment