Skip to content

Instantly share code, notes, and snippets.

@jessereynolds
Last active March 2, 2017 05:14
Show Gist options
  • Save jessereynolds/0605654a1570b8d4d7d2e4a987b1b0d7 to your computer and use it in GitHub Desktop.
Save jessereynolds/0605654a1570b8d4d7d2e4a987b1b0d7 to your computer and use it in GitHub Desktop.
windows-rename-and-domain-join
# Alternative idea - do the rename and domain join in one go (thanks Jessykah)
$username = 'jane'
$password = 'password'
$computer_name = 'foo'
$command_parts = [
'$username = "', $username, '" ; ',
'$password = ConvertTo-SecureString "', $password, '" -AsPlainText -Force ; ',
'$credentials = new-object System.Management.Automation.PSCredential($username,$password) ; ',
'if (Add-Computer -DomainName Sample.domain -Credential $credentials -NewName "', $computer_name, '") ',
'{ exit 0 } else { exit 1 }',
]
$command_with_exit_code = join($command_parts, '')
# Without the -Restart a successful rename gives a warning, which is then interpreted
# as a failure of the command, preventing the reboot and subsequent domain join from occuring.
#
# It would be better if I could find a way of having the powershell command properly return 0 for
# success and 1 for failure
$computer_name = 'foo'
$command = "Rename-Computer -NewName '${computer_name}' -Restart"
exec {'rename_computer':
command => "if (${command}) { exit 0 } else { exit 1 }",
unless => "if ((hostname) -eq '${computer_name}') { exit 0 } else { exit 1 }",
provider => "powershell",
logoutput => true,
notify => Reboot['after_rename_computer'],
}
reboot {'after_rename_computer':
when => 'refreshed',
}
class {'domain_membership':
domain => 'example.com',
username => 'jane',
password => 'password',
machine_ou => 'OU=aws,DC=example,DC=com,
reboot => true,
join_options => '3',
require => Reboot['after_rename_computer'],
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment