PowerShell script exchange
Post Reply
bbuchanan
Influencer
Posts: 16
Liked: 7 times
Joined: Jul 07, 2022 2:05 pm
Full Name: Brian Buchanan
Contact:

Powershell to create labs from CSV

Post by bbuchanan » 2 people like this post

I wrote this little script to create labs from a couple of CSV files. I made this after reformatting my surebackup host a second time and it was getting tedious to recreate all the labs. It's written to make it easy to copy/paste sections into powershell, there are probably better ways to write this. One CSV contains the Lab definition and the second CSV contains the network mappings for each lab.

If there's a better version floating around please direct me to it and I'll delete this.

I haven't tested the sample data in the csv below, hopefully I didn't make any typos.

Code: Select all

Virtual Labs.csv
LabName,LabDescription,LabHostName,LabDataStorename,ApplianceName,AppliancePoolname,ApplianceFolderName,ApplianceProductionNetworkName,ApplianceIPAddress,ApplianceSubnetMask,ApplianceDefaultGateway,AppliancePreferredDNSServer,ApplianceAlternateDNSServer
A_Lab,Lab for SureBackup of A,vmwarehostforveeamsurebackup.example.com,datastore1,A_Lab,A Lab,A Lab,VM Network,10.1.1.2,255.255.255.0,10.1.1.1,8.8.8.8,8.8.4.4
B_Lab,Lab for SureBackup of B,vmwarehostforveeamsurebackup.example.com,datastore1,B_Lab,B Lab,B Lab,VM Network,10.1.1.3,255.255.255.0,10.1.1.1,8.8.8.8,8.8.4.4
For IsolatedMasqueradeAddress I've been counting up from the high bit to avoid surprises when a production network is bigger than a /24, eg. 0, 128, 64, 192, 32, 160, 96...

Code: Select all

Virtual Lab Network Mappings.csv
LabName,ProductionHostName,ProductionNetworkName,ApplianceIPAddress,ApplianceIPSubnet,IsolatedNetworkName,IsolatedNetworkVlanId,IsolatedMasqueradeAddress
A_Lab,productionvmwarehost.example.com,VM Network,10.1.1.1,255.255.255.0,A_Lab VM Network,88,10.255.0.0
A_Lab,productionvmwarehost.example.com,VM Network 2,10.2.1.1,255.255.255.0,A_Lab VM Network 2,99,10.255.128.0
B_Lab,productionvmwarehost.example.com,VM Network,10.1.1.1,255.255.255.0,B_Lab VM Network,88,10.255.64.0
B_Lab,productionvmwarehost.example.com,VM Network 2,10.2.1.1,255.255.255.0,B_Lab VM Network 2,99,10.255.192.0

Code: Select all

$veeamserver="veeam.example.com"
Connect-VBRServer -Server $veeamserver

$ThisLabName="A_Lab"

$Labs=import-csv 'Virtual Labs.csv'

$LabNetworkMappings=import-csv 'Virtual Lab Network Mappings.csv'

foreach ($Lab in $Labs) {
  # comment out the below to make all the labs.
  if ($lab.LabName -ne $ThisLabName )
  {
    continue
  }
  
  $LabHost = Get-VBRServer -Type ESXi -Name $lab.Labhostname
  if (-not $Labhost)
  {
    write "Host not found"
	continue
  }

  # Get the data store information
  $LabDataStore = Find-VBRViDatastore -Name $lab.LabDataStoreName -Server $LabHost
  if (-not $LabDataStore)
  {
    write "Datastore not found"
    continue
  }

  # Get the Production Network
  $ApplianceProductionNetwork = Get-VBRViServerNetworkInfo -Server $Labhost | where NetworkName -eq $lab.ApplianceProductionNetworkName
  if (-not $ApplianceProductionNetwork )
  {
    write "Production Network not found"
    continue
  }

  $MappingRules=@()
  $MappingNetworkOptions=@()
  foreach ($LabNetworkMapping in $LabNetworkMappings)
  {
    if ($LabNetworkMapping.LabName -ne $lab.Labname)
	{
	  continue
	}
    
	$VBRProductionServer = Get-VBRServer -Type ESXi -Name $LabNetworkMapping.ProductionHostName
    if ( -not $VBRProductionServer )
	{
	  write "Production host not found"
	  continue
  	}
    
	$ProductionNetworkInfo = Get-VBRViServerNetworkInfo -Server $VBRProductionServer | where NetworkName -eq $LabNetworkMapping.ProductionNetworkName
    if ( -not $ProductionNetworkInfo )
	{
	  write "Production network not found"
	  continue
	}

    $MappingRule=New-VBRViNetworkMappingRule -Server $LabHost -ProductionNetwork $ProductionNetworkinfo -IsolatedNetworkName $LabNetworkMapping.IsolatedNetworkname -VLANID $LabNetworkMapping.IsolatednetworkVlanId

    $MappingRules+=$MappingRule
          
    $MappingNetworkOptions+=New-VBRViVirtualLabNetworkOptions -NetworkMappingRule $MappingRule `
      -IPAddress $LabNetworkMapping.ApplianceIPAddress `
   	  -SubnetMask $LabNetworkMapping.ApplianceIPSubnet `
   	  -MasqueradeIPAddress $LabNetworkMapping.IsolatedMasqueradeAddress
  }

  # Define the Appliance
  # V11 doesn't have a  "-EnableIPv4Interface" remove it if using 11
  $LabProxyAppliance = New-VBRViVirtualLabProxyAppliance -Server $Labhost `
    -Name $lab.Labname `
    -Datastore $Labdatastore `
    -Network $Applianceproductionnetwork `
    -EnableIPv4Interface `
    -IPAddress $lab.ApplianceIPAddress `
    -SubnetMask $lab.ApplianceSubnetMask `
    -DefaultGateway $lab.ApplianceDefaultGateway `
    -PreferredDNSServer $lab.AppliancePreferredDNSServer `
    -AlternateDNSServer $lab.ApplianceAlternateDNSServer
  
  # Create the Lab using the appliance
  Add-VBRViAdvancedVirtualLab -Server $Labhost `
    -Name $lab.Labname `
    -Description $lab.LabDescription `
    -DesignatedResourcePoolName $lab.AppliancePoolName `
    -DesignatedVMFolderName $lab.ApplianceFolderName `
    -ProxyAppliance $LabProxyAppliance `
    -NetworkMappingRule $MappingRules `
    -NetworkOptions $MappingNetworkOptions `
    -CacheDatastore $Labdatastore `
    -EnableRoutingBetweenvNics `
    -Force
}
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 9 guests