# Getting DNS\_HOSTNAME to work in windows

**URL:** https://forum.opennebula.io/t/getting-dns-hostname-to-work-in-windows/4945
**Category:** Integration Support
**Created:** [September 20, 2017, 1:47am UTC](https://forum.opennebula.io/t/getting-dns-hostname-to-work-in-windows/4945 "2017-09-20T01:47:54Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![vizo](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.opennebula.io/vizo/32/4559_2.png) [@vizo](https://forum.opennebula.io/u/vizo)
#### Post date: [September 20, 2017, 1:47am UTC](https://forum.opennebula.io/t/getting-dns-hostname-to-work-in-windows/4945/1 "2017-09-20T01:47:55Z")

</div>

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](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

---

<div class="post-metadata">

### Author: ![vholer](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.opennebula.io/vholer/32/4392_2.png) [@vholer](https://forum.opennebula.io/u/vholer)
#### Post date: [September 21, 2017, 4:53pm UTC](https://forum.opennebula.io/t/getting-dns-hostname-to-work-in-windows/4945/2 "2017-09-21T16:53:02Z")

</div>

Thank you! I have opened an issue [https://github.com/OpenNebula/addon-context-windows/issues/36](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
