PowerCLI

 View Only
  • 1.  Changing DNS name of VM

    Posted Jun 17, 2011 01:00 PM

    Hi, i am a newbie to powercli and after a few sleepless nights, some coffee and lots of reading i have put together a script that deploys some sample VMs for demo purposes. But when I use the script to roll out the VMs from template, the VMs are correctly named but the DNS name is exactly the same as the template - which is expected as I am not using any customisation for each VM.

    Is there a way to change the DNS name (not the VM - the VM name is correct when I look into the properties of the VM) name using command line or do I have to use a customisation setup? VM name for each VM is DemoVM and not DemoVMx

    My commands are:

    # Add esx host to vCenter

    Add-VMHost -Name esxdemo -Location PODDemo -User root -Password xxxx -Force


    # Deploy Demo VMs


    Get-Template DemoVM | New-VM –Name DemoVM1 -VMHost esxdemo -Datastore datastore1_esxdemo
    Get-Template DemoVM | New-VM –Name DemoVM2 -VMHost esxdemo -Datastore datastore1_esxdemo


    # Remove hosts from management vCenter

    Connect-VIServer -Server vCenter -Protocol https -User Administrator -Password xxxxxx


    Remove-VMHost -VMHost esxdemo -Server vCenter -confirm:$false


    # Connect to each ESXi host in turn and rename the local datastore.

    Connect-VIServer -Server esxdemo -Protocol https -User root -Password xxxx
    Get-Datastore -VMHost esxdemo -Name datastore1_esxdemo | Set-Datastore -Name datastore1

    Any help would be appreciated.

    Many thanks



  • 2.  RE: Changing DNS name of VM

    Posted Jun 17, 2011 02:05 PM

    This is a bit of a chicken and the egg problem.

    If you have the VMware Tools installed in these guests you could use the Invoke-VMScript cmdlet to run the rename (through a bat or bash file) inside the guest OS.

    Depending on the type of OS you have running inside the guest, the way to do the rename will  change.

    For a Windows OS you could use the netdom command (has to copied into the template) or you could use the WMI class called

    Win32_ComputerSystem and use the rename method on that class.

    If it is a Linux type OS you could just update the hostname entry in the /etc/sysconfig/network file. Note that this can be another file depending on the Linux distro you are using.



  • 3.  RE: Changing DNS name of VM

    Posted Jun 17, 2011 02:50 PM

    HI Luc, thanks for the response. I will give your suggestions a try next week and see how I get on.

    The VMs are Windows VMs so it is going to have to be bat or bash.

    Again, many thanks for your response.



  • 4.  RE: Changing DNS name of VM

    Posted Jun 17, 2011 03:02 PM

    If your guests have a Windows OS you cna use a bat file or a PowerShell script.

    The latter, provided you have PowerShell installed.

    On W2K8 R2 and Win7 it should be there by default.

    With PowerShell it should be straightforward to do the WMI based rename with the Get-WmiObject cmdlet.



  • 5.  RE: Changing DNS name of VM

    Posted Dec 12, 2011 01:56 PM

    Invoke these commands to change windows hostname from command line. The first command doesn't work on windows 2008 for some reason.

    I just run one after another, because I'm dealing with many types of windows versions, and I figure it won't hurt if one command fails.

    "wmic computersystem where name=`"%COMPUTERNAME%`" call rename name=`"$hostname`" "


    # second try, use netdom (works on win2008)


    "netdom renamecomputer localhost /newname:$hostname /force"