<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>5.1 Archives - LucD notes</title>
	<atom:link href="https://www.lucd.info/tag/5-1/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.lucd.info/tag/5-1/</link>
	<description>My PowerShell ramblings</description>
	<lastBuildDate>Thu, 31 Jan 2013 22:17:07 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://www.lucd.info/wp-content/uploads/2018/12/cropped-120px-Tibetan_Dharmacakra-32x32.png</url>
	<title>5.1 Archives - LucD notes</title>
	<link>https://www.lucd.info/tag/5-1/</link>
	<width>32</width>
	<height>32</height>
</image> 
<atom:link rel="hub" href="https://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="https://pubsubhubbub.superfeedr.com"/><atom:link rel="hub" href="https://websubhub.com/hub"/>	<item>
		<title>dvSwitch scripting &#8211; Part 13 &#8211; Export/Restore Config</title>
		<link>https://www.lucd.info/2013/01/31/dvswitch-scripting-part-13-export-restore-config/</link>
					<comments>https://www.lucd.info/2013/01/31/dvswitch-scripting-part-13-export-restore-config/#comments</comments>
		
		<dc:creator><![CDATA[LucD]]></dc:creator>
		<pubDate>Thu, 31 Jan 2013 22:12:37 +0000</pubDate>
				<category><![CDATA[config]]></category>
		<category><![CDATA[dvSwitch]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[vSphere]]></category>
		<category><![CDATA[5.1]]></category>
		<category><![CDATA[export]]></category>
		<category><![CDATA[restore]]></category>
		<guid isPermaLink="false">http://www.lucd.info/?p=4339</guid>

					<description><![CDATA[One of the exciting new dvSwitch features in vSphere 5.1 is the ability [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>One of the exciting new <strong>dvSwitch</strong> features in <strong>vSphere 5.1</strong> is the ability to <a href="https://kb.vmware.com/selfservice/microsites/search.do?language=en_US&amp;cmd=displayKC&amp;externalId=2034602" target="_blank">export and restore</a> a dvSwitch <strong>configuration</strong>. This new feature is only available through the <strong>vSphere Web Client</strong>.</p>
<p>On <a href="https://www.hypervisor.fr/" target="_blank">Hypervisor.fr</a>, a blog you should have in your RSS reader, there were already posts on how to configure the dvSwitch <a href="https://www.hypervisor.fr/?p=4229" target="_blank">healthcheck</a> and how to do a dvSwitch <a href="https://www.hypervisor.fr/?p=4240" target="_blank">configuration rollback</a> from PowerCLI. But till now, as far as I know, there were no functions to provide the dvSwitch <strong>export/restore</strong> functionality.</p>
<p><a href="https://www.lucd.info/2013/01/31/dvswitch-scripting-part-13-export-import-config/dvsw-export-import/" rel="attachment wp-att-4342"><img fetchpriority="high" decoding="async" class="alignnone  wp-image-4342" alt="dvSw-export-import" src="https://lucd.info/wp-content/uploads/2013/01/dvSw-export-import.png" width="336" height="341" srcset="https://www.lucd.info/wp-content/uploads/2013/01/dvSw-export-import.png 560w, https://www.lucd.info/wp-content/uploads/2013/01/dvSw-export-import-295x300.png 295w" sizes="(max-width: 336px) 100vw, 336px" /></a></p>
<p>Time to make this useful functionality available for the &#8220;<em>PowerCLI automation crowd</em>&#8221; <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p><span id="more-4339"></span></p>
<h2>The Script</h2>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">function Export-dvSwConfig {
&lt;#  
.SYNOPSIS  Export a dvSwitch configuration  
.DESCRIPTION The function will export the dvSwitch
  configuration(s) to a file. 
  This requires at least vSphere 5.1.
.NOTES  Author:  Luc Dekens  
.PARAMETER dvSw
  The dvSwitch(es) whose config you want to export.
.PARAMETER Path
  The path to a filename where the config should be
  written to.
.PARAMETER IncludePortgroups
  Switch that indicates if only the dvSwitch configuration
  or the dvSwitch and all portgroups should be exported
.EXAMPLE
  PS&gt; Export-dvSwConfig -Path C:\dvswconfig.xml -IncludePortgroups
.EXAMPLE
   PS&gt; $dvsw | Export-dvSwConfig -Path C:\file.xml
#&gt;

  param(
    [CmdletBinding()]
    [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
    [PSObject]$dvSw,
    [String]$Path,
    [Switch]$IncludePortgroups = $false
  )

  begin {
    $si = Get-View ServiceInstance
    $dvSwMgr = Get-View $si.Content.dvSwitchManager
    $selectionTab = @()
  }

  process{
    if($dvSw -is [System.String]){
      $dvSw = Get-VirtualSwitch -Distributed -Name $dvsw
    }
    if($IncludePortgroups){
      $selection = New-Object VMware.Vim.DVPortgroupSelection
      $selection.portgroupKey = Get-VirtualPortGroup -VirtualSwitch $dvsw | %{$_.Key}
      $selection.dvsUuid = $dvsw.ExtensionData.Uuid
      $selectionTab += $selection
    }
    $selection = New-Object VMware.Vim.DVSSelection
    $selection.dvsUuid = $dvsw.ExtensionData.Uuid
    $selectionTab += $selection
  }

  end{
    $dvswSaved = $dvSwMgr.DVSManagerExportEntity($selectionTab)
    $dvswSaved | Export-Clixml -Path $Path
  }
}

function Restore-dvSwConfig {
&lt;#  
.SYNOPSIS  Restores a dvSwitch configuration  
.DESCRIPTION The function will import the dvSwitch
  configuration(s) from a file. 
  This requires at least vSphere 5.1.
.NOTES  Author:  Luc Dekens  
.PARAMETER Path
  The path to a filename where the config is stored.
.PARAMETER IncludePortgroups
  Switch that indicates if only the dvSwitch configuration
  or the dvSwitch and all portgroups should be exported
.EXAMPLE
  PS&gt; Restore-dvSwConfig -Path C:\dvswconfig.xml
#&gt;

  param(
    [CmdletBinding()]
    [Parameter(Mandatory=$true)]
    [String]$Path,
    [Switch]$IncludePortgroups = $false
  )

  process {

    $si = Get-View ServiceInstance
    $dvSwMgr = Get-View $si.Content.dvSwitchManager

    $dvswImport = @()
    Import-Clixml -Path $Path | 
    where{($IncludePortgroups -and $_.EntityType -eq &quot;distributedVirtualPortgroup&quot;) -or 
        $_.EntityType -eq &quot;distributedVirtualSwitch&quot;} | %{
      $info = New-Object VMware.Vim.EntityBackupConfig
      $info.ConfigBlob = $_.ConfigBlob
      $info.ConfigVersion = $_.ConfigVersion
      $info.Container = $_.Container
      $info.EntityType = $_.EntityType
      $info.Key = $_.Key
      $info.Name = $_.Name
      $dvswImport += $info
    }
    $dvSwMgr.DVSManagerImportEntity($dvswImport,&quot;applyToEntitySpecified&quot;) | Out-Null
  }
}</pre><p></p>
<h4>Annotations</h4>
<p><strong>Line 25,37-39</strong>: My cheap way of emulating the <a href="https://www.vmware.com/support/developer/PowerCLI/PowerCLI51/html/about_obn.html" target="_blank">OBN</a> principle.</p>
<p><strong>Line 40-45</strong>: If the <strong>IncludePortgroups</strong> switch is $true, the configuration of the distributed virtual portgroups on the dvSwitch will also be exported.</p>
<p><strong>Line 53</strong>: The object that is returned by the call to the <a href="https://pubs.vmware.com/vsphere-51/topic/com.vmware.wssdk.apiref.doc/vim.dvs.DistributedVirtualSwitchManager.html#exportEntity" target="_blank">DVSManagerExportEntity</a> method is written to a file with the <a href="https://technet.microsoft.com/en-us/library/hh849916.aspx" target="_blank">Export-Clixml</a> cmdlet.</p>
<p><strong>Line 90-95</strong>: 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 <em>Deserialized</em> suffix in front. There are ways to avoid this, but the is a simple and readable solution.</p>
<h2>Sample Usage</h2>
<p>The use of the functions is quite simple.</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">$dvSw = Get-VirtualSwitch -Distributed -Name dvSw1
Export-dvSwConfig -dvSw $dvSw  -Path C:\dvSw1.xml</pre><p></p>
<p>By default this will save the configuration of the dvSwitch, but not the portgroups on the dvSwitch.<br />
To include the portgroups, you will have to use the IncludePortgroups switch.</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">$dvSw = Get-VirtualSwitch -Distributed -Name dvSw1
Export-dvSwConfig -dvSw $dvSw  -Path C:\dvSw1.xml -IncludePortgroups</pre><p></p>
<p>You can also use the function in a pipeline like this</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">Get-VirtualSwitch -Distributed -Name dvSw2 | Export-dvSwConfig -Path C:\dvSw2.xm2</pre><p></p>
<p>As you can read in the Annotations, the function uses the PowerShell Export-Clixml cmdlet to write the configuration to a file.<br />
Note that this format is <span style="background-color: #ffff00;">NOT COMPATIBLE</span> with the file format used in the <strong>vSphere Web Client</strong>.</p>
<p>If you want to emulate that format, you have a look at <a href="https://twitter.com/lamw" target="_blank">William Lam</a>&#8216;s post <a href="https://blogs.vmware.com/vsphere/2013/01/automate-backups-of-vds-distributed-portgroup-configurations-in-vsphere-5-1.html" target="_blank">Automate Backups of VDS &amp; Distributed Portgroup Configurations in vSphere 5.1.</a></p>
<p>To restore the configuration of a dvSwitch, and optionally the connected portgroups, is quite similar. A restore can be executed like this.</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">Restore-dvSwConfig -Path C:\dvsw1.xml -IncludePortgroups</pre><p></p>
<p>Notice that the <strong>IncludePortgroups</strong> switch defines if you want to restore the configuration of the portgroups on the dvSwitch as well.</p>
<p>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 <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p>Enjoy !</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.lucd.info/2013/01/31/dvswitch-scripting-part-13-export-restore-config/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>
