# Chef powershell cookbook won't update the hostname

**URL:** <https://discourse.chef.io/t/chef-powershell-cookbook-wont-update-the-hostname/2119>\
**Category:** Chef Infra (archive)\
**Created:** [October 19, 2011, 4:52am UTC](https://discourse.chef.io/t/chef-powershell-cookbook-wont-update-the-hostname/2119 "2011-10-19T04:52:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kallen](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/kallen/32/1049_2.png) [@Kallen](https://discourse.chef.io/u/Kallen)\
**Post date:** [October 19, 2011, 4:52am UTC](https://discourse.chef.io/t/chef-powershell-cookbook-wont-update-the-hostname/2119/1 "2011-10-19T04:52:50Z")

</div>

hello. this still isn’t working for me: updating Windows hostname using  
powershell via chef isn’t working. can anyone lend a hand?

debug output of the initial chef-client run on a newly launched  
windows ec2 instance can be seen here:  
[http://groknaut.net/chef-windows-rehostname.txt](http://groknaut.net/chef-windows-rehostname.txt)

in that output, i notice how it’s invoked and that it ran successfully:  
[Tue, 18 Oct 2011 21:22:54 -0700] INFO: powershell[update-hostname] sh(c:/windows/system32/WindowsPowershell/v1.0/powers  
hell.exe -ExecutionPolicy RemoteSigned -Command “& { C:\DOCUME~1\ops\LOCALS~1\Temp\1\chef-script20111018-2940-1y3pd5y-0.ps1 }”)  
[Tue, 18 Oct 2011 21:22:55 -0700] INFO: powershell[update-hostname] ran successfully

if it would help reveal what’s going on, how could i run that by hand?  
this file is no longer present:  
C:\DOCUME~1\ops\LOCALS~1\Temp\1\chef-script20111018-2940-1y3pd5y-0.ps1  
i wonder if i can tell chef-client to leave that temp file in place so  
i can try to invoke it. if i had that file, how do i run it by hand? some  
kind of ruby/chef-client/shell\_out handwave handwave…?

details:

using the powershell cookbook, i’m trying to cause a newly launched  
ec2 instance to update its hostname based on userdata, but it’s not  
working. (i _did_ use ec2configservice to allow hostname updates.)

when i run the powershell script within powershell it works. i reboot  
the host, and indeed it comes back up with the new name. but when i try  
to run this powershell script via chef, the instance reboots, but it  
doesn’t come back with the new name.

below i show my recipe and the powershell script itself. any advice?

notice in the recipe i’ve tried to call the powershell script using  
2 different methods (one is commented out in the recipe). neither  
are working. are both invocations valid?

i also tried removing parts of the script that would write to STDOUT,  
in case that was making chef unhappy. didn’t seem to help.

node info:  
###################################################################  
[cheftain01]$ knife node show ip-DEADBEEF.ec2.internal  
Node Name: ip-DEADBEEF.ec2.internal  
Environment: \_default  
FQDN: ip-DEADBEEF.ec2.internal  
IP: 50.17.113.175  
Run List: role[winpower]  
Roles: winpower  
Recipes: windows::default, windows::reboot\_handler, powershell::default, powershell::rehostname  
Platform: windows 5.2.3790

recipe:  
###################################################################

# Cookbook Name:: powershell

# Recipe:: rehostname

# 

windows\_reboot 300 do  
reason 'Because chef said so’  
action :nothing  
end  
windows\_reboot 30 do  
action :cancel  
end

powershell “update-hostname” do  
powershell\_script = \<\<‘POWERSHELL\_SCRIPT’  
$webclient = new-object system.net.webclient  
$awsurl = “[http://169.254.169.254/latest/user-data](http://169.254.169.254/latest/user-data)”  
$userdata =“c:\Local\Chef\userdata.txt”  
$webClient.DownloadFile("$awsurl","$userdata")  
# -r foo -h sous-chef-win -e trex -c trex  
$namefromuserdata = Get-Content userdata | %{ \_.Split(’ ')[3]; }  
$computername = Get-Content env:computername  
Write-Host -ForegroundColor Yellow "userdata: $namefromuserdata…"  
Write-Host -ForegroundColor Yellow "computername: $computername…"  
if ( $namefromuserdata -ine $computername )  
{  
Write-Host -ForegroundColor Green “renaming $computername to $namefromuserdata.”  
$sysInfo = Get-WmiObject -Class Win32\_ComputerSystem  
$result = $sysInfo.Rename($namefromuserdata)  
switch($result.ReturnValue)  
{  
0 { Write-Host -ForegroundColor Green “Success: renamed $computername to $namefromuserdata.” }  
5 { Write-Host -ForegroundColor Red “This cmdlet must be execute with administrative privileges” }  
default { Write-Host -ForegroundColor Red “Error” }  
}  
}  
else { Write-Host -ForegroundColor Green “NOT renaming $computername to $namefromuserdata.” }  
POWERSHELL\_SCRIPT  
notifies :request, ‘windows\_reboot[300]’  
#notifies :create, “ruby\_block[remove\_rehostname]”, :immediately  
creates "C:\Local\Chef\hostname-updated.txt"  
end

=begin  
powershell “update-hostname” do  
command "C:\Local\bin\update-hostname.ps1"  
action :run  
notifies :request, 'windows\_reboot[30]'  
end  
=end

# not working yet:

ruby\_block “remove\_rehostname” do  
block do  
Chef::Log.info(“Recipe rehostname completed, removing the destructive recipe[powershell::rehostname]”)  
node.run\_list.remove(“recipe[powershell::rehostname]”) if node.run\_list.include?(“recipe[powershell::rehostname]”)  
end  
action :nothing  
end

update-hostname.ps1:  
###################################################################  
$webclient = new-object system.net.webclient  
$awsurl = “[http://169.254.169.254/latest/user-data](http://169.254.169.254/latest/user-data)”  
$userdata =“c:\Local\Chef\userdata.txt”  
$webClient.DownloadFile("$awsurl","$userdata")

# in our userdata, hostname is the 4th element in array:

$namefromuserdata = Get-Content userdata | %{ \_.Split(’ ')[3]; }  
$computername = Get-Content env:computername  
Write-Host -ForegroundColor Yellow "userdata: $namefromuserdata…"  
Write-Host -ForegroundColor Yellow "computername: $computername…"  
if ( $namefromuserdata -ine $computername )  
{  
Write-Host -ForegroundColor Green “renaming $computername to $namefromuserdata.”  
$sysInfo = Get-WmiObject -Class Win32\_ComputerSystem  
$result = $sysInfo.Rename($namefromuserdata)  
switch($result.ReturnValue)  
{  
0 { Write-Host -ForegroundColor Green “Success: renamed $computername to $namefromuserdata.” }  
5 { Write-Host -ForegroundColor Red “This cmdlet must be execute with administrative privileges” }  
default { Write-Host -ForegroundColor Red “Error” }  
}

else { Write-Host -ForegroundColor Green “NOT renaming $computername to $namefromuserdata.” }

thanks,  
kallen

---

<div class="post-metadata">

**Author:** ![Seth\_Chisamore](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/seth_chisamore/32/488_2.png) [@Seth\_Chisamore](https://discourse.chef.io/u/Seth_Chisamore)\
**Post date:** [October 28, 2011, 4:48am UTC](https://discourse.chef.io/t/chef-powershell-cookbook-wont-update-the-hostname/2119/2 "2011-10-28T04:48:42Z")

</div>

This is most likely a case of not running chef-client with elevated privileges. This can be accomplished in a number of ways:

# \*nix

- run chef-client under the root account..ie login as root or su to root.
- run chef-client with 'sudo'

# windows

- login into the Administrator account (NOTE - this is different than an account in the Administrators group)
- run the chef-client process from the Administrator account while being logged into another account (you will be prompted for your Administrator password.):

runas /user:Administrator "cmd /C chef-client -c c:/chef/client.rb"

- Open a Windows command prompt using UAC [0][1], run chef-client in this command prompt

As an FYI I was able to make a simplified version [2] of your code work on Windows Server 2003 and Windows Server 2008.

Hope that helps!

--  
Seth Chisamore  
Software Design Engineer, Opscode, Inc.  
IRC, Skype, Twitter, Github: schisamo

[0] [User Account Control - Wikipedia](http://en.wikipedia.org/wiki/User_Account_Control)  
[1] [https://s3.amazonaws.com/uploads.hipchat.com/7557/16329/j8tfcvx8yd57uua/Windows%20Server%202008%20R2%20x64%20Standard-2\_thumb.jpg](https://s3.amazonaws.com/uploads.hipchat.com/7557/16329/j8tfcvx8yd57uua/Windows%20Server%202008%20R2%20x64%20Standard-2_thumb.jpg)  
[2] [set\_hostname.rb · GitHub](https://gist.github.com/761eae49a1145aa3133e)

On Wednesday, October 19, 2011 at 12:52 AM, [kallen@groknaut.net](mailto:kallen@groknaut.net) wrote:

> hello. this still isn't working for me: updating Windows hostname using  
> powershell via chef isn't working. can anyone lend a hand?
> 
> debug output of the initial chef-client run on a newly launched  
> windows ec2 instance can be seen here:  
> [http://groknaut.net/chef-windows-rehostname.txt](http://groknaut.net/chef-windows-rehostname.txt)
> 
> in that output, i notice how it's invoked and that it ran successfully:  
> [Tue, 18 Oct 2011 21:22:54 -0700] INFO: powershell[update-hostname] sh(c:/windows/system32/WindowsPowershell/v1.0/powers  
> hell.exe -ExecutionPolicy RemoteSigned -Command "& { C:\DOCUME~1\ops\LOCALS~1\Temp\1\chef-script20111018-2940-1y3pd5y-0.ps1 }")  
> [Tue, 18 Oct 2011 21:22:55 -0700] INFO: powershell[update-hostname] ran successfully
> 
> if it would help reveal what's going on, how could i run that by hand?  
> this file is no longer present:  
> C:\DOCUME~1\ops\LOCALS~1\Temp\1\chef-script20111018-2940-1y3pd5y-0.ps1  
> i wonder if i can tell chef-client to leave that temp file in place so  
> i can try to invoke it. if i had that file, how do i run it by hand? some  
> kind of ruby/chef-client/shell\_out handwave handwave..?
> 
> details:
> 
> using the powershell cookbook, i'm trying to cause a newly launched  
> ec2 instance to update its hostname based on userdata, but it's not  
> working. (i _did_ use ec2configservice to allow hostname updates.)
> 
> when i run the powershell script within powershell it works. i reboot  
> the host, and indeed it comes back up with the new name. but when i try  
> to run this powershell script via chef, the instance reboots, but it  
> doesn't come back with the new name.
> 
> below i show my recipe and the powershell script itself. any advice?
> 
> notice in the recipe i've tried to call the powershell script using  
> 2 different methods (one is commented out in the recipe). neither  
> are working. are both invocations valid?
> 
> i also tried removing parts of the script that would write to STDOUT,  
> in case that was making chef unhappy. didn't seem to help.
> 
> node info:  
> ###################################################################  
> [cheftain01]$ knife node show ip-DEADBEEF.ec2.internal  
> Node Name: ip-DEADBEEF.ec2.internal  
> Environment: \_default  
> FQDN: ip-DEADBEEF.ec2.internal  
> IP: 50.17.113.175  
> Run List: role[winpower]  
> Roles: winpower  
> Recipes: windows::default, windows::reboot\_handler, powershell::default, powershell::rehostname  
> Platform: windows 5.2.3790
> 
> recipe:  
> ###################################################################
> 
> # Cookbook Name:: powershell
> 
> # Recipe:: rehostname
> 
> # 
> 
> windows\_reboot 300 do  
> reason 'Because chef said so'  
> action :nothing  
> end  
> windows\_reboot 30 do  
> action :cancel  
> end
> 
> powershell "update-hostname" do  
> powershell\_script = \<\<'POWERSHELL\_SCRIPT'  
> $webclient = new-object system.net.webclient  
> $awsurl = "[http://169.254.169.254/latest/user-data](http://169.254.169.254/latest/user-data)"  
> $userdata ="c:\Local\Chef\userdata.txt"  
> $webClient.DownloadFile("$awsurl","$userdata")
> 
> # -r foo -h sous-chef-win -e trex -c trex
> 
> $namefromuserdata = Get-Content $userdata | %{ $\_.Split(' ')[3]; }  
> $computername = Get-Content env:computername  
> Write-Host -ForegroundColor Yellow "userdata: $namefromuserdata...."  
> Write-Host -ForegroundColor Yellow "computername: $computername...."  
> if ( $namefromuserdata -ine $computername )  
> {  
> Write-Host -ForegroundColor Green "renaming $computername to $namefromuserdata."  
> $sysInfo = Get-WmiObject -Class Win32\_ComputerSystem  
> $result = $sysInfo.Rename($namefromuserdata)  
> switch($result.ReturnValue)  
> {  
> 0 { Write-Host -ForegroundColor Green "Success: renamed $computername to $namefromuserdata." }  
> 5 { Write-Host -ForegroundColor Red "This cmdlet must be execute with administrative privileges" }  
> default { Write-Host -ForegroundColor Red "Error" }  
> }  
> }  
> else { Write-Host -ForegroundColor Green "NOT renaming $computername to $namefromuserdata." }  
> POWERSHELL\_SCRIPT  
> notifies :request, 'windows\_reboot[300]'  
> #notifies :create, "ruby\_block[remove\_rehostname]", :immediately  
> creates "C:\Local\Chef\hostname-updated.txt"  
> end
> 
> =begin  
> powershell "update-hostname" do  
> command "C:\Local\bin\update-hostname.ps1"  
> action :run  
> notifies :request, 'windows\_reboot[30]'  
> end  
> =end
> 
> # not working yet:
> 
> ruby\_block "remove\_rehostname" do  
> block do  
> Chef::Log.info ([http://Log.info](http://Log.info))("Recipe rehostname completed, removing the destructive recipe[powershell::rehostname]")  
> node.run\_list.remove("recipe[powershell::rehostname]") if node.run\_list.include?("recipe[powershell::rehostname]")  
> end  
> action :nothing  
> end
> 
> update-hostname.ps1:  
> ###################################################################  
> $webclient = new-object system.net.webclient  
> $awsurl = "[http://169.254.169.254/latest/user-data](http://169.254.169.254/latest/user-data)"  
> $userdata ="c:\Local\Chef\userdata.txt"  
> $webClient.DownloadFile("$awsurl","$userdata")
> 
> # in our userdata, hostname is the 4th element in array:
> 
> $namefromuserdata = Get-Content $userdata | %{ $\_.Split(' ')[3]; }  
> $computername = Get-Content env:computername  
> Write-Host -ForegroundColor Yellow "userdata: $namefromuserdata...."  
> Write-Host -ForegroundColor Yellow "computername: $computername...."  
> if ( $namefromuserdata -ine $computername )  
> {  
> Write-Host -ForegroundColor Green "renaming $computername to $namefromuserdata."  
> $sysInfo = Get-WmiObject -Class Win32\_ComputerSystem  
> $result = $sysInfo.Rename($namefromuserdata)  
> switch($result.ReturnValue)  
> {  
> 0 { Write-Host -ForegroundColor Green "Success: renamed $computername to $namefromuserdata." }  
> 5 { Write-Host -ForegroundColor Red "This cmdlet must be execute with administrative privileges" }  
> default { Write-Host -ForegroundColor Red "Error" }  
> }
> 
> else { Write-Host -ForegroundColor Green "NOT renaming $computername to $namefromuserdata." }
> 
> thanks,  
> kallen

---

<div class="post-metadata">

**Author:** ![Kallen](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/kallen/32/1049_2.png) [@Kallen](https://discourse.chef.io/u/Kallen)\
**Post date:** [November 1, 2011, 9:15pm UTC](https://discourse.chef.io/t/chef-powershell-cookbook-wont-update-the-hostname/2119/3 "2011-11-01T21:15:24Z")

</div>

solved .. more or less. if you're interested in the outcome:

> **[Chef Support for Automation & DevOps | Chef](https://www.chef.io/support)**
>
> Need help? Get in touch with the Chef Support team via email, social media, or support ticket. Learn more about how to find the right Chef support for your needs.

i still have open questions, but moving forward now.

kallen

On Tue, 18 Oct 2011, [kallen@groknaut.net](mailto:kallen@groknaut.net) wrote:

> hello. this still isn't working for me: updating Windows hostname using  
> powershell via chef isn't working. can anyone lend a hand?
> 
> debug output of the initial chef-client run on a newly launched  
> windows ec2 instance can be seen here:  
> [http://groknaut.net/chef-windows-rehostname.txt](http://groknaut.net/chef-windows-rehostname.txt)
> 
> in that output, i notice how it's invoked and that it ran successfully:  
> [Tue, 18 Oct 2011 21:22:54 -0700] INFO: powershell[update-hostname] sh(c:/windows/system32/WindowsPowershell/v1.0/powers  
> hell.exe -ExecutionPolicy RemoteSigned -Command "& { C:\DOCUME~1\ops\LOCALS~1\Temp\1\chef-script20111018-2940-1y3pd5y-0.ps1 }")  
> [Tue, 18 Oct 2011 21:22:55 -0700] INFO: powershell[update-hostname] ran successfully
> 
> if it would help reveal what's going on, how could i run that by hand?  
> this file is no longer present:  
> C:\DOCUME~1\ops\LOCALS~1\Temp\1\chef-script20111018-2940-1y3pd5y-0.ps1  
> i wonder if i can tell chef-client to leave that temp file in place so  
> i can try to invoke it. if i had that file, how do i run it by hand? some  
> kind of ruby/chef-client/shell\_out handwave handwave..?
> 
> details:
> 
> using the powershell cookbook, i'm trying to cause a newly launched  
> ec2 instance to update its hostname based on userdata, but it's not  
> working. (i _did_ use ec2configservice to allow hostname updates.)
> 
> when i run the powershell script within powershell it works. i reboot  
> the host, and indeed it comes back up with the new name. but when i try  
> to run this powershell script via chef, the instance reboots, but it  
> doesn't come back with the new name.
> 
> below i show my recipe and the powershell script itself. any advice?
> 
> notice in the recipe i've tried to call the powershell script using  
> 2 different methods (one is commented out in the recipe). neither  
> are working. are both invocations valid?
> 
> i also tried removing parts of the script that would write to STDOUT,  
> in case that was making chef unhappy. didn't seem to help.
> 
> node info:  
> ###################################################################  
> [cheftain01]$ knife node show ip-DEADBEEF.ec2.internal  
> Node Name: ip-DEADBEEF.ec2.internal  
> Environment: \_default  
> FQDN: ip-DEADBEEF.ec2.internal  
> IP: 50.17.113.175  
> Run List: role[winpower]  
> Roles: winpower  
> Recipes: windows::default, windows::reboot\_handler, powershell::default, powershell::rehostname  
> Platform: windows 5.2.3790
> 
> recipe:  
> ###################################################################
> 
> # Cookbook Name:: powershell
> 
> # Recipe:: rehostname
> 
> # 
> 
> windows\_reboot 300 do  
> reason 'Because chef said so'  
> action :nothing  
> end  
> windows\_reboot 30 do  
> action :cancel  
> end
> 
> powershell "update-hostname" do  
> powershell\_script = \<\<'POWERSHELL\_SCRIPT'  
> $webclient = new-object system.net.webclient  
> $awsurl = "[http://169.254.169.254/latest/user-data](http://169.254.169.254/latest/user-data)"  
> $userdata ="c:\Local\Chef\userdata.txt"  
> $webClient.DownloadFile("$awsurl","$userdata")  
> # -r foo -h sous-chef-win -e trex -c trex  
> $namefromuserdata = Get-Content $userdata | %{ $\_.Split(' ')[3]; }  
> $computername = Get-Content env:computername  
> Write-Host -ForegroundColor Yellow "userdata: $namefromuserdata...."  
> Write-Host -ForegroundColor Yellow "computername: $computername...."  
> if ( $namefromuserdata -ine $computername )  
> {  
> Write-Host -ForegroundColor Green "renaming $computername to $namefromuserdata."  
> $sysInfo = Get-WmiObject -Class Win32\_ComputerSystem  
> $result = $sysInfo.Rename($namefromuserdata)  
> switch($result.ReturnValue)  
> {  
> 0 { Write-Host -ForegroundColor Green "Success: renamed $computername to $namefromuserdata." }  
> 5 { Write-Host -ForegroundColor Red "This cmdlet must be execute with administrative privileges" }  
> default { Write-Host -ForegroundColor Red "Error" }  
> }  
> }  
> else { Write-Host -ForegroundColor Green "NOT renaming $computername to $namefromuserdata." }  
> POWERSHELL\_SCRIPT  
> notifies :request, 'windows\_reboot[300]'  
> #notifies :create, "ruby\_block[remove\_rehostname]", :immediately  
> creates "C:\Local\Chef\hostname-updated.txt"  
> end
> 
> =begin  
> powershell "update-hostname" do  
> command "C:\Local\bin\update-hostname.ps1"  
> action :run  
> notifies :request, 'windows\_reboot[30]'  
> end  
> =end
> 
> # not working yet:
> 
> ruby\_block "remove\_rehostname" do  
> block do  
> Chef::Log.info("Recipe rehostname completed, removing the destructive recipe[powershell::rehostname]")  
> node.run\_list.remove("recipe[powershell::rehostname]") if node.run\_list.include?("recipe[powershell::rehostname]")  
> end  
> action :nothing  
> end
> 
> update-hostname.ps1:  
> ###################################################################  
> $webclient = new-object system.net.webclient  
> $awsurl = "[http://169.254.169.254/latest/user-data](http://169.254.169.254/latest/user-data)"  
> $userdata ="c:\Local\Chef\userdata.txt"  
> $webClient.DownloadFile("$awsurl","$userdata")
> 
> # in our userdata, hostname is the 4th element in array:
> 
> $namefromuserdata = Get-Content $userdata | %{ $\_.Split(' ')[3]; }  
> $computername = Get-Content env:computername  
> Write-Host -ForegroundColor Yellow "userdata: $namefromuserdata...."  
> Write-Host -ForegroundColor Yellow "computername: $computername...."  
> if ( $namefromuserdata -ine $computername )  
> {  
> Write-Host -ForegroundColor Green "renaming $computername to $namefromuserdata."  
> $sysInfo = Get-WmiObject -Class Win32\_ComputerSystem  
> $result = $sysInfo.Rename($namefromuserdata)  
> switch($result.ReturnValue)  
> {  
> 0 { Write-Host -ForegroundColor Green "Success: renamed $computername to $namefromuserdata." }  
> 5 { Write-Host -ForegroundColor Red "This cmdlet must be execute with administrative privileges" }  
> default { Write-Host -ForegroundColor Red "Error" }  
> }
> 
> else { Write-Host -ForegroundColor Green "NOT renaming $computername to $namefromuserdata." }
> 
> thanks,  
> kallen
