I've been in IT for a long time. It has been exciting to see how the industry has changed and how it as adapted to new technologies. Even so, I appreciate situations where sometimes the "old ways" are still the best ways. For example, we no longer really need the ancient lmhosts file to help resolve NETBIOS names to IP addresses. However, there may be a few cases where lmhosts solves a problem and then wouldn't it be nice to manage it with PowerShell?
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
I've been working on a project related to automating my teaching environment using Hyper-V and DSC. One of the tasks requires using remoting to connect to the new virtual machine, but the machine is on a private, NAT'd network so name resolution is sometimes an issue. This is where PowerShell Direct is so useful as it allows you to establish a remoting session over the VM bus. However, for now this only works from a Windows Server 2016 Hyper-V host to a Windows 10 or Windows Server 2016 virtual machine.
So I make sure my VMs have an IPAddress which I can then use with New-PSSession. But at some point I realized I wanted to use the computer name, hence the name for quick and dirty name resolution. That's a lot of back story which is probably irrelevant.
The lmhosts file is a plain text file that resides in C:\Windows\System32\Drivers\etc and is call lmhosts without any extension. You most likely don't have one although you will see a lmhosts.sam file which serves as a template. Essentially the file is a text list of IP address and computername.
172.16.30.200 chi-dc01 172.16.30.201 chi-dc02 172.16.30.203 chi-dc04 172.16.30.212 chi-hvr2
What I wanted was a PowerShell way to manage this file. I created a simple module called MyLmhost. It has 3 simple commands.
The commands use a combination of Get-Content with regular expressions to parse out the IP address and computer name and creates objects.
I can easily create new entries:
If the entry doesn't exist it will be added, otherwise this command will update the entry with the new IP address or name. And of course I can remove an entry.
Whenever you make a change to the file it will create a backup copy. The backup copy keeps getting overwritten every time you make a change.
I imagine many of you may not have a practical need for this module, but you may find the code educational. You can find it on Github at https://github.com/jdhitsolutions/Mylmhost. If you have any questions or problems, post an issue in the GitHub repository.
Enjoy!
Update: You can find the module on the PowerShell Gallery. Run Install-Module mylmhost.
I would certainly vote to publish this on PowerShell gallery. There definitely are scenarios where you build a set of VM’s on the same subnet (e.g., in a cloud Vnet) where you do _NOT_ have Active Directory and private DNS available. So a tool to manage LMHosts could be useful. (Have also done it with HOSTS files.)
could be a nice addition to this then:
https://github.com/richardszalay/pshosts