Skip to content

Instantly share code, notes, and snippets.

@chrisbrownie
Created April 11, 2018 01:13
Show Gist options
  • Save chrisbrownie/5f614a2614c47991c9430993fa7f30cb to your computer and use it in GitHub Desktop.
Save chrisbrownie/5f614a2614c47991c9430993fa7f30cb to your computer and use it in GitHub Desktop.
Uses SSM to reset the administrator password of a Windows machine
#!/usr/local/bin/python
import argparse
import boto3
def resetPassword(instanceId, password, region, profile):
session = boto3.Session(profile_name=profile, region_name=region)
client = session.client('ssm')
runPwshDocument = 'AWS-RunPowerShellScript'
username = 'administrator'
passwordResetCommand = 'net user {username} {password}'.format(
username=username, password=password)
response = client.send_command(
InstanceIds=[instanceId],
DocumentName=runPwshDocument,
Parameters={
'commands': [passwordResetCommand]
}
)
return response['Command']
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Reset EC2 instance .\\Administrator password")
parser.add_argument('-i', '--instance', metavar='STRING', dest='instance',
type=str, default=None, help="Instance ID in the format of i-123456789")
parser.add_argument('-P', '--password', metavar='STRING', dest='password',
type=str, default="P@ssw0rd100#", help="Password for Administrator account")
parser.add_argument('-p', '--profile', metavar='STRING', dest='profile',
type=str, default=None, help="aws-cli profile name")
parser.add_argument('-r', '--region', metavar='STRING',
dest='region', type=str, default=None, help="AWS Region")
args = parser.parse_args()
results = resetPassword(
instanceId=args.instance,
password=args.password,
region=args.region,
profile=args.profile
)
print("Status: ", results['Status'])
print("Command ID: ", results['CommandId'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment