summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn McCrae <jmccrae@chef.io>2021-05-06 16:24:41 -0700
committerJohn McCrae <jmccrae@chef.io>2021-05-06 16:24:41 -0700
commitc676bd3a99cb734d07cab6272418c7dcb3677ebc (patch)
tree46e1c3c1682df70c02b358dcf72a3d08b054d4a2
parent364b9236f4a90b00aee9a91a18651cbcc4e38e6b (diff)
downloadchef-c676bd3a99cb734d07cab6272418c7dcb3677ebc.tar.gz
Updated the Windows code to use Rename-Computer, added properties for a domain username and password.
Signed-off-by: John McCrae <jmccrae@chef.io>
-rw-r--r--lib/chef/resource/hostname.rb35
1 files changed, 33 insertions, 2 deletions
diff --git a/lib/chef/resource/hostname.rb b/lib/chef/resource/hostname.rb
index 8a9d3bea13..03fcb03d91 100644
--- a/lib/chef/resource/hostname.rb
+++ b/lib/chef/resource/hostname.rb
@@ -44,6 +44,24 @@ class Chef
ipaddress '198.51.100.2'
end
```
+
+ **Change the hostname of a Windows, Non-Domain joined node**:
+
+ ```ruby
+ hostname 'renaming a workgroup computer' do
+ hostname 'Foo'
+ end
+ ```
+
+ **Change the hostname of a Windows, Domain-joined node**:
+
+ ```ruby
+ hostname 'renaming a domain-joined computer' do
+ hostname 'Foo'
+ windows_domain_username 'Domain\Someone'
+ windows_user_password 'SomePassword'
+ end
+ ```
DOC
property :hostname, String,
@@ -71,6 +89,12 @@ class Chef
description: "Determines whether or not Windows should be reboot after changing the hostname, as this is required for the change to take effect.",
default: true
+ property :windows_domain_username, String,
+ description: "The domain username with permissions to change the local hostname. Specified in the form of 'Domain\User'"
+
+ property :windows_user_password, String,
+ description: "The password associated with the domain user account"
+
action_class do
def append_replacing_matching_lines(path, regex, string)
text = IO.read(path).split("\n")
@@ -245,8 +269,15 @@ class Chef
unless Socket.gethostbyname(Socket.gethostname).first == new_resource.hostname
converge_by "set hostname to #{new_resource.hostname}" do
powershell_exec! <<~EOH
- $sysInfo = Get-WmiObject -Class Win32_ComputerSystem
- $sysInfo.Rename("#{new_resource.hostname}")
+ if ([string]::IsNullOrEmpty(#{new_resource.windows_domain_username})){
+ Rename-Computer -NewName #{new_resource.hostname}
+ }
+ else {
+ $user = #{new_resource.windows_domain_username}
+ $securepassword = #{new_resource.windows_user_password} | Convertto-SecureString -AsPlainText -Force
+ $Credentials = New-Object System.Management.Automation.PSCredential -Argumentlist ($user, $securepassword)
+ Rename-Computer -NewName #{new_resource.hostname} -DomainCredential $Credemtials
+ }
EOH
end