Automation

 View Only
  • 1.  Script to assist with VM provisioning

    Posted Sep 05, 2012 11:07 PM

    I need help trying to figure out why the new-vm cmdlet is reporting an error.  Any assistance would be appreciated.

    code snip (excuse the crappy line wraps)

    $VIServer ="vCenterServer" Connect-VIServer $VIServer
    $VMCSV_Builds = Import-Csv -Path .\VM_Config.csv
    $i = 0
    $VMCSV_Builds | % {
    # map out my variables
         $Clust = Get-Datacenter | get-Cluster ($_.Cluster)
         $templatevm = $_.templatevm
         $datastore = $_.datastore
         $vmhost = $Clust | get-VMhost | get-random
         $custspec = $_.custspec
         $vmname = $_.vmname
         $NUMCPU = $_.NUMCPU
         $MemoryMB = $_.MemoryMB
         $ipaddr = $_.ipaddress
         $subnet = $_.subnet
         $gateway = $_.gateway
         $pdns = $_.pdns
         $sdns = $_.sdns
         #$vlan = $_.vlan
         $description = $_.notes

    #debugging

    Write-Host $Clust
    Write-Host $vmhost.Name
    $Step = "1"

    if ($Step -eq "1"){
    #Step 1
    # A - Connect VM
         if (!(Get-VM $vmname -ErrorAction 0))
         {
    # B - Setup the IP address
         Get-OSCustomizationSpec
              $custspec | Get-OSCustomizationNicMapping |
         Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress $ipaddr -SubnetMask $subnet -DefaultGateway $gateway -Dns $pdns,$sdns
    # C - Copy the Template and apply customizations
         $item = New-VM -VMhost $vmhost -Name $vmname -Template $templatevm -NumCpu $NUMCPU -MemoryMB $MemoryMB -Datastore $datastore'           -DiskStorageFormat Thin -Description $description -OSCustomizationSpec $custspec -RunAsync "`n Step 1 is now being run.`n"
         Get-Task | ? {
              $_.id -eq $Item.id}
              Start-Sleep -Seconds 30
              }
         else
              {
              "`nVM aleady exists, checking for running tasks . .`n"
              $Task = @(Get-Task | ? {$_.Description -eq "Clone virtual machine"})

              if ($Task)
                   {
                   #$Task
                   Write-Host "Task is $($Task[$i].PercentComplete) % complete."
                   $i++
                   }
              else
                   {
                   "Task is complete, go onto the next step (2).`n"
                   }
              }
         }
    }

    end snip

    this is the error that I get:

    New-VM : Parameter set cannot be resolved using the specified named parameters.
    At D:\Users\hal0210\desktop\Scripts\CreateVM.ps1:45 char:27
    +              $item = New-VM <<<<  -VMhost $vmhost -Name  $vmname -Template $templatevm -NumCpu $NUMCPU -MemoryMB $MemoryMB  -Datastore $datastore -Dis
    kStorageFormat Thin -Description $description -OSCustomizationSpec $custspec -RunAsync "`n Step 1 is now being run.`n"
        + CategoryInfo          : InvalidArgument: (:) [New-VM], ParameterBindingException
        + FullyQualifiedErrorId : AmbiguousParameterSet,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewVM



  • 2.  RE: Script to assist with VM provisioning

    Posted Sep 06, 2012 02:53 AM

    Hello, halibut-

    It looks like your code is missing a line break in the New-VM line.  The part that echos out that step 1 is now being run needs to be on a new line.  So, the following line:

    $item = New-VM -VMhost $vmhost -Name $vmname -Template $templatevm -NumCpu $NUMCPU -MemoryMB $MemoryMB -Datastore $datastore'           -DiskStorageFormat Thin -Description $description -OSCustomizationSpec $custspec -RunAsync "`n Step 1 is now being run.`n"

    should be like:

    $item = New-VM -VMhost $vmhost -Name $vmname -Template $templatevm -NumCpu $NUMCPU -MemoryMB $MemoryMB -Datastore $datastore -DiskStorageFormat Thin -Description $description -OSCustomizationSpec $custspec -RunAsync
    "`n Step 1 is now being run.`n"

    (There was an additional single quote after the $datastore variable, too -- I removed it).  Does that do better for you?



  • 3.  RE: Script to assist with VM provisioning

    Posted Sep 06, 2012 01:43 PM

    No, it didn't change.  Still the same error code.



  • 4.  RE: Script to assist with VM provisioning
    Best Answer

    Posted Sep 06, 2012 03:19 PM

    Hello, halibut-

    Ah, yes, there is another problem:  the parameters that you are using for New-VM.  Since you are using the -Template parameter, that excludes using some of the other New-VM paramters, due to the Template parameter set.

    The valid parameters for use with New-VM when deploying from template are:

    *AdvancedOption
    Confirm
    *Datastore
    Description
    *DiskStorageFormat
    DrsAutomationLevel
    HAIsolationResponse
    HARestartPriority
    Location
    *Name
    *OSCustomizationSpec
    ResourcePool
    RunAsync
    Server
    *Template
    VApp
    VMHost
    WhatIf

    So, you need to remove the -NumCpu and -MemoryMB parameters (and values) from the New-VM call.  Then, after the New-VM call completes, you can use the Set-VM cmdlet to set NumCpu and MemoryMB for the newly created VM.

    Give that a shot.  And, you can get more info about ParameterSets by running:  Get-Help -full about_Functions_Advanced_Parameters



  • 5.  RE: Script to assist with VM provisioning

    Posted Sep 06, 2012 04:06 PM

    That did it.  Thanks for the fix!



  • 6.  RE: Script to assist with VM provisioning

    Posted Sep 06, 2012 04:21 PM

    D'oh and now it produces a different error about the $clust variable being empty.  The line $clust = get-datacenter | get-cluster ($_.Cluster) isn't pulling any information.

    Chris



  • 7.  RE: Script to assist with VM provisioning

    Posted Sep 06, 2012 05:01 PM

    Figured out the loop isn't finishing and I need to change the error checking to exit the loop before it attempts to run the next line that is blank.  Thanks for the help.

    Chris