PowerShell to rename a Hyper-V VM’s hostname from the Hyper-V host

4

PowerShell to rename a Hyper-V VM’s hostname from the Hyper-V host

PowerShell v3 in Windows Server 2012 opens up way more stuff than ever before for automation. I like my VMs to have a host name that matches the name I give the VM in on the Hyper-V host – that way when I write a PowerShell script to build 50 VMs they get useful names and they can all be created from the same VHD image. So I rename my Hyper-V VMs from my Hyper-V host.

I’ve shown lots of people this script at our UK IT Camps and generally people are all a bit like “why would you want to do that”. Well, personally, I create lots of demo enviornments but as a real IT guy you might want to quickly spin up a cloud.

Lets take a look at some PowerShell:

functionRename-UnknownVM ($VMtoRename,$NewVMName)
{


$vmip=Get-VMNetworkAdapter$VMToRename|whereswitchname-eq"CorpNet"| `


select-expandproperty"IPAddresses"|where {$_-match"^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$"}


$vmHostName=[system.net.dns]::GetHostEntry($vmip)


$vmHostName=$vmHostName.HostName


Invoke-Command-ComputerName$vmHostName-ScriptBlock {


rename-Computer-NewName$args[0]-DomainCredentialcontoso\administrator-Restart } -ArgumentList$NewVMName
}
Rename-UnknownVM


-VMtoRename rdsb-NewVMName rdsb 

So what’s happening, lets start at the bottom. Obviously everything is wrapped in a function to make it easy to call in the above RDSB  is the name of my VM – it’s the VM name and it’s the name that I want to rename it to. The function then goes and gets the named VM’s network adapter, it then finds just the network interface attached to a named virtual switched “CorpNet”, then we get the IP Address property which is stored as text so we match it on a RegEx. That gives us the IP of the VM.

Next we need to get the current name of the VM so we do that from DNS, we need this because the “rename-computer” cmdlet needs the name, not the IP Address a host to rename it.

Finally I use an invoke-commend script block to remotely execute the rename on the VM and the admin running the script is prompted for a password.

Simples. Actually not so much I’d like to thank Jonathan Noble for his help here.

Share with your network so they know you're THE mobility expert
PowerShell to rename a Hyper V VMs hostname from the Hyper V host10PowerShell to rename a Hyper V VMs hostname from the Hyper V host0PowerShell to rename a Hyper V VMs hostname from the Hyper V host2PowerShell to rename a Hyper V VMs hostname from the Hyper V host12PowerShell to rename a Hyper V VMs hostname from the Hyper V hostPowerShell to rename a Hyper V VMs hostname from the Hyper V hostPowerShell to rename a Hyper V VMs hostname from the Hyper V host0PowerShell to rename a Hyper V VMs hostname from the Hyper V host0PowerShell to rename a Hyper V VMs hostname from the Hyper V host

4 comments

  1. Henry - May 28, 2013 5:23 pm

    Awesome post Simon (guessing that’s your name), but i would like to clarify with you here.. this will only work if AD is already setup with DNS and DHCP?

    Reason i ask this, well i was about to do the same thing, just wanted to use the invoke-command and realize i don’t know what my stock VMs computers name were, as i’m only building up a LAB to test all this.

    Then realize your script may not work for me when I’m building a Hyper V server and loading up all these VMs… when their not part of a network yet…

    Cheers, hope to hear back from you on this .. thanks for sharing.. read books on powershell, finally playing with it now…

    Reply
    • Simon May - July 11, 2013 11:38 am

      @Henry,

      Sorry I don’t seem to be seeing comment notifications for my blog – will fix.

      This should work just fine in a lab, as long as you do have working DNS and your servers can update DNS. In my case all my servers are indeed part of a domain and I actually use Syspreped VMs with an answer file included within them, this generates a random name and joins the domain.

      Cheers

      Si

      Reply
      • Henry - July 11, 2013 11:57 am

        Thanks Simon, awesome, which means i only have to get the AD going, then apply this method to all additional VMs, as i’ve noticed this will not work, if it’s not part of AD/DNS.. cause you know what i mean.. like you can’t get the list of computers name to match up.. more troublesome i believe.. sorry spoke out of my mind the last few words… but i’m sure you get what i mean!

        Cheers dude.. will follow your postings!

        Reply
  2. Hammer - June 10, 2013 4:55 pm

    This is exactly what I was looking for, thank you so much.

    Reply

What do you think?

Simon May is an Infrastructure Technology Evangelist at Microsoft concentrating on Devices and Services but with special interests in deployment and device management. Simon is a professional public speaker and the author of several books on Windows.