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
yes, it is getting the details
Import-Csv ".\vmdeploy.csv" -UseCulture | select vmname, ahdd
vmname ahdd
------ ----
newtemp1 10
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
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
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
Looks ok.
Can you check if this returns the expected values?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
yes, it is getting the details
Import-Csv ".\vmdeploy.csv" -UseCulture | select vmname, ahdd
vmname ahdd
------ ----
newtemp1 10
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
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