3 Replies Latest reply on Apr 5, 2020 6:52 AM by BReed01

    How to incorporate If/Else csv check when deploying multiple VMs

    BReed01 Novice

      I have a script that I'm working on that will deploy multiple VMs from a csv.  I'm trying to get the script to look at a field in the csv, and if blank, do this, if data, do it this other way.

       

      There's a lot of extra fluff in the script, but here's the relevant info:

       

      $VMs = Import-CSV ".\VM_Deploy.csv"
      
      foreach ($vm in $VMs)
      {
      try
      {
      $Name = $vm.Name
      $Template = Get-Template -Name $vm.Template
      $VMVersion = $vm.VMVersion
      $ResourcePool = $vm.ResourcePool
      $Cluster = Get-Cluster -Name $vm.Cluster
      $VMHost = Get-VMHost -Name $vm.VMHost
      $Datastore = Get-Datastore -Name $vm.Datastore
      $OSCust = Get-OSCustomizationSpec -Name $vm.OSCustomizationSpec
      
      New-VM -Name $Name -Template $Template -ResourcePool $Cluster -VMHost $VMHost -Datastore $Datastore -OSCustomizationSpec $OSCusSpec
      
      

       

      Not every site I am working on will have vCenter clusters setup (or may have multiple).  So what I was trying to do was check if the Cluster cell is blank, and if so, use the New-VM line above, and if it's not blank, then add in a -ResourcePool $Cluster.

       

      I was trying to replace the above New-VM line with something like:

       

      IF ([string]::IsNullOrEmpty($vm.Cluster))
      {
      New-VM -Name $Name -Template $Template -VMHost $VMHost -Datastore $Datastore -OSCustomizationSpec $OSCusSpec 
      }
      Else
      New-VM -Name $Name -Template $Template -ResourcePool $Cluster -VMHost $VMHost -Datastore $Datastore -OSCustomizationSpec $OSCusSpec
      }
      

       

      I also tried something like If ($vm.Cluster -eq $Null) , or IF ($Cluster -eq ' ') etc etc.

       

      I just can't seem to figure out how to get it to read a blank cell and do X, and if not blank, do Y.  It typically just fails right away and nothing gets deployed no matter how I attempt it.

       

      Anyone have any suggestions?  I would greatly appreciate it.

        • 1. Re: How to incorporate If/Else csv check when deploying multiple VMs
          LucD Guru
          Community WarriorsvExpertUser Moderators

          I would need to see your CSV file to make any definite conclusions, but a simple test CSV the following works for me.

          I used an inline CSV for testing, but you can replace the ConvertFrom-Csv with Import-Csv and you CSV file

           

          $data = @'

          VM,Cluster,Datastore

          vm1,cluster1,ds1

          vm2,,ds2

          '@


          ConvertFrom-Csv -InputObject $data -UseCulture -PipelineVariable row |

          ForEach-Object -Process {

              if($row.Cluster){

                  Write-Host "For $($row.VM) cluster value is $($row.Cluster)"

              }

              else{

                  Write-Host "For $($row.VM) cluster field is empty "

              }

          }


          A basic concept with Import-Csv (or ConvertFrom-Csv) is that each property in a row is a string.
          An empty string is translated to $false in a test, while a string with anything in there is considered $true.

           

          The other tests you used

          $row.Cluster -eq ''

           

          or

           

          [string]::IsNullOrEmpty($row.Cluster)

           

          should also work to discover empty cells in the CSV.

           

          There must be something in your CSV that makes these tests not work for you.

          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 2. Re: How to incorporate If/Else csv check when deploying multiple VMs
            BReed01 Novice

            Thank you, LucD.  I'm using an Import-CSV process.

             

            I just went ahead and attached the whole script and csv to the orig post.  I do know there are several things I can probably do better, and do probably have a lot of unnecessary fluff in the script.  The attachment does not have any of the testing I was trying to do.

             

            Any feedback is most welcome though. 

             

            On a side note, I think I was reading that you don't have to use the x86 of Powershell for this type of script anymore.  Do you know if that's true?  I am testing for that in the script, so am curious if it's even necessary.

            • 3. Re: How to incorporate If/Else csv check when deploying multiple VMs
              LucD Guru
              vExpertUser ModeratorsCommunity Warriors

              Your CSV looks like a normal CSV.
              When I tested with that CSV, the if-statements I gave in my previous reply all worked as expected.

               

              And yes, you can don't have to use the 32-bit PS version anymore (when you are on a recent PowerCLI version).

              Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz