PowerCLI script to deploy virtual machines from a template

Hey all,.

I have this part done script. My questions are:

1. I want to know the nubmber of "Windows XP - X" are currently deployed, so I want to add. I don't want to use a CSV format or any other to inmport of. Yes, I am that lazy. For some reason I can't understand it!

2. on the line I would askes 'what esx host you want to use' just hit '1' for esx1 vs type the name of the host.

The questions above can be made? If so HELP! I worked on it for days. Thank you!

  1. Yes I have a Connect-VIServer-Server blah blah blah... Just removed.



$freespaceCalc = @{name = "FreespaceGB"; Expression = { : tour ($_.)} (FreeSpaceMB * 1 MB/1 GB)}}

$capacityCalc = @{name = "CapacityGB"; Expression = { : tour ($_.)} (CapacityMB * 1 MB/1 GB)}}

  1. User agents how manny are required

$agentTotal = Read-Host "Enter the number of Agents WinXp you need."

$agnetTotal = $agentTotal

Get-Datastore. Select Name, $freespaceCalc, $capacityCalc | Sort-Object Name. FT - force - AutoSize. Out-Default

$datastore = Read-Host "what data store you want to use?

Get-VMHost | Select-Object Name. Sort name | FT - force - AutoSize. Out-Default

$esxHost = Read-Host "what Esx host you want to use?

$XP is get - VM - name WinXP-*.

$totalXPCount = @($XP). Length

$totalXP = (1 + $totalXPCount)

for ($adt = ($totalXP + $agentTotal); $totalXP - $adt lt; $totalXP ++) {}

If ($totalXPCount - eq $adt)




$machineName = "WinXP-$totalXPCount.

$template = "Template XP-32.

$osspec = get-OSCustomizationSpec XP

foreach ($vm to $machineName)

{$vm = New - VM - name $vm - model $template - host $esxHost - store of data $datastore - OSCustomizationSpec $osspec | start-vm}



The $hostIndex variable contains the index in the table $hostList of the selected host.

This is the script has been fixed.

Note that the SW forum has problems with hooks for you better use the attached file.

# Current "** Create Win XP **";
$freespaceCalc = @{ Name = "FreespaceGB"; Expression = { [Math]::Round($_.FreeSpaceMB * 1MB / 1GB ) } }
$capacityCalc = @{ Name = "CapacityGB"; Expression = { [Math]::Round($_.CapacityMB * 1MB / 1GB) } }

# User enters how manny agents are requred
$agentTotal = [int](Read-Host "Enter how many WinXp Agents you need.")

$totalXPCount = (Get-VM -Name WinXP-* | Measure-Object).Count
if($totalXPCount -lt $agentTotal){
     for($adt = $totalXPCount + 1; $adt -le $agentTotal; $adt++){

          Get-Datastore | Select Name, $freespaceCalc, $capacityCalc |Sort-Object Name | ft -Force -AutoSize| Out-Default
          $datastore = Read-Host "What DataStore do you want to use?"

          $hostList = Get-VmHost | select Name | Sort-Object
          $i = 1
          $hostList | % {Write-Host $i ":" $_.Name; $i++}
          $hostIndex = Read-Host "Enter a number (1 -" $hostList.Count ")"
          Write-Host "You selected host" $hostIndex "with hostname" $hostList[$hostIndex - 1].Name

          $machineName = "WinXP-$adt"
          $template = "XP-32 Template"
          $osspec = Get-OSCustomizationSpec XP

          foreach ($vm in $machineName){
               $vm = New-VM -Name $machineName -Template $template -Host $hostList[$hostIndex - 1].Name -Datastore $datastore -OSCustomizationSpec $osspec |start-vm }

Tags: VMware

Similar Questions

  • Create virtual machines from a template


    I'm trying to deploy virtual machines from a template, and then select a specific LUN in my cluster data store. How the code below can be changed to achieve this?

    Thank you

    $VMs = get-Content "VmNames.txt".

    $SourceTemp = "WIn2k08-R2.

    $DsCluster = "DS-Cluster01".

    $Folder = "Win2K08".

    {Foreach ($VM to $VMs)

    New-VM-name $VMs - model $SourceTemp - $DsCluster - $folder location data store


    Do you mean a specific data store of this datastorecluster?

    If so, you can just pass the datastorename on the parameter data store

  • Deploy a new virtual machine from a template

    Is there a workflow avaiable to deploy a new virtual machine from a template?  I tried using the Clone Virtual Machine, no workflow customization but receive an error saying, "task"CloneVM_Task"error: a specified parameter was not correct."
    "Spec.Location.pool (name of the dynamic Script Module: vim3WaitTaskEnd #20).

    When I use a template as a source of VM.

    I can't find any documents pointing to the deployment of the model.


    It's the right workflow, but you must provide a pool of valuable resources for this workflow. If you do not resource pools, it can be shown in a vCO to root in the inventory (vCenter does not work).


  • Error trying to deploy a virtual machine from a template

    Hello everyone,

    We went from ESX 3.5 U4 and VC 2.0 to vSphere 4 and ESX 4.0. In VI 3 deployment of virtual machines in models worked fine without any errors. Now, I have the problem that I can't deploy virtual machines from templates more. I upgraded VMWare tools on each machine for vSphere 4. But whenever I try to deploy a VM from a template I get the following error: "number of virtual devices exceeds the maximum for a given controller. This happens only when I go to the option "Change the virtual hardware (experimental)" and by changing the virtual network to another local VIRTUAL network adapter (in my case from INTERNAL to VLAN21) before you deploy. When I'm not changing the network card virtual, all works well.

    I have been through a few KB and some discussion of the community, but those who are almost for vmware converter.

    Could you please help me?

    Thanks in advance for any answers.


    Convert virtual machine model.

    Modify the virtual machine settings.

    Choose a network suitable for model (it shows probably no name)

    Convert virtual machine model

    Try to deploy new virtual machine model to see if it works again correctly.

    StarWind Software R & D

  • How to create a virtual machine from a template?


    I created a model of virtual machine with a virtual computer on an ESX Server. I want to use this model to create more virtual machines on different ESX servers. How can I do this?

    Thank you


    In your Virtual Center client in the left upper corner click the inventory button and choose 'Models and Virtual Machines' this will display your virtual machines as well as the underneather of models in the window of the host tree.  Right-click on the template and select deploy the virtual machine from this template and it will start the wizard.

  • PowerCLI Script to migrate virtual machines with the same network Source for the new vCenter Server.

    Hi all

    I checked a few posts on the migration to the new vCenter Server virtual machine. But I'm not very good with Powercli and need some help here.

    We have a new vCenter server where we migrate virtual machines from one source vCenter server. Here are two things that I don't know how to connect together to make sure that bwfore we turn on the virtual machine to the virtual machine is connected to its source network.

    Part 1 - this script works well but do not get any network on the vCenter destination.

    cluster = Get-Cluster "clusterA.

    $inventory = get-Cluster $cluster | Get - VM | Add-Member - MemberType ScriptProperty-name "VMXPath" - value {$this.extensiondata.config.files.vmpathname}-Passthru-Force | Select-Object Name ResourcePool, folder, VMXPath |

    $inventory | Export-Csv c:\file.csv

    $inventory = import-csv c:\file.csv

    $cluster = get-group 'b '.

    {foreach ($vm to $inventory)

    $ESXHost = get-Cluster $cluster | Get-VMHost | Select - 1 first

    New-VM - VMFilePath $vm. VMXPath - VMHost $ESXHost - location $vm. Folder - ResourcePool (Get-Cluster $cluster |) Get-ResourcePool $vm. ResourcePool)


    Part 2 - I want to preserve the Portgroup macaddress and the Ip of the virtual computers and connect them to the new vCenter. How can I do this with the script below. Or how can I combine them together?

    $vm_list = get-Cluster-name "clustera | Get - VM test * | Name sort

    $information = {foreach ($vm to $vm_list)

    $network_adapter_information = get-NetworkAdapter - VM $vm

    $vm | Select Name,PowerState,ResourcePool,@{N="Path; E=       {$_. ExtensionData.Summary.Config.VmPathName}},@{N="NetworkAdapter '; E = {[string]: join (":", ($network_adapter_information |))} %{$_. {{(Nom + "," + _.NetworkName $}))}}, @{N = "MacAddress"; E = {[string]: join (",", ($network_adapter_information | % {$_.}))} MacAddress}))}}


    Step 2

    # Unsubscribe virtual machines

    Remove-VM - VM $vm_list-RunAsync Verbose - confirm: $false

    Step 3

    $cluster = get-group 'b '.

    {foreach ($vm to $inventory)

    $ESXHost = get-Cluster $cluster | Get-VMHost | Select - 1 first

    New-VM - VMFilePath $vm. VMXPath - VMHost $ESXHost - location $vm. Folder - ResourcePool (Get-Cluster $cluster |) Get-ResourcePool $vm. ResourcePool)


    # Save virtual machines

    foreach ($info in $information)


    $vmxpath = $info. Path

    $resource_pool = $info. ResourcePool.Name

    New-VM - ResourcePool (ResourcePool-Get-name $resource_pool) VMFilePath - $vmxpath - RunAsync-Verbose | Out-Null


    Step 4

    # Put the network cards

    foreach ($info in $inventory)


    foreach ($network_information in ($info.NetworkAdapter - split ":"))))


    $virtual_machine = $info | %{$_. Name}

    $network_adapter = ($network_information-split ",") [0]

    $network_vlan = ($network_information-split ",") [1]

    Get-VM-name $virtual_machine | Get-NetworkAdapter-name $network_adapter | Together-NetworkAdapter - NetworkName $network_vlan-Verbose-RunAsync-confirm: $false | Out-Null



    # Start the virtual machines

    foreach ($vm to $vm_list)


    Start-VM - VM $vm. Name-RunAsync Verbose - confirm: $false | Out-Null}


    Thanks in advance for any help.

    Hi all

    I made some changes to the script and it worked for me. We have added a host on the destination with all exchanges and storage mapped vCenter.

    $vm_list = get-Cluster-name 'Site B - b | Get - VM | Name sort

    $information = {foreach ($vm to $vm_list)

    $network_adapter_information = get-NetworkAdapter - VM $vm

    $vm | Select Name,PowerState,ResourcePool,@{N="Path; E=       {$_. ExtensionData.Summary.Config.VmPathName}},@{N="NetworkAdapter '; E = {[string]: join (":", ($network_adapter_information |))} %{$_. {{(Nom + "," + _.NetworkName $}))}}, @{N = "MacAddress"; E = {[string]: join (",", ($network_adapter_information | % {$_.}))} MacAddress}))}}


    # Unsubscribe virtual machines

    Remove-VM - VM $vm_list-Verbose - confirm: $false

    $cluster = get-Cluster "Site A - clustera.

    {foreach ($vm to $information)

    $ESXHost = get-Cluster $cluster | Get-VMHost | Select - 1 first

    New-VM - VMFilePath $vm.path - VMHost $ESXHost - location $vm. Folder - ResourcePool (Get-Cluster $cluster |) Get-ResourcePool $vm. ResourcePool)


    # Save virtual machines

    foreach ($info in $information)


    $vmxpath = $info. Path

    $resource_pool = $info. ResourcePool

    New-VM - ResourcePool (ResourcePool-Get-name "test1") VMFilePath - $vmxpath - RunAsync-Verbose | Out-Null


    # Put the network cards


    foreach ($info in $information)


    foreach ($network_information in ($info.NetworkAdapter - split ":"))))


    $virtual_machine = $info | %{$_. Name}

    $network_adapter = ($network_information-split ",") [0]

    $network_vlan = ($network_information-split ",") [1]

    Get-VM-name $virtual_machine | Get-NetworkAdapter-name $network_adapter | Together-NetworkAdapter - NetworkName $network_vlan-Verbose - confirm: $false | Out-Null



    # Start the virtual machines

    foreach ($vm to $vm_list)


    Start-VM - VM $vm. Name-RunAsync Verbose - confirm: $false | Out-Null


    Hope this will help many of you.

  • PowerCLI script to migrate virtual machines to the new vCenter environment

    Hello world

    in the next few weeks, we need about migrate 700 VMs (Windows/Linux) server to a new environment (new vCenter, new hosts, but VMs will remain on same data warehouses). VMs will be migrated not in one batch, but rather from time to time (which, however, can understand the need to migrate some 10s of virtual machines at the same time).

    Cluster names and folder structure will be identical to the old and the new vCenters. As host in the new vCenter names differ from those in the old vCenter, thought to provide a table of 'translation', which tells the script to save a given VM on host B (new vCenter) when having been registered in order to host a (old vCenter) and so on.

    I thought writing a PowerCLI script which, broadly speaking, can accomplish the following steps:

    • Connect to vCenterOld and vCenterNew
    • Correspondence table of host to read from a file (see step "register VM vCenterNew below)

    Then, in a loop:

    • Ask VM name
    • Get the settings of the virtual machine: Datastore/path, network/Portgroup, HA restart priority, host, folder, note
    • View the parameters and the VM name and request confirmation migrate the virtual machine
    • Turn off VM (closed OS gracefully, but force power off if power status is still not equal 'off' after a time given)
    • To unsubscribe from VM vCenterOld (removal of inventory)
    • Registry of the VM to the data store vCenterNew (assumptions: same folder as vCenterOld, host names are different, but for each source in vCenterOld host a host of destination in vCenterNew for you join the virtual machine to can be provided, that is, CSV)
    • The value of VM portgroup (same name as in vCenterOld)
    • Set VM HA restart priority
    • PowerON VM
    • Wait / check the VM (for example, ping) connectivity / continue immediately if the virtual machine is accessible or after a certain time if the virtual machine is still inaccessible
    • Confirm that the virtual machine has been migrated successfully, OR ask the user to check the connectivity of the virtual machine

    Now to my questions:

    • Is anyone know about an existing script that does something similar or contains parts that can be useful to compile a script like this?
    • How can vCenter two connections at the same time been managed within a PowerCLI script - or doesn't work at all and a connect/disconnect to/from the two vCenters must be completed for each virtual computer?

    Many thanks for any help.

    The following seems to work for me

    $vm = Get-VM -Name MyVM$nic = Get-NetworkAdapter -VM $vm $vdPG = $nic | select -ExpandProperty NetworkName
    Get-VDPortgroup -Name $vdPG
  • Script to import virtual machines from data warehouses

    Is there a quick way to import all of the virtual machines on a data store in a vCenter?  A kind of browse VMX files and import?

    More to the point.  I have the only store of data that has been moved to a new location with approximately 100 VM on it.  I want to import all the virtual machines on this unique to a single host data store to the new location.

    Thank you

    Boston Tech Guy

    You can specify the name of this 1 data store in the variable $Datastores .

    And if you change this line

    $ESXHost= Get-Cluster$Cluster| Get-VMHost|select-First1


    $ESXHost=Get-VMHost -Name MyEsx

    It should do the trick.

  • How to change the size of virtual machine from a template installation disc.

    Dear team

    How to change the size of the disk when installing VM from a template? I'm on ESX 4.0 Vcenter 4.0



    I don't know if this is possible, you may need to deploy the machine first, then use the VMware Converter or vDisk settings in the properties of the virtual machine.

    The converter is in charge on the partition, as well as (for Windows clients).


    VCP / VMware vEXPERT 2009

  • 120 available virtual machines from a template - newbie question

    Hi all -

    I am currently working on a project where I need to create a model W2K8R2 120 VMs.

    Is it possible to automate this process quick and easy 5.1 using Orchestrator?

    I only need automates the process of VM - name. After I created manually assign VLAN and IP on each virtual machine.

    Any suggestions?

    Type r


    Start here: How to deploy VMS of form in Orchestrator template - and follow the link to the additional item at the end of this one. This covers the technical closure to clone several virtual machines to have fun!

  • Deploy multiple virtual machines from one model to the help of customization of the OS and a text file for hostname and IP

    Hi all

    Not sure if this has already been answered, I did a search on the forums, but couldn't find an answer to what I'm looking for.

    I want to fully automate the deployment of multiple VMs of a model using a customization file, but also to retrieve the host names and IP addresses from a text file / spreadsheet.

    for example:

    I need to deploy a test environment of 30 virtual machines using the template file and customizing TestVM. I have a spreadsheet with the VM host names and IP addresses. The process now is to manually enter the host name and IP when the customization file invites to do while deploying them in each virtual machine. The customization file takes care of the rest (license key, admin password, add to the domain etc.) can I automate entry of the name of host and IP addresses in reading from a text file?

    Thanks in advance!

    Take a look on the deployment, customization and modification of virtual machines to a csv file

    There are many more examples of this community on the same subject,

  • How to add a script to a virtual machine using vmwrae api?


    I am trying to add a new disk to an existing VM (Linux).

    To do this.

    Now to use the new drive on a run a series of commands, like:

    -format the drive

    -mount the drive

    -put this information to disk in the file/etc/fstab

    My question is how we can automate this task without actually going inside the virtual machine?

    This is precisely what my advice is intended to accomplish. There are many examples available online to use Invoke-VMScript to automate actions in a Linux VM.


    This was done from my Windows using PowerCLI workstation to a virtual machine running SLES:

    PowerCLI C:\> Invoke-VMScript -vm $vm -HostCredential $hc -GuestCredential $gc -ScriptText "uptime"
     08:32:52 up 88 days,  7:27,  0 users,  load average: 0.00, 0.00, 0.00
  • How to move a virtual machine from one virtual switch to the other since the SDK / command line?

    How to move a virtual machine from one virtual switch to the other since the SDK / command line?

    Joshua Smith

    Are you referring to the evolution of the virtual machines network portgroup of one to the other and in your case potentially issue on to another vSwitch? If so, take a look at this script: updateVMPortgroup.pl everything you do really makes a call to ReconfigVM_Task() and passing in a specification change and put to day the network in which you want to change


    William Lam

    VMware vExpert 2009

    Scripts for VMware ESX/ESXi and resources at: http://engineering.ucsb.edu/~duonglt/vmware/

    repository scripts vGhetto

    VMware Code Central - Scripts/code samples for developers and administrators

    150 VMware developer

    Introduction to the vMA (tips/tricks)

    Getting started with vSphere SDK for Perl

    Twitter: @lamw

    If you find this information useful, please give points to "correct" or "useful".

  • Creating new virtual machine from an another vm

    I installed VMware ESXi 3.5 and that you have created a virtual machine to Windows Server 2003. I would like to use this virtual machine as a model to create other virtual machines. How to create a new virtual machine from a virtual machine existing? What is the best practice? Thank you.

    stop the virtual machine in question, and "the clone to a template.  You can then deploy oversized.  Make sure you have all of the sysprep files that are located in the right place.

    You wan to watch this .pdf, even if a small wedge, it might be useful


    Keep in mind that in order to use the functionality of model or a clone, you must have a vCenter Server

    If you believe this or any othe response was useful please consider marking as "useful" or "correct".

  • Virtual machine from Windows XP to Windows 7 Professional

    Two years ago the virtual machine from Windows XP was installed on my laptop which has Windows 7 Professional.  I use version XP for playing games is not compatible on Windows 7.  Everything worked well until a few weeks ago.  I was unable to open the games.  An error message window is displayed when I click to start the game.  "C:\WINDOWS\SYSTEM32\CONFIG. NT.  The file system not suitable for running MS-DOS and Microsoft Windows applications.  Choose 'Close' to terminate the application. "The top border of the window says error message"16 bit Windows subsystem.   Any ideas.

    After temporarily off any protection, I tried restore and recovery without success.  Error message says that both failed.

    Please keep in mind that I'm completely computer illiterate.  Thank you.

    Hello Willias,

    Thanks for posting your question on the Microsoft community.

    The question will be better suited to the audience of professionals on the TechNet forums.

    I would recommend posting your query in the TechNet forums.

    TechNet Forum

    Thank you

Maybe you are looking for