Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PowerShell Script to create a Customized VMware VM

Posted on 2012-03-12
9
Medium Priority
?
1,509 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
9 Comments
 
LVL 124

Accepted Solution

by:
Andrew Hancock (VMware vExpert / EE MVE^2) earned 2000 total points
ID: 37714243
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
ID: 37720296
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 124
ID: 37720321
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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

Author Comment

by:Islandr
ID: 38062892
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
ID: 38839334
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 124
ID: 38839335
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 124
ID: 38842796
2. Accept http:#a37714243
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
In this video tutorial I show you the main steps to install and configure  a VMware ESXi6.0 server. The video has my comments as text on the screen and you can pause anytime when needed. Hope this will be helpful. Verify that your hardware and BIO…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

609 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