summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn McCrae <jmccrae@chef.io>2021-05-10 11:18:19 -0700
committerJohn McCrae <jmccrae@chef.io>2021-05-11 11:48:26 -0700
commitf06da21dfc7d4b255b94967fb4052405918b1c7e (patch)
tree006a528d55d4377bb2086cc35a38395594705fab
parent45a9ffdd2d5f0278e348814bc1190579aa68d8de (diff)
downloadchef-f06da21dfc7d4b255b94967fb4052405918b1c7e.tar.gz
changed the mechanism for verifying domain joined vs workgroup joined
Signed-off-by: John McCrae <jmccrae@chef.io>
-rw-r--r--lib/chef/resource/hostname.rb25
1 files changed, 19 insertions, 6 deletions
diff --git a/lib/chef/resource/hostname.rb b/lib/chef/resource/hostname.rb
index 1fff18dc6c..e7577dab73 100644
--- a/lib/chef/resource/hostname.rb
+++ b/lib/chef/resource/hostname.rb
@@ -243,12 +243,25 @@ class Chef
end
unless Socket.gethostbyname(Socket.gethostname).first == new_resource.hostname
- converge_by "set hostname to #{new_resource.hostname}" do
- puts "What username is being used : #{new_resource.domain_user}"
- powershell_exec! <<~EOH
- $sysInfo = Get-WmiObject -Class Win32_ComputerSystem
- $sysInfo.Rename("#{new_resource.hostname}")
- EOH
+ if is_domain_joined?
+ if new_resource.domain_user.nil? || new_resource.domain_password.nil?
+ raise "Domain Username and Password are required parameters"
+ else
+ converge_by "set hostname to #{new_resource.hostname}" do
+ powershell_exec! <<~EOH
+ $user = #{new_resource.domain_user}
+ $secure_password = #{new_resource.domain_password} | Convertto-SecureString -AsPlainText -Force
+ $Credentials = New-Object System.Management.Automation.PSCredential -Argumentlist ($user, $secure_password)
+ Rename-Computer -NewName #{new_resource.hostname} -DomainCredential $Credentials
+ EOH
+ end
+ end
+ else
+ converge_by "set hostname to #{new_resource.hostname}" do
+ powershell_exec! <<~EOH
+ Rename-Computer -NewName #{new_resource.hostname}
+ EOH
+ end
end
# reboot because $windows