Getting DNS_HOSTNAME to work in windows

I will start out by saying I am not very good powershell but I think I have found a way to get DNS_Hostname variable to work in windows, maybe some one out there can tweak to make it cleaner.
First I put this dll in my context files http://simpledns.com/dns-client-lib.aspx
Then I edit the rename function in context.ps1

function renameComputer($context) {
$dnshostname = "DNS_HOSTNAME"
if ($context[$dnshostname] -eq “YES”) {
$dnsclient = $contextDrive.Name + “JHSoftware.DnsClient”
[System.Reflection.Assembly]::LoadFile($dnsclient)

Write-Host $dnsclient
$dnsip = Get-NetIPAddress -AddressFamily IPv4 |where { $_.InterfaceAlias -notmatch ‘Loopback’}
$hostname = [JHSoftware.DNSClient]::LookupReverse($dnsip.IPAddress) -replace ‘(.+?)…+’,’$1’
Write-Output $hostname | add-content c:\hostname.txt
$ComputerInfo = Get-WmiObject -Class Win32_ComputerSystem
$ComputerInfo.rename($hostname)

}
Else {($hostname = $context[“SET_HOSTNAME”])
if ($hostname) {
$ComputerInfo = Get-WmiObject -Class Win32_ComputerSystem
$ComputerInfo.rename($hostname)
}

}
}

so far works like a charm hope this can help some one else

Thank you! I have opened an issue https://github.com/OpenNebula/addon-context-windows/issues/36 with a link to your example. I hope it’ll be possible to use internal resolving functions, instead of depending on external DLL. If you want, you can try to make the change in code on your own and open pull request against mentioned repo.

Best regards,
Vlastimil Holer