dvSwitch scripting – Part 13 – Export/Restore Config

One of the exciting new dvSwitch features in vSphere 5.1 is the ability to export and restore a dvSwitch configuration. This new feature is only available through the vSphere Web Client.

On Hypervisor.fr, a blog you should have in your RSS reader, there were already posts on how to configure the dvSwitch healthcheck and how to do a dvSwitch configuration rollback from PowerCLI. But till now, as far as I know, there were no functions to provide the dvSwitch export/restore functionality.

dvSw-export-import

Time to make this useful functionality available for the “PowerCLI automation crowd” 😉

The Script

Annotations

Line 25,37-39: My cheap way of emulating the OBN principle.

Line 40-45: If the IncludePortgroups switch is $true, the configuration of the distributed virtual portgroups on the dvSwitch will also be exported.

Line 53: The object that is returned by the call to the DVSManagerExportEntity method is written to a file with the Export-Clixml cmdlet.

Line 90-95: The reason why the function copies the properties one-by-one instead of doing the complete object in one go is due to serialisation-deserialisation process that you get with the Export- and Import-Clixml cmdlets. The imported objects all have a type that has a Deserialized suffix in front. There are ways to avoid this, but the is a simple and readable solution.

Sample Usage

The use of the functions is quite simple.

By default this will save the configuration of the dvSwitch, but not the portgroups on the dvSwitch.
To include the portgroups, you will have to use the IncludePortgroups switch.

You can also use the function in a pipeline like this

As you can read in the Annotations, the function uses the PowerShell Export-Clixml cmdlet to write the configuration to a file.
Note that this format is NOT COMPATIBLE with the file format used in the vSphere Web Client.

If you want to emulate that format, you have a look at William Lam‘s post Automate Backups of VDS & Distributed Portgroup Configurations in vSphere 5.1.

To restore the configuration of a dvSwitch, and optionally the connected portgroups, is quite similar. A restore can be executed like this.

Notice that the IncludePortgroups switch defines if you want to restore the configuration of the portgroups on the dvSwitch as well.

The 2 methods that are used by these functions have several other possibilities. You can for example also use these files to Import a dvSwitch, but that will be for a later post 😉

Enjoy !

2 Comments

    mb

    Same problem as previous commenter.

    Could we get a updated working version as the web client export/import does not have the ability to actually recreate the entire dvswitch with the same key id, which means it’s useless when trying to restore on a different vcenter server than the export was used on.

    Marc

    hi Luc.

    I try to import export the config from a DVSwitch in one vCenter to another vcenter.

    The Export seems correct.
    When I try to import in the other vcenter i lose access to the vcenter (the vcenter client stops) with the command:

    Restore-dvSwConfig -Path ConfigExported.xml -IncludePortgroups

    and I get:

    Exception calling “DVSManagerImportEntity” with “2” argument(s): “Unable to connect to the remote server”
    At line:38 char:5
    + $dvSwMgr.DVSManagerImportEntity($dvswImport,”applyToEntitySpecified”) | Out- …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WebException

    And nothing changed when I log back.

    any idea?

    I tried with no Dvswitch in the new vc. I tried with a DVswitch of the same name and same DVSwitch-uplink-number…

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*

This site uses Akismet to reduce spam. Learn how your comment data is processed.