VMware Cloud Community
jsmiele
Contributor
Contributor

Problem new-vm within a script specifying more than 1 disk

I'm having an issue running New-VM within a script where I read in a csv file to a set of variables and construct the new-vm command.  Everything executes fine when a single disk is specified and the VM is created properly.  If however more than a single disk is specified which the cmdlet says is acceptable with the new -DiskGB optional value separated by commas, the resulting VM is improperly built.  When this is fed through the script it seemingly sends the correct string (-DiskGB 1,1) however it runs this to a [decimal] value dropping the comma and creating a single 11G disk.  I can successfully run the new-vm command with the exact string I'm sending with the script in a powershell window and get the correct VM built.  The issue seems to be with trying to automate it and I can't determine how to pass the disks variable so that it will be properly interpreted.

0 Kudos
7 Replies
LucD
Leadership
Leadership

That could be your regional setting that does this. Do you use the comma as the decimal seperator ?

Try it like this perhaps -DiskGB [decimal]1,[decimal]1


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

0 Kudos
jsmiele
Contributor
Contributor

No standard period decimal separator, but I will try sending it that way to see if it changes anything with the functionality.

0 Kudos
LucD
Leadership
Leadership

You could also try it this way

$diskSizes = 1,1

New-VM ... -DiskGB $diskSizes


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

0 Kudos
jsmiele
Contributor
Contributor

That's actually where the problem lies, I'm reading in a csv file and assigning the values to variables which I have on the new-vm line.  So at present it looks something like..

......DiskSize......

......,"1,1".....

$VM_Disk = Parameter.DiskSize

New-VM ... -DiskGB $VM_Disk

The $VM_Disk variable is assigned 1,1 properly, and if I step through the script or write out to screen whats being executed at the New-VM line its correctly entered as -DiskGB 1,1.  But when the actual cmdlet runs it does something with the 1,1 and drops the comma.  Looking at the expected value type -DiskGB runs on a decimal value, but $VM_Disk is being reported as a string value.  If I run [decimal]$VM_Disk it drops the comma from the value also giving me 11.  Somehow when you run just the cmdlet without it being part of a script with a fed in value it's fine accepting the 1,1 and will generate two 1G disks.  But when you feed it the numbers in a script I can only assume its running a value type conversion to change the string to a decimal.  I tried playing around with value conversions telling it the variable contained an integer but that led to the same results.

0 Kudos
LucD
Leadership
Leadership

From Import-Csv the value is a [string], you could check by veryfing the type of this Parameter.DiskSize

Why don't you use 2 seperate columns in the CSV and use 2 variables on the DiskGB parameter.

The CSV

....,Disk1,Disk2,....

...,1,1,....

...,2,1....

When you import the CSV file you will get 2 variables $_.Disk1 and $_.Disk2.

Now feed those to the parameter

New-VM ....-DiskGB $_.Disk1,$_.Disk2


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

0 Kudos
jsmiele
Contributor
Contributor

This is actually what brought me to using the newer DiskGB option value over the DiskMB (aside from the fact that DiskMB is being deprecated).  I was looking to keep the script and the CSV file as clean and generic as possible.  Without the need of additional if blocks to determine if additional disks had been specified as input parameters.

0 Kudos
DCasota
Expert
Expert

I had the same problem. Maybe this snippet is helpful:

$filepath = "list.csv"

$VMs = Import-CSV $filepath -delimiter ";"

Foreach ($VM in $VMs)

{

$strVMDiskSizes=$VM.DiskGB

$arrVMDiskSizes=@($strVMDiskSizes.Split(","))

$DiskGB=@()

foreach ($DiskSize in $arrVMDiskSizes)

{

  $DiskGB += [Decimal]$DiskSize

}

Not pretty, but it works.

0 Kudos