VMware Cloud Community
russjar
Enthusiast
Enthusiast
Jump to solution

New-VM

Hi all,

I'm trying to script out the creation of a bunch of VMs. I'm using the Import-CSV option and when I run the script I'm continually recieving the error

"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 ag
ain.
At D:\Pathtoscriptandscriptname.ps1:4 char:15
+         New-VM -Name <<<<  $_.Name -VMHost (Get-VMHost GetHosts) -Datastor
e (Get-Datastore GetTheDatastores) -DiskMB $_.OSDisk -MemoryMB $_.Mem -NumCpu $_.Num
CPU -RunAsync -WhatIf
    + CategoryInfo          : InvalidData: (:) [New-VM], ParameterBindingValid
   ationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom
   ation.ViCore.Cmdlets.Commands.NewVM"

Connect-VIServer VCServer
Import-CSV D:\Pathtoscriptandscriptname.ps1 | Foreach
(
        New-VM -Name $_.Name -VMHost ESXHost -MemoryMB $_.Mem -NumCpu $_.NumCPU -RunAsync -WhatIf

)

Also when I run the script I receive the following...


cmdlet ForEach-Object at command pipeline position 2
Supply values for the following parameters:
Process[0]:

I've ran the Import-CSV at the command window and it displays at the command window fiine. I can't figure out what the problems is....

Thanks in Advance.

VCP,MCSE NT4/W2k/W2k3, MCSA W2k3
Tags (1)
0 Kudos
1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership
Jump to solution

If you do:

Get-Help New-VM -Full


you will see under SYNTAX that there are four parameter sets for the New-VM cmdlet. The DiskMB, MemoryMB, NumCPU and GuestID parameters are only in the first parameter set. The VM parameter is only in the second parameter set. You should use parameters from one parameter set only. Otherwise you get the "Parameter set cannot be resolved using the specified named parameters" error.

Using Hal Rottenberg's Get-Parameter function you can easily see to which parameter set a parameter belongs.

Get-Parameter New-VM | Sort-Object ParameterSet,Name | Select-Object Name,Parameterset | Format-Table -AutoSize


gives the following output:

Name                ParameterSet
----                ------------
Datastore           CloneVm
Description         CloneVm
DiskStorageFormat   CloneVm
DrsAutomationLevel  CloneVm
HAIsolationResponse CloneVm
HARestartPriority   CloneVm
Location            CloneVm
Name                CloneVm
OSCustomizationSpec CloneVm
ResourcePool        CloneVm
RunAsync            CloneVm
Server              CloneVm
VApp                CloneVm
VM                  CloneVm
VMHost              CloneVm
AlternateGuestName  DefaultParameterSet
CD                  DefaultParameterSet
Datastore           DefaultParameterSet
Description         DefaultParameterSet
DiskMB              DefaultParameterSet
DiskPath            DefaultParameterSet
DiskStorageFormat   DefaultParameterSet
DrsAutomationLevel  DefaultParameterSet
Floppy              DefaultParameterSet
GuestId             DefaultParameterSet
HAIsolationResponse DefaultParameterSet
HARestartPriority   DefaultParameterSet
Location            DefaultParameterSet
MemoryMB            DefaultParameterSet
Name                DefaultParameterSet
NetworkName         DefaultParameterSet
NumCpu              DefaultParameterSet
ResourcePool        DefaultParameterSet
RunAsync            DefaultParameterSet
Server              DefaultParameterSet
VApp                DefaultParameterSet
Version             DefaultParameterSet
VMHost              DefaultParameterSet
VMSwapfilePolicy    DefaultParameterSet
Description         RegisterVm
DrsAutomationLevel  RegisterVm
HAIsolationResponse RegisterVm
HARestartPriority   RegisterVm
Location            RegisterVm
Name                RegisterVm
ResourcePool        RegisterVm
RunAsync            RegisterVm
Server              RegisterVm
VApp                RegisterVm
VMFilePath          RegisterVm
VMHost              RegisterVm
Datastore           Template
Description         Template
DiskStorageFormat   Template
DrsAutomationLevel  Template
HAIsolationResponse Template
HARestartPriority   Template
Location            Template
Name                Template
OSCustomizationSpec Template
ResourcePool        Template
RunAsync            Template
Server              Template
Template            Template
VApp                Template
VMHost              Template

If you want to clone a VM you can only use parameters from the CloneVm parameter set.

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

0 Kudos
7 Replies
mattboren
Expert
Expert
Jump to solution

Hello, russjar-

Looks like you just have a minor issue with the "foreach".  Try:

Connect-VIServer VCServer
Import-CSV D:\Pathtoscriptandscriptname.csv | Foreach-Object {
   
New-VM -Name $_.Name -VMHost ESXHostName -MemoryMB $_.Mem -NumCpu $_.NumCPU -RunAsync -WhatIf
}

The way you were using it (with the pipeline variable, $_, in the New-VM statement), you are intending to use the Foreach-Object cmdlet, aliased as "foreach".  But, when you use parenthesis instead of curly brackets/braces, the "foreach" then gets interpreted as the loop language construct, instead of as Foreach-Object.  So, the parenthesis instead of the curly brackets were the issue.

I also changed it from using the "foreach" alias to using the full cmdlet name, "Foreach-Object", but only for clarity's sake.  The alias (again, which is different from a foreach loop), would work just the same.

If you have not seen it, you can get more info about the foreach loop via

Get-Help -full about_Foreach

russjar
Enthusiast
Enthusiast
Jump to solution

Hey Matt thanks of the imput, have made necessary changes but still erroring at the 'Name', although I'm receiveng multipule errors this time whereas previously I was recieving a single error on the 'Name' which tells me the script is reading the input file correctly this time.

VCP,MCSE NT4/W2k/W2k3, MCSA W2k3
0 Kudos
mattboren
Expert
Expert
Jump to solution

Hey, russjar-

I am wondering about your CSV file -- you have a "header" row in it, right?  So, the first line is a header row with column names, and then the data rows begin, something like:

Name,Mem,NumCPU

test0,1024,1
test1,2048,2

It seems like that might be missing, but to help troubleshoot, what errors are you getting now?

Btw, if you do not have the header row in the .csv file and just have the data rows, you can specify a header when you use the Import-Csv cmdlet as such:

...

Import-Csv D:\Pathtoscriptandscriptname.csv -Header Name,Mem,NumCPU

...

russjar
Enthusiast
Enthusiast
Jump to solution

Hi Matt,

Your suggestinos have got the script working, however I am also trying to clone an existing VM by adding the "-VM VMName" but that's giving me grief now. If I omit the -VM VMName the script runs through without error.

New-vm -Name vmname -vmhost esxhost -Datastore datastore -DiskMB 51200,348160 -MemoryMB 4096 -NumCpu 2 -GuestId windows7Server64Guest -VM vmtoclone -RunAsync -WhatIf

this is the exact error I receive when running the new-vm commend

New-VM : Parameter set cannot be resolved using the specified named parameters.
At line:1 char:7
+ New-VM <<<<  -Name guestname -VMHost hostname
-Datastore datastore -DiskMB 51200,348160 -MemoryM
B 4096 -NumCpu 2 -GuestId windows7server64guest -WhatIf -VM vm_shell
    + CategoryInfo          : InvalidArgument: (:) [New-VM], ParameterBindingE
   xception
    + FullyQualifiedErrorId : AmbiguousParameterSet,VMware.VimAutomation.ViCor
   e.Cmdlets.Commands.NewVM

Think I've narrowed this down to the -DiskMb variable. Without this variable the command runs through fine.

(having one of those days)

VCP,MCSE NT4/W2k/W2k3, MCSA W2k3
0 Kudos
bulletprooffool
Champion
Champion
Jump to solution

Guest ID for a Windows 7 64 bit machine should be

windows7_64Guest

Not

windows7Server64Guest   (I think)

have you had a look at Onyx for generating code? I find it quite handy for solving nuances like this.

Here's a quick how to:

http://www.get-virtual.info/2011/02/16/script-of-the-day-quick-and-easy-vmware-powershell-scripts/

One day I will virtualise myself . . .
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

If you do:

Get-Help New-VM -Full


you will see under SYNTAX that there are four parameter sets for the New-VM cmdlet. The DiskMB, MemoryMB, NumCPU and GuestID parameters are only in the first parameter set. The VM parameter is only in the second parameter set. You should use parameters from one parameter set only. Otherwise you get the "Parameter set cannot be resolved using the specified named parameters" error.

Using Hal Rottenberg's Get-Parameter function you can easily see to which parameter set a parameter belongs.

Get-Parameter New-VM | Sort-Object ParameterSet,Name | Select-Object Name,Parameterset | Format-Table -AutoSize


gives the following output:

Name                ParameterSet
----                ------------
Datastore           CloneVm
Description         CloneVm
DiskStorageFormat   CloneVm
DrsAutomationLevel  CloneVm
HAIsolationResponse CloneVm
HARestartPriority   CloneVm
Location            CloneVm
Name                CloneVm
OSCustomizationSpec CloneVm
ResourcePool        CloneVm
RunAsync            CloneVm
Server              CloneVm
VApp                CloneVm
VM                  CloneVm
VMHost              CloneVm
AlternateGuestName  DefaultParameterSet
CD                  DefaultParameterSet
Datastore           DefaultParameterSet
Description         DefaultParameterSet
DiskMB              DefaultParameterSet
DiskPath            DefaultParameterSet
DiskStorageFormat   DefaultParameterSet
DrsAutomationLevel  DefaultParameterSet
Floppy              DefaultParameterSet
GuestId             DefaultParameterSet
HAIsolationResponse DefaultParameterSet
HARestartPriority   DefaultParameterSet
Location            DefaultParameterSet
MemoryMB            DefaultParameterSet
Name                DefaultParameterSet
NetworkName         DefaultParameterSet
NumCpu              DefaultParameterSet
ResourcePool        DefaultParameterSet
RunAsync            DefaultParameterSet
Server              DefaultParameterSet
VApp                DefaultParameterSet
Version             DefaultParameterSet
VMHost              DefaultParameterSet
VMSwapfilePolicy    DefaultParameterSet
Description         RegisterVm
DrsAutomationLevel  RegisterVm
HAIsolationResponse RegisterVm
HARestartPriority   RegisterVm
Location            RegisterVm
Name                RegisterVm
ResourcePool        RegisterVm
RunAsync            RegisterVm
Server              RegisterVm
VApp                RegisterVm
VMFilePath          RegisterVm
VMHost              RegisterVm
Datastore           Template
Description         Template
DiskStorageFormat   Template
DrsAutomationLevel  Template
HAIsolationResponse Template
HARestartPriority   Template
Location            Template
Name                Template
OSCustomizationSpec Template
ResourcePool        Template
RunAsync            Template
Server              Template
Template            Template
VApp                Template
VMHost              Template

If you want to clone a VM you can only use parameters from the CloneVm parameter set.

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
russjar
Enthusiast
Enthusiast
Jump to solution

Hi Robert yes you are quite right, I did come to this conslusion myself before you commented.

Do appreciate your input/feedback along with everyone else.

VCP,MCSE NT4/W2k/W2k3, MCSA W2k3
0 Kudos