VMware Cloud Community
td_sk
Contributor
Contributor
Jump to solution

powershell script modification

Hi Thanks ,

Can some one help to modify the script  .where the script need to execute  the second line if only the HDD2 value exists in CSV file. I tried different option but no luck.

The CSV file is created by an front end web tool and in backend we are using ps script.So some times the user may not required the 2nd HDD in vm.

Sample CSV file

NameTemplatehostdatastorecustomizationCPUMemoryhdd1hdd2hdd2
VM-DeployVMTMPENT2K8ESXi-host1VM-DEPLOY-DSwin 20084125

Script we are using is

Connect-VIServer localhost

$vms = Import-CSV E:\vm-deploy.csv

foreach($vm in $vms){

   Set-VM $vm.name -NumCpu $vm.CPU -MemoryGB $vm.Memory -Confirm:$false

        New-HardDisk -VM $vm.name -CapacityGB $vm.hdd1 -Datastore $vm.datastore -StorageFormat EagerZeroedThick -Controller "SCSI Controller 1"

            New-HardDisk -VM $vm.name -CapacityGB $vm.hdd2 -Datastore $vm.datastore -StorageFormat EagerZeroedThick -Controller "SCSI Controller 1"

               New-HardDisk -VM $vm.name -CapacityGB $vm.hdd3 -Datastore $vm.datastore -StorageFormat EagerZeroedThick -Controller "SCSI Controller 1"

}

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try something like this

Connect-VIServer localhost

$vms = Import-CSV E:\vm-deploy.csv

foreach($vm in $vms){

   Set-VM $vm.name -NumCpu $vm.CPU -MemoryGB $vm.Memory -Confirm:$false

   New-HardDisk -VM $vm.name -CapacityGB $vm.hdd1 -Datastore $vm.datastore -StorageFormat EagerZeroedThick -Controller "SCSI Controller 1"

  if($vm.hdd2){

     New-HardDisk -VM $vm.name -CapacityGB $vm.hdd2 -Datastore $vm.datastore -StorageFormat EagerZeroedThick -Controller "SCSI Controller 1"

    if($vm.hdd3)

       New-HardDisk -VM $vm.name -CapacityGB $vm.hdd3 -Datastore $vm.datastore -StorageFormat EagerZeroedThick -Controller "SCSI Controller 1"

    }

  }

}

The script will only check for hdd3 when hdd2 is set.


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

View solution in original post

0 Kudos
1 Reply
LucD
Leadership
Leadership
Jump to solution

Try something like this

Connect-VIServer localhost

$vms = Import-CSV E:\vm-deploy.csv

foreach($vm in $vms){

   Set-VM $vm.name -NumCpu $vm.CPU -MemoryGB $vm.Memory -Confirm:$false

   New-HardDisk -VM $vm.name -CapacityGB $vm.hdd1 -Datastore $vm.datastore -StorageFormat EagerZeroedThick -Controller "SCSI Controller 1"

  if($vm.hdd2){

     New-HardDisk -VM $vm.name -CapacityGB $vm.hdd2 -Datastore $vm.datastore -StorageFormat EagerZeroedThick -Controller "SCSI Controller 1"

    if($vm.hdd3)

       New-HardDisk -VM $vm.name -CapacityGB $vm.hdd3 -Datastore $vm.datastore -StorageFormat EagerZeroedThick -Controller "SCSI Controller 1"

    }

  }

}

The script will only check for hdd3 when hdd2 is set.


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

0 Kudos