Updating hostname with powershell via chef not working


#1

hellooo. i have 2 questions –

  1. updating Windows hostname using powershell via chef isn’t working
    (implied question is “hallllp?”)

  2. what’s the proper way to update a renamed host in the chef server?

first, 1) updating Windows hostname using powershell via chef isn’t working

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. (yep, i did make the needed change using ec2configservice
in order to allow a hostname update).

when i run the powershell script by hand 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.

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

Cookbook Name:: powershell

Recipe:: nameservice

windows_reboot 30 do
reason 'Because chef said so’
action :nothing
end
windows_reboot 30 do
action :cancel
end

=begin
powershell “update-hostname” do
powershell_script = <<‘POWERSHELL_SCRIPT’
$webclient = new-object system.net.webclient
[snip for brevity. see update-hostname.ps1 below]
POWERSHELL_SCRIPT
notifies :request, 'windows_reboot[30]'
end
=end

powershell “update-hostname” do
command "C:\Local\bin\update-hostname.ps1"
action :run
notifies :request, 'windows_reboot[30]'
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.” }

and now, 2) what’s the proper way to update a renamed host in the
chef server?

upon birth, an ec2 windows instance’s hostname uses its ip-to-hex. the
initial chef-client run registers this name with chef server as a client
and a node. (i realize i could try to rename the instance before the
first chef-client run…).

my intention is for the initial chef-client run to update the hostname.
but after the rename, the instance can’t auth to the chef-server.

is it correct to “knife node edit $node-name” and “knife client edit
$client-name” on the server to update these to the new name? and then
delete the old node and client? if it is correct, anyone have an easy
way to do this (i.e. non-manual)?

related question i’ll toss in: is there an easy way to delete a client
when said client is going to be redeployed using the same name?

(i could swear i saw a thread on the list talking about something like
this, but i can’t find it now.)

thank you so much!
kallen