Solved

PowerShell Script to create a Customized VMware VM

Posted on 2012-03-12
9
1,311 Views
Last Modified: 2013-02-09
Hi experts,
I got the following Powershell script from this link:

http://communities.vmware.com/message/1753790
 
 
 
Somehow I’ve been trying to run the script, but it does not work, I connect via PowerCLI to my vCenter, I exported to a csv file the configuration that I am trying to accomplished, but when trying to run the script (below) I am getting the errors

$worksheet = Import-CSV "C:\Export-Files\Windows2k8-Template.csv"
$worksheet | ForEach-Object {
      new-VM `
          -Name $_.VM_Name `
          -VMHost $_.Host `
          -Template $_.Template `
          -Datastore $_.Datastore `
          -OSCustomizationspec $_.Customization `
          -Location $_.Location `
          -DiskStorageFormat $_.DiskStorageFormat |`
          Set-VM -NumCpu $_.CPU -MemoryMB $_.Memory -Confirm:$false
          Start-VM -VM $_.VM_Name
         }

Errors that I am getting:
New-VM : Cannot validate argument on parameter 'Name'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.
At C:\Users\jluciano\AppData\Local\Temp\2bfdd9bc-e8b6-47f3-b692-eeae9d50f64a.ps1:4 char:16
+           -Name <<<<  $_.VM_Name `
    + CategoryInfo          : InvalidData: (:) [New-VM], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewVM
 
Start-VM : Cannot validate argument on parameter 'VM'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.
At C:\Users\jluciano\AppData\Local\Temp\2bfdd9bc-e8b6-47f3-b692-eeae9d50f64a.ps1:12 char:23
+           Start-VM -VM <<<<  $_.VM_Name
    + CategoryInfo          : InvalidData: (:) [Start-VM], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.StartVM

My final goal is to get this information on a csv or xml file, that I can deploy based on this info:

Name
Description
NumCpu
MemoryMB
HardDisk1 Size (GB)
HardDisk1 Datastore
HardDisk2 Size (GB)
HardDisk2 Datastore
HardDisk3 Size (GB)
HardDisk3 Datastore
VMSwapfilePolicy
Swap Datastore
PortGroup1 Name
PortGroup1 VLAN
PortGroup2 Name
PortGroup2 VLAN
PortGroup3 Name
PortGroup3 VLAN
Folder
ResourcePool

Of course if I somebody assist me with the script
0
Comment
Question by:Islandr
  • 4
  • 3
9 Comments
 
LVL 117

Accepted Solution

by:
Andrew Hancock (VMware vExpert / EE MVE) earned 500 total points
Comment Utility
It looks as if one, or more, of the Names in your CSV file does not find a VM.

As a consequence the variable $_.VM_Name  will be $null and all the following cmdlets will fail.

This is because of the following

Make sure the header of your CSV has the value Name, and change  $_.VM_Name to $_.Name in the script above.

This will cure the issue of the script not running. Make sure you have a Template and Guest Customization as well for the script to run correctly.

This script will deploy/clone a VM based on the Template and Guest Customization name, using the memory and cpu parameters supplied. Note the Memory is in MB! Not GB!
0
 

Author Comment

by:Islandr
Comment Utility
Hanccoka,

I tried that and did not work, I've been hitting my head against the wall and nothing is really works, my main goal is to get the following info into a xml file:

Name
Description
NumCpu
MemoryMB
HardDisk1 Size (GB)
HardDisk1 Datastore
HardDisk2 Size (GB)
HardDisk2 Datastore
HardDisk3 Size (GB)
HardDisk3 Datastore
VMSwapfilePolicy
Swap Datastore
PortGroup1 Name
PortGroup1 VLAN
PortGroup2 Name
PortGroup2 VLAN
PortGroup3 Name
PortGroup3 VLAN
Folder
ResourcePool


And then build a template where I can deploy a VM based on the information collected from the XML template.  Please let me know if further information is needed.

Thanks,
0
 
LVL 117

Expert Comment

by:Andrew Hancock (VMware vExpert / EE MVE)
Comment Utility
post your CSV, it works fine for me.

do you have

1. A Template created
2. Guest Customization created.

What's the error message.

Post the script you are using.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Islandr
Comment Utility
Hanccoka,

At the end we endup not using the script, but I'll pick your brain some other time thanks.
0
 

Author Comment

by:Islandr
Comment Utility
I've requested that this question be deleted for the following reason:

I am very disappointed with this service, I post a question and I have to give up on it, because nobody bother to replied
0
 
LVL 117

Expert Comment

by:Andrew Hancock (VMware vExpert / EE MVE)
Comment Utility
I am very disappointed with this service, I post a question and I have to give up on it, because nobody bother to replied

I answered your question, and I took the time to test the script and create a CSV file. It tested fine for me. I asked you to post the actual script you were using, and you replied

At the end we endup not using the script

It seems like it was me as a volunteer, that has wasted my time here on replying to this question.
0
 
LVL 117

Expert Comment

by:Andrew Hancock (VMware vExpert / EE MVE)
Comment Utility
2. Accept http:#a37714243
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When we have a dead host and we lose all connections to the ESXi, and we need to find a way to move all VMs from that dead ESXi host.
Will try to explain how to use the VMware feature TAGs in the VMs and create Veeam Backup Jobs using TAGs. Since this article is too long, I will create second article for the Veeam tasks.
Teach the user how to configure vSphere Replication and how to protect and recover VMs Open vSphere Web Client: Verify vsphere Replication is enabled: Enable vSphere Replication for a virtual machine: Verify replicated VM is created: Recover replica…
This tutorial will walk an individual through the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now