Automation

 View Only
  • 1.  PowerCLI to deploy VMs at once

    Posted Mar 09, 2011 02:22 PM

    I can deploy multiple VMs usng PowerCLI but they deploy sequentially and with an error.  It seems almost easier to deploy them manually so that they are created at the same time, and not one after the other.  I also want static IPs but not sure how to achieve it.

    Right now I am using a customization specs file which specifies static IP  and  prompt for it.  But it never does, it deploys the vms then throws  an  error in VC stating 'a specified paramater was not correct.  nicsettings  :adatpter:ip."  I want incrementing IPs on the VMS I  deploy.

    This is the contents of my basic .ps1 file

    New-vm   -vmhost vm04a.domain.com -Name w2k8sp2s64qa14-vm -Template   Template_w2k8sp2s64qa -Datastore vm04a_storage -OSCustomizationspec   w2k8sp2s64

    Can someone help me out?

    1. Assign static IPs

    2. Deploy all VMs at once

    ...



  • 2.  RE: PowerCLI to deploy VMs at once

    Posted Mar 09, 2011 02:49 PM

    To deploy all the VMs (nearly) at once you could use the -RynAsync parameter.



  • 3.  RE: PowerCLI to deploy VMs at once

    Posted Mar 09, 2011 02:51 PM

    Thanks.  Basically it would be....> ./<whatever>.ps1 -RynAsync

    ?

    Also can I not prompt for IP when doing this for a win2008 box?  It won't create the VM when set to prompt for IP.



  • 4.  RE: PowerCLI to deploy VMs at once
    Best Answer

    Posted Mar 09, 2011 02:57 PM

    No, the -RunAsync parameter goes on the New-VM cmdlet.

    It will continue with the line after the New-VM without waiting for the New-VM cmdlet to finish.



  • 5.  RE: PowerCLI to deploy VMs at once

    Posted Mar 09, 2011 03:17 PM

    Ok that worked, thanks man.

    Any ideas on the being able to set static IPs?



  • 6.  RE: PowerCLI to deploy VMs at once

    Posted Mar 10, 2011 07:21 AM

    In my experience the "prompt user" option only works when you deploy from within the vSphere client, not when you deploy with a PowerCLI script.

    Which makes sense in a way, since it would be illogical to use an interactive feature in an automation script :-)

    What you could do is use the Set-OSCustomizationNicMapping cmdlet in your script to modify the static IP address in the Customisation specification before each call to the New-VM cmdlet.



  • 7.  RE: PowerCLI to deploy VMs at once

    Posted Mar 10, 2011 02:33 PM

    Thanks, I agree on the prompt.  But I am new to deploying VMs in this  manner so I wasn't sure how it was supposed to work.  However, this is  the script I am using:

    Connect-VIServer vcenter.domain.com
     
    $vmlist = Import-CSV C:\vms.csv
     
    foreach ($item in $vmlist) {
       # I like to map out my variables
         $Template = $item.template
         $datastore = $item.datastore
         $vmhost = $item.vmhost
         $custspec = $item.custspec
         $vmname = $item.vmname
         $ipaddr = $item.ipaddress
         $subnet = $item.subnet
         $gateway = $item.gateway
         $pdns = $item.pdnswins
         $sdns = $item.sdnswins


         #Get the Specification and set the Nic Mapping (Apply 2 DNS/WINS if 2 are present)
         If ($Varable) {
             Get-OSCustomizationSpec $custspec |  Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode  UseStaticIp -IpAddress $ipaddr -SubnetMask $subnet -DefaultGateway  $gateway -Dns $pdns,$sdns
         } else {
             Get-OSCustomizationSpec $custspec |  Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode  UseStaticIp -IpAddress $ipaddr -SubnetMask $subnet -DefaultGateway  $gateway -Dns $pdns
         }
     
         #Clone the BaseVM with the adjusted Customization Specification
         New-VM -Name $vmname -Template $Template -Datastore $datastore  -VMHost $vmhost | Set-VM -OSCustomizationSpec $custspec -RunAsync  -Confirm:$false
     
        #Remove the NicMapping (Don't like to leave things unkept)
         Get-OSCustomizationSpec $custspec | Get-OSCustomizationNicMapping | Remove-OSCustomizationNicMapping -Confirm:$false


    and the .cvs looks like this:

    Template,datastore,vmhost,custspec,vmname,ipaddress,subnet,gateway,pdnswins,sdnswins
    Template_w2k3r2e64,vm02a_storage,vm02a.domain.com,w2k3r2e64qa,w2k3r2e64-vm,10.102.30.128,255.255.255.0,10.102.30.1,10.0.2.35,10.0.2.60

    Which isn't working, if I comment out the last line it will work.  However if  I don't it will create the VM but it will hang with the message below:

    cmdlet Remove-OSCustomizationNicMapping at command pipeline position 3
    Supply values for the following parameters:
    OSCustomizationNicMapping[0]:

    If  I comment out that line it creates the VMS without fail but obvously never sets the static IP.   Any ideas?



  • 8.  RE: PowerCLI to deploy VMs at once

    Posted Apr 25, 2013 06:58 PM

    Has there been an update to this?  Did you get your solution to deploy with Static IPs?

    I have the same issue, I need to deploy several hundred VMs with Static IPs.

    Thanks,

    Boston Tech Guy



  • 9.  RE: PowerCLI to deploy VMs at once

    Posted Apr 25, 2013 07:18 PM

    Yeah I got it working but with only windows VMs.  I use a .csv file and the script below, be sure to update the default gateway and DNS IP addresses within the script to match whatever network you are deploying VMs on. 

    Import-Csv deploy.csv -UseCulture | %{
        Get-OSCustomizationSpec $_.Customization | Get-OSCustomizationNicMapping | `
        Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $_."IP Address" `
            -SubnetMask 255.255.254.0 -DefaultGateway 1.1.1.1 -Dns 2.2.2.2,3.3.3.3
        $vm=New-VM -Name $_."Server Name" -Template $_.Template -Host $_."Esx Host" `
            -Datastore $_.Datastore -OSCustomizationSpec $_.Customization `
            -Confirm:$false -RunAsync
    }

    The .csv file is simple and looks like this:

    I  fill in the information for the x number of vms I want created in the .csv file then run the ps script (make sure .csv file is in the same dir as the script).  The customization files that I created in vcenter (Management > Customization Specifications Manager) have STATIC IP's assinged to them already, but the static IP gest overwritten with the script and .csv file above.



  • 10.  RE: PowerCLI to deploy VMs at once

    Posted Apr 25, 2013 07:37 PM

    This is excellent!  You were also able to make the Cust Spec a variable in the script.  That rocks! We can use that too.

    Going to intergrate this into our deploy scripts and start testing.

    Thanks

    Boston Tech Guy