VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Unable to validate and add new harddisk

Hi,

I am unable to validate and add new harddisk from below script. I want to validate, if the value is greater than 0 then add the hardisk else skip this step.

$vmlist = Import-CSV ".\vmdeploy1.csv" -UseCulture

foreach ($item in $vmlist) {

#Grabbing the vm object once for efficiency.

$VM = Get-VM $item.vmname

#Add Harddisk

if($item.ahdd -gt 0){

$VM | New-HardDisk -CapacityGB $item.ahdd -Confirm:$false

}

else

{

Get-VM $vm | Start-VM -Confirm:$false

}

}

There is no error and harddisk.is not getting added.

Please help

Reply
0 Kudos
1 Solution

Accepted Solutions
ganapa2000
Hot Shot
Hot Shot
Jump to solution

yes, it is getting the details

Import-Csv ".\vmdeploy.csv" -UseCulture | select vmname, ahdd

vmname   ahdd

------   ----

newtemp1 10

View solution in original post

Reply
0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

The script looks ok, are you sure your CSV file is correct?

It should have a layout as follows

vmname,ahdd

vm1,1

vm2,2


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

csv looks good. i tried replace the this code

if($item.ahdd -gt 0){

$VM | New-HardDisk -CapacityGB $item.ahdd -Confirm:$false

}

to

$VM | New-HardDisk -CapacityGB $item.ahdd -Confirm:$false

Now I am getting this error

New-HardDisk : Cannot validate argument on parameter 'CapacityGB'. The argument is null. Provide a valid value for the argument, and then try running the command again.

At D:\deploy_Template\vmdeploy.ps1:27 char:32

+ $VM | New-HardDisk -CapacityGB $item.ahdd -Confirm:$false

+                                ~~~~~~~~~~

    + CategoryInfo          : InvalidData: (:) [New-HardDisk], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.NewHardDisk

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That seems to indicate something goes wrong in the CSV.
Can you share/attach your CSV?


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

sure, hereby attached

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Looks ok.
Can you check if this returns the expected values?

Import-Csv ".\vmdeploy1.csv" -UseCulture | select vmname, ahdd


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

yes, it is getting the details

Import-Csv ".\vmdeploy.csv" -UseCulture | select vmname, ahdd

vmname   ahdd

------   ----

newtemp1 10

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Strange.
For me this seems to work ok.
Perhaps far-fetched, but can you attach the .ps1 file you are using?
There might something funny in there.


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

Thanks for your time and help.

I resolved the issue by deleting the .csv file and recreated. now it is working.

Not sure, what was the issue Smiley Happy

Reply
0 Kudos