Automation

 View Only
  • 1.  Unable to validate and add new harddisk

    Posted Apr 04, 2019 06:48 AM

    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



  • 2.  RE: Unable to validate and add new harddisk

    Posted Apr 04, 2019 07:00 AM

    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



  • 3.  RE: Unable to validate and add new harddisk

    Posted Apr 04, 2019 07:54 AM

    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



  • 4.  RE: Unable to validate and add new harddisk

    Posted Apr 04, 2019 07:56 AM

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



  • 5.  RE: Unable to validate and add new harddisk

    Posted Apr 04, 2019 08:04 AM

    sure, hereby attached



  • 6.  RE: Unable to validate and add new harddisk

    Posted Apr 04, 2019 08:10 AM

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

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



  • 7.  RE: Unable to validate and add new harddisk
    Best Answer

    Posted Apr 04, 2019 08:12 AM

    yes, it is getting the details

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

    vmname   ahdd

    ------   ----

    newtemp1 10



  • 8.  RE: Unable to validate and add new harddisk

    Posted Apr 04, 2019 08:16 AM

    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.



  • 9.  RE: Unable to validate and add new harddisk

    Posted Apr 04, 2019 08:21 AM

    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 :smileyhappy: