We started using Distributed Virtual Switches (DVS) with VMware (vSphere). The switches configure (and port groups) can be exported and used as a backup. The following script is a modified version of one posted in the links listed below.
PS: As of this release, there is a bug where the path to the modules regarding PowerCLI isn’t automatically added to the Machine Level environment variable PSModulePath. I manually added mine to the utility machine running the script
Adjustments introduces in the script:
- Keeps around 30 days of configuration files on disk for backups
- Process multiple vCenters in single script
- Externalizes configuration into an XML file
Assumptions
- Using PowerCLI 6.0 and modules (in reference links)
- Using vCenter 5.5 and above
- Using Windows Task Scheduler (in the connect-viserver I use –force to inherit the permissions of the Windows Task Scheduler ID)
- The Windows Task scheduler account has “Logon As Batch Job” permissions.
- E:ScriptsBackupDVS folder is present.
#Reference links (original script came from bolded link)
#http://vcdx56.com/2013/10/29/backup-vsphere-vnetwork-distributed-switches-using-powercli/
#https://communities.vmware.com/thread/512171
#https://technet.microsoft.com/en-us/library/Ee692801.aspx
#https://blogs.vmware.com/PowerCLI/2015/03/powercli-6-0-introducing-powercli-modules.html
#https://blogs.vmware.com/PowerCLI/2015/03/powercli-6-0-r1-now-generally-available.html
PowerShell script (backupdvs.ps1)
param
(
[String] $ConfigurationFile = $(throw “Please specify the configuration file for the Content move.`r`nExample:`r`n`tGet-MachineLookup.ps1 -ConfigurationFile `”E:DirectoryChangeThisPath.xml`””)
)
Import-Module VMware.VimAutomation.Vds
switch (Test-Path $ConfigurationFile)
{
True {Write-Host “Using $ConfigurationFile For Script Variables”
$Properties = [xml](Get-Content $ConfigurationFile)
}
False {Write-Host “$ConfigurationFile Not Found For Script Variables – Quitting”
Exit
}
}
$vCenters=$Properties.Configuration.Properties.vCenterList
$vCenterList = $vCenters.Split(“;”)
$BackupFolder = $Properties.Configuration.Properties.BackupFolder
foreach($vCenter in $vCenterList)
{
$date=get-date -uformat %d
$BackupPath=”$($BackupFolder)$($Date)$($vCenter)”
New-item -Type Directory -Path $BackupPath -force
connect-viserver $vcenter -force
$switches=get-vdswitch
foreach ($switch in $switches)
{
#
# Backup each vNetwork Distributed Switch not including the port groups
export-vdswitch $switch -Withoutportgroups -Description “Backup of $switch without port groups” -Destination “$($BackupPath)$switch.without_portgroups.zip” -force
#
# Backup each vNetwork Distributed Switch including the port groups
export-vdswitch $switch -Description “Backup of $switch with port groups” -Destination “$($BackupPath)$switch.with_portgroups.zip” -force
#
# Backup each port group individually
get-vdswitch $switch | Get-VDPortgroup | foreach { export-vdportgroup -vdportgroup $_ -Description “Backup of port group $($_.name)” -destination “$($BackupPath)$($_.name).portgroup.zip” -force}
}
}
External Configuration file (backupdvs.xml)
<?xml version=”1.0″ encoding=”UTF-8″?>
<Configuration>
<Properties>
<vCenterList>vCenter1;vCenter2</vCenterList>
<BackupFolder>E:ScriptsBackupDVS</BackupFolder>
<Output>Backup-VDSwitch</Output>
<OutputErrors>Backup-VDSwitchErrors</OutputErrors>
</Properties>
</Configuration>