title: I need to create to Redundant VMs (both on 2 different datastores) via function
body: <p>I have defined all the needed variables in a separate <code>Variable_Defination.ps1</code> file and have called it up here.
  Is there a better way to do this?</p>
  
  <p>I just want a script to create 2 redundant VMs both running on separate datastores and having Affinity rule set up for disaster recovery cases     </p>
  
  <pre class="lang-bsh prettyprint-override"><code>. ./Variable_Defination.ps1
  
  function CreateRedundantVMs {
      Param(
          [Parameter(Mandatory=$true, ValueFromPipeline=$true)]
          [string]$ClusterName,
          [string]$ComputerName1,
          [string]$ComputerName2,
          [string]$sourcetemplate,
          [string]$InfraResourcePoolName,
          [string]$OSCustomizationspec,
          [string]$description
      )
  
      $Viabledatastores = (Get-Cluster $ClusterName | Get-Datastore) |
                          Where {$_.Name -like '*vSSD*'} |
                          Sort-Object FreeSpaceGB  -Descending |
                          Select -First 2 -ErrorAction 'Stop'
      if ($Viabledatastores) {
          $Viabledatastores
      } else {
          Write-Host "No Viable Datastores found"
          break;
      }
  
      Write-Verbose "`n---------------------- Creating redundant VMs now ------------------------`n " -Verbose
      New-VM -Name $ComputerName1 -ResourcePool $InfraResourcePoolName -Datastore $Viabledatastores[0] -Description $description -Template $sourcetemplate -OSCustomizationspec $OSCustomizationspec -DiskStorageFormat Thin
      New-VM -Name $ComputerName2 -ResourcePool $InfraResourcePoolName -Datastore $Viabledatastores[1] -Description $description -Template $sourcetemplate -OSCustomizationspec $OSCustomizationspec -DiskStorageFormat Thin
      Start-Sleep -s 3
      Write-Host "`n -------------------------------------------------------------------------`n "
      Write-Verbose -Message "Virtual Machine $ComputerName1 and $ComputerName2 Deployed. Powering On" -Verbose
      Write-Host "`n -------------------------------------------------------------------------`n "
      Start-VM -VM $ComputerName1
      Start-VM -VM $ComputerName2
  }
  
  # calling function
  CreateRedundantVMs $ClusterName $ComputerName1 $ComputerName2 $sourcetemplate $InfraResourcePoolName $OSCustomizationspec $description
  </code></pre>