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
LucD
Leadership
Leadership
Jump to solution

What happens, do all clone jobs get started ?

Did you check the value you have in $CurrentClones ?

You could display the value of this variable on screen from within the While-loop, that way you should be able to follow what is going on.


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

0 Kudos
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

Hi Lucd,

I have passed 5 VM's in CSV sheet and after execution of this script 5 VM's started to clone at a time.

I have cross checked and $CurrentClones is having 0 value.

I will try to pass $currentclones in while loop and get back to you.

Thanks Lucd.

0 Kudos
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

Hi Lucd,

I have tried like below and all VM's started cloning from CSV and more over it is not accepting the Varaiables from CSV File and getting error unable to get Name "$_.cluster". Please suggest.

$MaxClones = 3

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

$nvm = Import-CSV $CSV -Useculture

$nvm | %{

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

$CurrentClones = Get-Job -State Running | Measure-Object | Select -ExpandProperty Count

  while($CurrentClones -ge $MaxClones)

  {

  sleep 30

  $CurrentClones = 0

  }

}

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Do you have a column named Cluster in the CSV file ?


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

0 Kudos
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

Yes Lucd, It's column name please find the CSV in attachment.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Does the following display the properties correctly ?

Import-CSV $CSV -Useculture | Select Name,Cluster

Could you perhaps also include a screenshot of the error message you are getting ?


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

0 Kudos
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

Hi Lucd,

Please see the attachments and suggest.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I overlooked it as well, you were using the pipeline variable $_, but that was not the row from the CSV file anymore.

Try like this

$MaxClones = 3

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

$nvm = Import-CSV $CSV -Useculture

foreach($row in $nvm){

    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

    $CurrentClones = Get-Job -State Running | Measure-Object | Select -ExpandProperty Count

   

    while($CurrentClones -ge $MaxClones)

    {

        sleep 30

        $CurrentClones = Get-Job -State Running | Measure-Object | Select -ExpandProperty Count

    }

}


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

0 Kudos
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

Hi Lucd,

I have kept 3 VM's in CSV and executed below script and still 3 vm's started cloning at a time and script execution got completed once the final VM clone has started. Please find the attachment.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try displaying the $currentClones value inside the While loop.


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

0 Kudos
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

Hi Lucd,

I have tried passing $currentClones value in while loop and still 3 VM's are cloning at a time as I have limited to 2 in CSV. Please find attachment.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Why are you setting the value of $CurrentClones to 0 inside the loop ?

That way the loop will never wait when more than 2 clones are running


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

0 Kudos
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

I though it displaying $currentclones in while loop is passing $currentclones =0 , If I have applied wrongly, Please suggest.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try the following, it will display the value of $CurrentClones

$MaxClones = 3

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

$nvm = Import-CSV $CSV -Useculture

foreach($row in $nvm){

    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

    $CurrentClones = Get-Job -State Running | Measure-Object | Select -ExpandProperty Count

  

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

    while($CurrentClones -ge $MaxClones)

    {

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

        sleep 30

        $CurrentClones = Get-Job -State Running | Measure-Object | Select -ExpandProperty Count

    }


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

0 Kudos
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

It is showing $currentClones as 0, Please find attachment.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I overlooked that, it should be Get-Task instead of Get-Job.

$MaxClones = 3

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

$nvm = Import-CSV $CSV -Useculture

foreach($row in $nvm){

    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

    $CurrentClones = Get-Task -Status Running | Measure-Object | Select -ExpandProperty Count

  

    while($CurrentClones -ge $MaxClones)

    {

        sleep 30

        $CurrentClones = Get-Task -Status Running | Measure-Object | Select -ExpandProperty Count

    }


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

0 Kudos
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

Hi Lucd,

I have passed 3 VM's for cloning and limited to 2.

Script is limiting the Clone based on total number of Tasks happening on VI, I have observed while vMotion is happening at that time also script is going to sleep mode until unless that migration is completed. Please suggest.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is because the script is only looking for active tasks that have the status "running".

The test should be made more specific, you should test besides the status also on the name of the task.

Can you do a Get-Task while these clones are running ?

That should allow us to make the test more specific.


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

0 Kudos
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

Hi Lucd,

After adding taskname in script it is working fine, but I hope if another Engineer has opened another PowerCLI console and started VM Cloning using same script at that time get-task will be returning total tasks executing in vSphere environment instead of executing in loop, is there any way to overcome this issue.

Get-Task is returning all success, Error, Running in vSphere Client Recent Taks. Please suggest.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can test on the Name and the status of the task

$CurrentClones = Get-Task -Status Running | where {$_.Name -match 'CloneVM_Task'} |

    Measure-Object | Select -ExpandProperty Count

Another solution is to keep track of the Task IDs and only check the status of those you submitted.

Something like 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

 

    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