VMware Cloud Community
TdisalvoOrinoco
Enthusiast
Enthusiast
Jump to solution

Create VM using PowerCLI and csv file

I know that there is a lot of info out there about this and I think I have a prety good working script.  A lot was borrowed from this thread, http://communities.vmware.com/thread/315193 many thanks to LucD.

My code is as follows:

## Deploy VMs from CSV File
## Much borrowed from http://communities.vmware.com/thread/315193?start=15&tstart=0


## Imports CSV file
Import-Csv "C:\guests.csv" -UseCulture | %{
## Gets Customization info to set NIC to Static and assign static IP address
    Get-OSCustomizationSpec $_.Customization | Get-OSCustomizationNicMapping | `
    Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $_."IP Address" `
        -SubnetMask $_.Subnet -DefaultGateway $_.Gateway -Dns $_.DNS
    $vm=New-VM -Name $_."Server Name" -Template $_.Template -Host $_."Esx Host" `
        -Datastore $_.Datastore -OSCustomizationSpec $_.Customization `
        -Confirm:$false -RunAsync
## .......
}

It will go through and clone the VM, and then it looks like it is running the OSCustomizaitonSpec and then it bombs.  It does clean up the bad VM.  The Recent Tasks shows "clone virtual machine fails a specified parapeter was not correct. hostname"

The issue is that I am not trying to set a hostname with the OSCustomixationSpec, I am only trying to put in IP address info.

I am runningn PowerCLI 5.0.1 build 581491

My ESXi host is 5.0.0 build 914586

My vCenter Server is Ver 5.0.0 build 623373

I opened a new thread, becuase the age of the old thread.  Not sure if that is the best way to do things.     

0 Kudos
48 Replies
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

Hi Lucd,

I have Posted the Screenshot with Status and Name of the task looping in script and it is working fine, and I will check with this Task ID created by New-vm and get back to you. Thanks a lot.

0 Kudos
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

Hi Lucd,

Script is working fine !!! is it possible to display like VM1 is deployed, VM2 is deploying. Please suggest.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sure, try this

$MaxClones = 3

$CSV = "C:\users\$env:username\Linux.csv"

$nvm = Import-CSV $CSV -Useculture

$idTab = @()

foreach($row in $nvm){

    $idTab += New-VM -Name $row.Name`

        -vm $(Get-Cluster $row.cluster | Get-ResourcePool $row.SVMLocation | get-vm $row.SourceVM)`

        -VMHost $(Get-Cluster $row.cluster | Get-VMHost -state "connected" | Get-Random)`

        -ResourcePool $(Get-Cluster $row.cluster | Get-ResourcePool $row.rp)`

        -datastore $(Get-Cluster $row.cluster | Get-DataStore $row.ds)`

        -Notes $row.Notes -RunAsync | Select -ExpandProperty Id

    $CurrentClones = Get-Task -Status Running |

        where {$idTab -contains $_.Id} |

        Measure-Object | Select -ExpandProperty Count

    Write-Output "VM $($row.Name) is being deployed"

    while($CurrentClones -ge $MaxClones)

    {

        sleep 30

        $CurrentClones = Get-Task -Status Running |

            where {$idTab -contains $_.Id} |

            Measure-Object | Select -ExpandProperty Count

    }

}

   


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

Hi Lucd,

Thanks for your support and below is the final script which I am going to use for multiple deployments.

Is there any way that due to some problem if the command doesn't executed then It will generate a log in some path like C:\error.log

Ex: VM1 is not cloned, VM2 CPU is not configured etc.

Please suggest.

$MaxClones = 3

$CSV = "C:\users\$env:username\Linux.csv"

$nvm = Import-CSV $CSV -Useculture

$idTab = @()

$nvm |

%{

$idTab += New-VM -Name $_.Name`

        -vm $(Get-Cluster $_.cluster | Get-ResourcePool $_.SVMLocation | get-vm $_.SourceVM)`

        -VMHost $(Get-Cluster $_.cluster | Get-VMHost -state "connected" | Get-Random)`

        -ResourcePool $(Get-Cluster $_.cluster | Get-ResourcePool $_.rp)`

        -datastore $(Get-Cluster $_.cluster | Get-DataStore $_.ds)`

        -Notes $_.Notes -RunAsync | Select -ExpandProperty Id

  $CurrentClones = Get-Task -Status Running |

        where {$idTab -contains $_.Id} |

        Measure-Object | Select -ExpandProperty Count

  Write-Output "$($_.Name) is deploying"

  Write-Output "In loop: Current: $($CurrentClones)   Max: $($MaxClones)"

  while($CurrentClones -ge $MaxClones)

  {

        sleep 30

 

  Write-Output "$($_.Name) is deploying"       

  Write-Output "In loop: Current: $($CurrentClones)   Max: $($MaxClones)"

  if($currentclones -eq $maxclones){

        $CurrentClones = Get-Task -Status Running |

        where {$idTab -contains $_.Id} |

        Measure-Object | Select -ExpandProperty Count

  }

}  

#Configuring CPU If values exists in CSV

$nvm | %{ if($_.CPU -ne [string]$null) {Set-VM -VM $_.Name -NumCpu $_.Cpu -Confirm:$false  }}

#Configuring Memory If values exists in CSV

$nvm | %{ if($_.MGB -ne [string]$null) {Set-VM -VM $_.Name -MemoryGB $_.MGB -Confirm:$false }}

#Adding HardDisk If values exists in CSV

$nvm | %{ if($_.HDD -ne [string]$null) {New-HardDisk -VM $_.Name -CapacityGB $_.HDD -Persistence persistent -Confirm:$false }}

#Configuring VLAN If values exists in CSV

$nvm | %{ if($_.VLAN -ne [string]$null) {Get-VM $_.Name | Set-NetworkAdapter  -NetworkName "$_.VLAN" -Confirm:$false}}

0 Kudos
esxi1979
Expert
Expert
Jump to solution

Hi

Set-OSCustomizationNicMapping : 12/12/2015 9:54:21 PM    Set-OSCustomizationNicMapping        You must not provide

Wins or Dns when you create NIC mapping for a customization spec with type 'linux'.

At line:3 char:5

+     Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $_."IP Address" ...

+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [Set-OSCustomizationNicMapping], ViError

    + FullyQualifiedErrorId : Core_NicMappingCmdletBase_ValidateParameters_LinuxAndWins,VMware.VimAutomation.ViCore.Cm

   dlets.Commands.SetOSCustomizationNicMapping

Set-OSCustomizationNicMapping : 12/12/2015 9:54:21 PM    Set-OSCustomizationNicMapping        When the IpMode

parameter is set to UseStaticIp, you must specify the IPAddress, Subnetmask, Dns(on Windows specs only), and

DefaultGateway parameters.

At line:3 char:5

+     Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $_."IP Address" ...

+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [Set-OSCustomizationNicMapping], ViError

    + FullyQualifiedErrorId : Core_NicMappingCmdletBase_ValidateParameters_RequiredParametersSaticIp,VMware.VimAutomat

   ion.ViCore.Cmdlets.Commands.SetOSCustomizationNicMapping

Here is my xls

Server NameESX HostTemplateDatastoreCustomizationNumCpuIP AddressMemoryGBSubnetMaskDefaultGateway

dns

pls suggest

0 Kudos
esxi1979
Expert
Expert
Jump to solution

Never mind, Thanks

I saw your note

For a Linux OS you can not specify the DNS parameter on the Set-OSCustomization cmdlet.

First try leaving out the this DNS parameter

0 Kudos
mike_irvin
Contributor
Contributor
Jump to solution

I don't understand why there are two customization specs used: one from the .csv file ($_.Customization) and one defined as "$cust".  When i run this script I get an error  "could not find customization specification with name 'Test'.  When I change the script to use $_.Customization as the name in both places, the script runs. 

What is the purpose of having the 2 specs?

0 Kudos
philk33
Enthusiast
Enthusiast
Jump to solution

I agree, it doesn't make sense.

Also, did you get this error in the output?

pastedImage_0.png

oscustomizationspec doesn't seem to itself be depreciated.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is no error, it's a warning, telling you that this parameter might disappear in a future PowerCLI release.

Start using the suggested parameter.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos