description = 'rename computer account'
name = 'rename-computer'
args = dict(
	domainmode=dict(action='store_true', help='rename joined computer'),
	name=dict(required=True, help='new name'),
)


def post(winrm):
	if winrm.args.domainmode:
		ps = '''
$user = '%(user)s'
$pass = ConvertTo-SecureString -String "%(password)s" -AsPlainText -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ($user, $pass)
Rename-Computer -NewName "%(name)s" -DomainCredential $cred -Restart
''' % vars(winrm.args)
	else:
		ps = 'Rename-Computer -NewName "%(name)s" -Restart' % vars(winrm.args)
	winrm.run(ps)
	winrm.wait_until_client_is_gone()
	winrm.wait_for_client()
	winrm.run('$env:computername', silent=True)
	assert winrm.args.name.lower() == winrm.last_stdout.strip().lower()
