none
want to rename 100Virtual machines RRS feed

  • Question

  • Hi

    I want to rename 100 virtual machines in my environment like below

    tcsvm103 will be renamed as tcsvm103_donotpoweron

    but whenever I run the below script, it throw me errors

    $serverlist = get-content c:\serverlist.txt

    function append-text {
      process{
       foreach-object {$_ + "_donotpoweron"}
        }
      }

    $newservername = $serverlist | append-text

    Set-vm -vm $serverlist -name $newservername

    Saturday, September 26, 2015 3:08 AM

Answers

  • Hi Asharma,

    I too wondering how its working without Hyper-V role installed in the management server. Anyways if Get-VM is listing all the required servers then you should be good to go.

    You already have the loop just move in the Set- cmdlet inside. Rename the function as you like, we don't really need it.

    $serverlist = get-content c:\serverlist.txt
     
    function append-text { 
      process{
        foreach-object {
    			$newservername = $_ + "_donotpoweron"
    			Set-vm -Name $_ -NewVMName $newservername
    			
    		    }
         } 
      }
     
    $serverlist | append-text

    One liner,

    get-content c:\serverlist.txt | %{Set-vm -Name $_ -NewVMName "$_`_donotpoweron"}

    NOTE- These command refer to Hyper-V roles, not the VMware PowerCLI cmdlets, please test and modify accordinly before using them.


    Regards,

    Satyajit

    Please“Vote As Helpful” if you find my contribution useful or “MarkAs Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.


    • Edited by Satyajit321 Monday, September 28, 2015 6:35 AM
    • Marked as answer by asharma5 Thursday, October 1, 2015 8:37 AM
    Monday, September 28, 2015 6:31 AM

All replies

  • How can you rename a VM by assigning a value to an object?   The command is "Rename-Vm" - look it up for detailed instructions.


    \_(ツ)_/

    Saturday, September 26, 2015 4:39 AM
  • Hi Asharma,

    What's the error?

    Try to put the Set-VM inside the forloop and rename one by one.

    Secondly Set-VM [-VM] and Set-VM [-Name] belongs to two different parameter sets ,hence can't be called at the same time. More over you need to use -NewVMName<String> to rename it.

    Try something like:

    Get-VM -Name $server | Set-vm -NewVMName $newservername

    Lastly are all 100VMs in the same Hyper-V host, where you are running the script. Cause if not, its not going to find the other VMs I guess.

    Use -ComputerName parameter with the Get-VM as well.

    I'm wondering if you ever read the Syntax for Set-VM.




    Regards,

    Satyajit

    Please“Vote As Helpful” if you find my contribution useful or “MarkAs Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.


    • Edited by Satyajit321 Saturday, September 26, 2015 9:16 AM
    Saturday, September 26, 2015 9:15 AM
  • Hi Satyajit,

    My script work fine when I enter only one VM in text file. But when I enter more then one VM's name ,then I encounter errors.

    And I am working in VMware environment, there I can connect multiple VCenter at one time and can access all the VM's. So I was able to find all the listed VM's.

    below listed command will not help as it will rename all VM's to first name mention in $computername variable.

    Get-VM -Name $server | Set-vm -NewVMName $newservername

    I think for loop will wok. Can you help me out in for loop for this script.

    Sunday, September 27, 2015 8:47 AM
  • These commands are for Hyper-V.  You are using VMWare.  Please post in VMWare forum for instructions on how to use VMWare.

    \_(ツ)_/

    Sunday, September 27, 2015 2:40 PM
  • commands are same in VMware and Hyper-v.
    Monday, September 28, 2015 2:41 AM
  • Hi Asharma,

    I too wondering how its working without Hyper-V role installed in the management server. Anyways if Get-VM is listing all the required servers then you should be good to go.

    You already have the loop just move in the Set- cmdlet inside. Rename the function as you like, we don't really need it.

    $serverlist = get-content c:\serverlist.txt
     
    function append-text { 
      process{
        foreach-object {
    			$newservername = $_ + "_donotpoweron"
    			Set-vm -Name $_ -NewVMName $newservername
    			
    		    }
         } 
      }
     
    $serverlist | append-text

    One liner,

    get-content c:\serverlist.txt | %{Set-vm -Name $_ -NewVMName "$_`_donotpoweron"}

    NOTE- These command refer to Hyper-V roles, not the VMware PowerCLI cmdlets, please test and modify accordinly before using them.


    Regards,

    Satyajit

    Please“Vote As Helpful” if you find my contribution useful or “MarkAs Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.


    • Edited by Satyajit321 Monday, September 28, 2015 6:35 AM
    • Marked as answer by asharma5 Thursday, October 1, 2015 8:37 AM
    Monday, September 28, 2015 6:31 AM