Chef powershell cookbook won't update the hostname


#1

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

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
$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
$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


#2

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] 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
[2] https://gist.github.com/761eae49a1145aa3133e

On Wednesday, October 19, 2011 at 12:52 AM, 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

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
$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)(“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
$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


#3

solved … more or less. if you’re interested in the outcome:
http://help.opscode.com/discussions/problems/853-powershell-cookbook-wont-update-the-hostname

i still have open questions, but moving forward now.

kallen

On Tue, 18 Oct 2011, 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

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
$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
$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