Automate backing up DVS using Powercli and POwerShell

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


  • 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:\Scripts\BackupDVS folder is present.

#Reference links (original script came from bolded link)


PowerShell script (backupdvs.ps1)

    [String] $ConfigurationFile = $(throw “Please specify the configuration file for the Content move.`r`nExample:`r`n`tGet-MachineLookup.ps1 -ConfigurationFile `”E:\Directory\ChangeThisPath.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”
$vCenterList = $vCenters.Split(“;”)
$BackupFolder = $Properties.Configuration.Properties.BackupFolder

foreach($vCenter in $vCenterList)
    $date=get-date -uformat %d

    New-item -Type Directory -Path $BackupPath -force   

    connect-viserver $vcenter -force
    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)\$” -force
        # Backup each vNetwork Distributed Switch including the port groups
        export-vdswitch $switch -Description “Backup of $switch with port groups” -Destination “$($BackupPath)\$” -force
        # Backup each port group individually
        get-vdswitch $switch | Get-VDPortgroup | foreach { export-vdportgroup -vdportgroup $_ -Description “Backup of port group $($” -destination “$($BackupPath)\$($”  -force}

External Configuration file (backupdvs.xml)

<?xml version=”1.0″ encoding=”UTF-8″?>