PowerShell script exchange
Post Reply
eziemann
Lurker
Posts: 1
Liked: 3 times
Joined: Mar 12, 2020 2:45 pm
Full Name: Evan Ziemann
Contact:

Scripting Replication job network mapping

Post by eziemann » 3 people like this post

I wanted to share a script to update the network mappings associated in Replication jobs.
This is for VMware replications from a distributed vSwitch to a standard vSwitch.
(I guess standard to standard works if the names are the same)

1 issue I found with this script is that the VMs associated for the job can change. If you removed VMs from a job and then run this script, the removed ones will appear again.
I let VMware know about that issue in a ticket.

There is lots of cleanup that could be done to the arrays with loops. This just got the job done for me (otherwise I would have had over 1000 clicks in the GUI)

Code: Select all

asnp "VeeamPSSnapIn" -ErrorAction SilentlyContinue


$JobName =  "Test_Repl"

#Map Source to Destination VMware network names
$srcVLAN0 = "VLAN2100-IT_RDS"
$dstVLAN0 = "VLAN2100-IT_RDS"

$srcVLAN1 = "VLAN2101-IT_Monitoring"
$dstVLAN1 = "VLAN2101-IT_Monitoring"

$srcVLAN2 = "VLAN2102-IT_Prod"
$dstVLAN2 = "VLAN2102-IT_Prod"

$srcVLAN3 = "VLAN2103-IT_Test"
$dstVLAN3 = "VLAN2103-IT_Test"

$srcVLAN4 = "VLAN2110-Intranet_Prod"
$dstVLAN4 = "VLAN2110-Intranet_Prod"

$srcVLAN5 = "VLAN2111-Intranet_Test"
$dstVLAN5 = "VLAN2111-Intranet_Test"

$srcVLAN6 = "VLAN0001-Prod"
$dstVLAN6 = "DR_ReplVMs"

$srcVLAN7 = "VLAN0999-DMZ"
$dstVLAN7 = "DR_DMZ"

$job = Get-VBRJob -Name $JobName

#Source Network is a distributed switch
#Destination networks are standard switch
$srcNet0 = Get-VBRViServerNetworkInfo -Server "hq-esx1.corp.com" | Where { $_.Type -eq "ViDVS" -and $_.SwitchName -eq "HQ-DSwitch" -and $_.NetworkName -eq $srcVLAN0 }
$dstNet0 = Get-VBRViServerNetworkInfo -Server "dr-esx1.corp.com" | Where-Object { $_.NetworkName -eq $dstVLAN0 } 

$srcNet1 = Get-VBRViServerNetworkInfo -Server "hq-esx1.corp.com" | Where { $_.Type -eq "ViDVS" -and $_.SwitchName -eq "HQ-DSwitch" -and $_.NetworkName -eq $srcVLAN1 }
$dstNet1 = Get-VBRViServerNetworkInfo -Server "dr-esx1.corp.com" | Where-Object { $_.NetworkName -eq $dstVLAN1 } 

$srcNet2 = Get-VBRViServerNetworkInfo -Server "hq-esx1.corp.com" | Where { $_.Type -eq "ViDVS" -and $_.SwitchName -eq "HQ-DSwitch" -and $_.NetworkName -eq $srcVLAN2 }
$dstNet2 = Get-VBRViServerNetworkInfo -Server "dr-esx1.corp.com" | Where-Object { $_.NetworkName -eq $dstVLAN2 } 

$srcNet3 = Get-VBRViServerNetworkInfo -Server "hq-esx1.corp.com" | Where { $_.Type -eq "ViDVS" -and $_.SwitchName -eq "HQ-DSwitch" -and $_.NetworkName -eq $srcVLAN3 }
$dstNet3 = Get-VBRViServerNetworkInfo -Server "dr-esx1.corp.com" | Where-Object { $_.NetworkName -eq $dstVLAN3 } 

$srcNet4 = Get-VBRViServerNetworkInfo -Server "hq-esx1.corp.com" | Where { $_.Type -eq "ViDVS" -and $_.SwitchName -eq "HQ-DSwitch" -and $_.NetworkName -eq $srcVLAN4 }
$dstNet4 = Get-VBRViServerNetworkInfo -Server "dr-esx1.corp.com" | Where-Object { $_.NetworkName -eq $dstVLAN4 } 

$srcNet5 = Get-VBRViServerNetworkInfo -Server "hq-esx1.corp.com" | Where { $_.Type -eq "ViDVS" -and $_.SwitchName -eq "HQ-DSwitch" -and $_.NetworkName -eq $srcVLAN5 }
$dstNet5 = Get-VBRViServerNetworkInfo -Server "dr-esx1.corp.com" | Where-Object { $_.NetworkName -eq $dstVLAN5 } 

$srcNet6 = Get-VBRViServerNetworkInfo -Server "hq-esx1.corp.com" | Where { $_.Type -eq "ViDVS" -and $_.SwitchName -eq "HQ-DSwitch" -and $_.NetworkName -eq $srcVLAN6 }
$dstNet6 = Get-VBRViServerNetworkInfo -Server "dr-esx1.corp.com" | Where-Object { $_.NetworkName -eq $dstVLAN6 } 

$srcNet7 = Get-VBRViServerNetworkInfo -Server "hq-esx1.corp.com" | Where { $_.Type -eq "ViDVS" -and $_.SwitchName -eq "HQ-DSwitch" -and $_.NetworkName -eq $srcVLAN7 }
$dstNet7 = Get-VBRViServerNetworkInfo -Server "dr-esx1.corp.com" | Where-Object { $_.NetworkName -eq $dstVLAN7 } 


#Create array of source and then dest. network objects
$arrSrc = $srcNet0, $srcNet1, $srcNet2, $srcNet3, $srcNet4, $srcNet5, $srcNet6, $srcNet7
$arrDst = $dstNet0, $dstNet1, $dstNet2, $dstNet3, $dstNet4, $dstNet5, $dstNet6, $dstNet7

#update the job with array of source and array of dest. networks
Set-VBRViReplicaJob -Job $job -EnableNetworkMapping -SourceNetwork $arrSrc -TargetNetwork $arrDst

oleg.feoktistov
Veeam Software
Posts: 2010
Liked: 670 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Scripting Replication job network mapping

Post by oleg.feoktistov »

Hi Evan,

Great script, thanks for sharing!

Cheers,
Oleg
jmke79
Novice
Posts: 9
Liked: 3 times
Joined: Aug 13, 2020 1:17 pm
Full Name: JM
Contact:

Re: Scripting Replication job network mapping

Post by jmke79 »

Thank you eziemann; your script did the trick for me.

I'm using VEEAM Replica jobs to migrate virtual machines between two different vcenters.
Old vcenter (src) has standard switches, has multiple DataCenter objects, with under them multiple clusters, and under them again multiple ESX hosts.
(network names on src between different clusters that have the same name, I also assume have same VLANID)

Destination vcenter (dst) uses VDS, one single DataCenter object, and a few clusters with VDS per cluster.
At the same time, I'm renaming network labels between src <> dst Vcenters, and with VEEAM replica this can go completely transparent; no manual action, woohoo :)

however that replica job requires to have all those network mappings old>new. Having the possibility to import large import mapping jobs would be an awesome feature.
In the meantime I had to make do with this abomination of a powershell script (noob here). I'm importing source and destination networks from a csv (source,destination columns)

Code: Select all

##############################
## Load Modules and Snapins ##
##############################

asnp "VeeamPSSnapIn" -ErrorAction SilentlyContinue
Import-Module VMware.VimAutomation.Core
Import-Module VMware.VimAutomation.Vds

######################
## BEGIN VARIABLES  ## 
######################

##preq; VEEAM Replica job that exists
$JobName =  "name_of_existing_replicajob"
$job = Get-VBRJob -Name $JobName

##Define Source vcenter
$VCENTER_SRV1 = "vcenter FQDN"
##Define Destination vcenter
$VCENTER_SRV2 = "vcenter FQDN"

##Define Source DATACenter to go through clusters and STD vswitch network
$datacenter = "name datacenter on SRC vcenter"

##Define destination ESX and VDS from target cluster 
$targetesx = "ESX FQDN of vmhost in target cluster"
$targetvds = "name of target virtual distributed switch assigned to the cluster"

##location of input csv: with source,destination columns (source networkname, destination networkname)
$csv = import-csv "location to .csv"

######################
## END VARIABLES  ## 
######################


#Connect to VCENTER1
Connect-VIServer $VCENTER_SRV1  -WarningAction 0
#Connect to VCENTER2
Connect-VIServer $VCENTER_SRV2  -WarningAction 0


##loop through csv and add ViReference from source and destination to arrays
$srcarray = @()
$dstarray = @()
$csv | foreach-object {
	$source = $_.source
	$destination =$_.destination
	write-host "###############################################################" -foregroundcolor white
	write-host "Source network: $source" -foregroundcolor blue -backgroundcolor white
	write-host "###############################################################" -foregroundcolor white
	write-host "Going through all ESX in:" -NoNewline -foregroundcolor white 
	## set found variable to false
	$found = 'false'
	foreach($cluster in Get-Cluster -Location $datacenter)
	{
		##if network is not yet found, go through clusters
		if ($found -eq 'false')
		{
			write-host " $cluster :" -NoNewline -foregroundcolor white
			
			##if network is not found yet, go through esx hosts
			if ($found -eq 'false')
			{
				foreach($esx in Get-VMHost -Location $cluster)
				{
					$srcNet0 = Get-VBRViServerNetworkInfo -Server "$esx" | Where-Object { $_.NetworkName -eq $source } 
					if ( $srcNet0 )
					{
					$ViReference = $srcNet0.ViReference
					$srcarray += $srcNet0
					write-host " "  -foregroundcolor white
					write-host "--------------------------------------------------" -foregroundcolor white 
					write-host "   found network: $srcVLAN0 on $esx" -foregroundcolor white 
					write-host "         ViReference: $ViReference"  -foregroundcolor white 
					write-host " "  -foregroundcolor white
					##network found, set variable to true
					$found = 'true'
					break
					}
				}
			}else
				{
				##break foreach loop because network ViReference was already found on other ESX
				break
				}
		}else
			{
			##break foreach loop because network ViReference was already found on other cluster
			break
			}
	}
	write-host " "
	write-host "###############################################################" -foregroundcolor green -backgroundcolor black
	write-host "Destination network: $destination" -foregroundcolor red -backgroundcolor DarkYellow
	write-host "###############################################################" -foregroundcolor green -backgroundcolor black
	$dstNet0 = Get-VBRViServerNetworkInfo -Server "$targetesx" | Where { $_.Type -eq "ViDVS" -and $_.SwitchName -eq "$targetvds" -and $_.NetworkName -eq $destination }
	if ( $dstNet0 )
			{
			$ViReference = $dstNet0.ViReference
			$dstarray += $dstNet0
			write-host "   found network: $destination on $targetesx" -foregroundcolor green -backgroundcolor black
			write-host "         ViReference: $ViReference" -foregroundcolor green -backgroundcolor black
			write-host " "
			}else
			{
			write-host "   network: $destination not found on $targetesx" -foregroundcolor green -backgroundcolor black
			}
}


#update VEEAM Replica job with array of source and array of dest. networks
write-host "Updating Replica Job $JobName " -foregroundcolor yellow -backgroundcolor black
Set-VBRViReplicaJob -Job $job -EnableNetworkMapping -SourceNetwork $srcarray -TargetNetwork $dstarray
jmke79
Novice
Posts: 9
Liked: 3 times
Joined: Aug 13, 2020 1:17 pm
Full Name: JM
Contact:

Re: Scripting Replication job network mapping

Post by jmke79 »

I realized that instead of cycling each time through the ESX hosts, it would be better to read all networks in ONCE and put them in searchable array; and then loop through that array, that would be so much faster.
also if it doesn't find a src/dst network; network mappings will not be correct and/or last replica job update won't run, as it expects the src/dst arrays to be identical

so your CSV file needs to be 100% correct.

#to improve :)


if your machine does not have internet connectivity and it takes an eternity to load the vmware powershell modules:

- Open "Internet Options" in the Control Panel or Internet Explorer
- Click the "Connections" tab
- Set proxy to 127.0.0.1
- Click the "Advanced" tab
- Scroll down to "Security"
- UNcheck "Check for publisher's certificate revocation"
- UNcheck "Check for server certificate revocation"
- Click OK
oleg.feoktistov
Veeam Software
Posts: 2010
Liked: 670 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Scripting Replication job network mapping

Post by oleg.feoktistov » 1 person likes this post

Hi @jmke79. Thanks for sharing!
smarivoet
VeeaMVP
Posts: 136
Liked: 14 times
Joined: Jul 30, 2013 6:15 am
Full Name: stijn marivoet
Contact:

Re: Scripting Replication job network mapping

Post by smarivoet » 1 person likes this post

eziemann wrote: Mar 12, 2020 5:21 pm I wanted to share a script to update the network mappings associated in Replication jobs.
This is for VMware replications from a distributed vSwitch to a standard vSwitch.
...
Hi J. ,

great that you followed the advise of sharing!
(I'll make sure Laurent will do the required for you :wink: )

And as suspected the result is clear: based on the comments it is well appreciated! sharing is caring :mrgreen:
Stijn Marivoet
www.veeamclick.be
Territory and Enterprise SE, VeeaMVP
jmke79
Novice
Posts: 9
Liked: 3 times
Joined: Aug 13, 2020 1:17 pm
Full Name: JM
Contact:

Re: Scripting Replication job network mapping

Post by jmke79 »

some areas of improvement in that script; as it's not the fastest in its current form :)
but it does work, mapped 600 networks in a few hours now to different replica jobs

manually it would take a week and drive me GUI crazy :p
johan.h
Veeam Software
Posts: 723
Liked: 185 times
Joined: Jun 05, 2013 9:45 am
Full Name: Johan Huttenga
Contact:

Re: Scripting Replication job network mapping

Post by johan.h » 1 person likes this post

It seems useful in its current form :). If you like, you could use Get-VBRViServerNetworkInfo -Server "$targetesx" outside of the $csv | for-each loop and cache that information, meaning this would be called only once, and make the script faster.
Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests