VMware Cloud Community
jvm2016
Hot Shot
Hot Shot

powercli script to create vm from template

Hello LucD,

could you post the script to create vm from template.

0 Kudos
21 Replies
LucD
Leadership
Leadership

Have a look at Example 10 of the New-VM cmdlet online help page.

Or do you have any specific requirements?


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

0 Kudos
jvm2016
Hot Shot
Hot Shot

well  there is specific requirement .

if you can check following code   .I have just encountered one hude script to deply vms from .csv file  using template (which has two network adapters)

there is issue while  configuring network .  following is the code for configuring network adapter .

pastedImage_0.png

pastedImage_1.png

and below is the error.

pastedImage_2.png

  to solve this currently i am making network1 and network2 as mandatory parameter  like this   but  it is something extra iam doing as it also  reads the .csv file .

pastedImage_3.png

Hope iam making sense here .

0 Kudos
LucD
Leadership
Leadership

If I understand it correctly, you are trying to use splatting for the Set-NetworkAdapter.

And the hash table is build in the 1st piece of code.

Try like this (note the @ character when using splatting)

$VM | Get-NetworkAdapter | where{$_.Name -eq 'NetworkAdapter 2'} |

Set-NetworkAdapter @network


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

0 Kudos
jvm2016
Hot Shot
Hot Shot

not sure of  splatting .  but checking .also there are two network adapters to be configured .

0 Kudos
LucD
Leadership
Leadership

Since you are storing the parameters and their values in a hash table, I assumed you trying to use splatting.

The error actually says that it doesn't know how to convert from the hash table.

But try my suggestion.

In this specific error you are only configuring 1 vNIC ('Network adapter 2').

If you have more the concept should be the same.

I would need to see your complete script to check that.

Btw, it's handier if you attach code snippets as text files, not a screenshots.


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

0 Kudos
jvm2016
Hot Shot
Hot Shot

could you suggest how to pass the .csv file of diferent values .like name.numcpu,memory etc...

0 Kudos
LucD
Leadership
Leadership

You can use the Import-Csv cmdlet for that.

Make sure that the column names in the CSV correspond with the property names you are using in your script.

# CSV has at least a column with the header VMName

# VMName

# VM1

# VM2

#

Import-csv -Path .\vm.csv -UseCulture | %{

    Get-VM -Name $_.VMName

}

To show you a solution that integrates with your script, I would need to see the script


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

0 Kudos
jvm2016
Hot Shot
Hot Shot

i am checking all these one more time however iam attaching the script.Please let me know if you are to view it .

0 Kudos
LucD
Leadership
Leadership

The critical part is how you composed yuor CSV file.

Lines 53-76 describe exactly how the CSV should be composed.

The line number in the error you included earlier doesn't seem to correspond with the line numbers in the script you attached.

In the script line 337 handles 'Network adapter 1 ', not 'Network adapter 2' like the error message seems to indicate.

The attached script also clearly does splatting (as indicated by the @ in the following line).

$VM | Get-NetworkAdapter |where { $_.Name -eq 'Network adapter 1' } | Set-NetworkAdapter @network | Out-Null

In the error message you seem to have replaced that @ by a $.

The attached script in any case does not correspond with what you seem to be running.


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

0 Kudos
jvm2016
Hot Shot
Hot Shot

Thanks for your reply.

currently script fails while configuring second adapter .

error:null value

so i checked cSv and found only one ipaddress and corresponding network info is given .

iam not sure how to pass multiple values.

0 Kudos
LucD
Leadership
Leadership

The original script you are using is not able to handle multiple vNICs and multiple IP addresses.

You obviously added logic to cope with a 2nd vNIC and IP address, your IPAddress2 and network2 additions.

In your effort to update the script, you apparently changed the splatting (@ changed in a $)

Did you do those changes to the script?

And did you change the CSV accordingly?


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

0 Kudos
jvm2016
Hot Shot
Hot Shot

I have deleted my additions.

also I changed $ to @ .

so the script which i posted is not able to handle multiple nics??

0 Kudos
LucD
Leadership
Leadership

Not in its original form (as published on that website).


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

0 Kudos
jvm2016
Hot Shot
Hot Shot

Somehow i think that splatting is used to solve this problem .

only thing which is pending is update .csv file accordingly which iam not sure how to pass multiple values.

0 Kudos
LucD
Leadership
Leadership

When you changed the script (with properties like IPAddress2 and network2), did you also update the CSV accordingly.
And no, splatting has nothing to do with supporting a 2nd vNIC, you mistyped there.


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

0 Kudos
jvm2016
Hot Shot
Hot Shot

Well in that case i am thinking to make mandatory parameter for second nic the way we did it for ventername and credential .

i am able to think only this solution right now .do you have any other option like using import excel module instead of importing csv ??

0 Kudos
LucD
Leadership
Leadership

No, it is quite simple, when you introduce new properties for the VM (like a 2nd vNIC), you have to add the corresponding columns in the CSV as well.

Nothing to do with CSV or XLSX


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

0 Kudos
jvm2016
Hot Shot
Hot Shot

i have following portion of .csv file  these are the headers for network thier values not shown in following output.

pastedImage_0.png

now to facilitate vnic2 configuration i added same again with thier values.

so i added following in .csv file

pastedImage_1.png

is this how .csv should be modified??

0 Kudos
LucD
Leadership
Leadership

Those 2 lines look exactly the same to me.

Perhaps dump the columns of the CSV than you can see what is there.

You can do something like this

Import-Csv -Path .\your-input.csv | gm


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

0 Kudos