ganapa2000
Enthusiast
Enthusiast

Unable to validate and add new harddisk

Jump to solution

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

0 Kudos
1 Solution

Accepted Solutions
ganapa2000
Enthusiast
Enthusiast

yes, it is getting the details

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

vmname   ahdd

------   ----

newtemp1 10

View solution in original post

0 Kudos
8 Replies
LucD
Leadership
Leadership

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

0 Kudos
ganapa2000
Enthusiast
Enthusiast

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

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
ganapa2000
Enthusiast
Enthusiast

sure, hereby attached

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
ganapa2000
Enthusiast
Enthusiast

yes, it is getting the details

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

vmname   ahdd

------   ----

newtemp1 10

View solution in original post

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
ganapa2000
Enthusiast
Enthusiast

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

0 Kudos