VMware Cloud Community
nanodk
Contributor
Contributor
Jump to solution

Backup of dvSwitch

Hi All,

Im new to powercli, however i would like to create a script that do a of my dvSwitchs,

I would also like to create a script that can import the backup of the dvSwitch.

I would prefer a dump of the dvSwitches into a csv file per dvswitch.

In my setup, im only interested in the portgroup name and the vlan id

Howto do that?

Regards Kim

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try something like this.

It will handle regular VLanIds, VLanId ranges and pvLans

&{foreach($dvSw in Get-VirtualSwitch -Distributed){
  Get-VirtualPortGroup -VirtualSwitch $dvSw | 
  Select @{N="dvSw";E={$dvSw.Name}},Name,
  @{N="VLanId";E={     if($_.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchPvlanSpec]){       $_.ExtensionData.Config.DefaultPortConfig.Vlan.PvLanId
    }    
elseif($_.ExtensionData.Config.DefaultPortConfig.Vlan.VLanId -is [VMware.Vim.NumericRange[]]){       [string]::Join(',',($_.ExtensionData.Config.DefaultPortConfig.Vlan.VLanId | %{         [string]$_.Start + "-" + [string]$_.End
      }))     }     else{       $_.ExtensionData.Config.DefaultPortConfig.Vlan.VLanId
    }   }} }} | Export-Csv C:\report.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
13 Replies
LucD
Leadership
Leadership
Jump to solution

Try something like this.

It will handle regular VLanIds, VLanId ranges and pvLans

&{foreach($dvSw in Get-VirtualSwitch -Distributed){
  Get-VirtualPortGroup -VirtualSwitch $dvSw | 
  Select @{N="dvSw";E={$dvSw.Name}},Name,
  @{N="VLanId";E={     if($_.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchPvlanSpec]){       $_.ExtensionData.Config.DefaultPortConfig.Vlan.PvLanId
    }    
elseif($_.ExtensionData.Config.DefaultPortConfig.Vlan.VLanId -is [VMware.Vim.NumericRange[]]){       [string]::Join(',',($_.ExtensionData.Config.DefaultPortConfig.Vlan.VLanId | %{         [string]$_.Start + "-" + [string]$_.End
      }))     }     else{       $_.ExtensionData.Config.DefaultPortConfig.Vlan.VLanId
    }   }} }} | Export-Csv C:\report.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
nanodk
Contributor
Contributor
Jump to solution

Thanks Lucd,

I just realised that I also need number of ports per portgroup and numbers of ports in use per portgroup

regards kim

0 Kudos
LucD
Leadership
Leadership
Jump to solution

No problem, try this

&{foreach($dvSw in Get-VirtualSwitch -Distributed){
  Get-VirtualPortGroup -VirtualSwitch $dvSw | 
  Select @{N="dvSw";E={$dvSw.Name}},Name,NumPorts,
  @{N="Ports used";E={@($_.ExtensionData.Vm).Count}},
  @{N="VLanId";E={
   
if($_.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchPvlanSpec]){       $_.ExtensionData.Config.DefaultPortConfig.Vlan.PvLanId
    }    
elseif($_.ExtensionData.Config.DefaultPortConfig.Vlan.VLanId -is [VMware.Vim.NumericRange[]]){       [string]::Join(',',($_.ExtensionData.Config.DefaultPortConfig.Vlan.VLanId | %{         [string]$_.Start + "-" + [string]$_.End
      }))     }     else{       $_.ExtensionData.Config.DefaultPortConfig.Vlan.VLanId
    }   }} }} | Export-Csv C:\report.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

nanodk
Contributor
Contributor
Jump to solution

Hi Lucd,

I have created a script to restore the dvs based on your http://www.lucd.info/2009/10/12/dvswitch-scripting-part-2-dvportgroup/

That work fine, however, if i want to import the csv file again with something simple like

dvSw;Name;NumPorts;Ports used;VLanId

------------------------------------

Prod-Switch;Anne Bogholder Net;4;1;200

Prod-Switch;Apokteren Net;4;1;229

$lines = Import-Csv c:\report.csv
foreach ($line in $lines) {
    echo $line.Name
}

That echos absolutly nothing. Whats wrong?

regard kim

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Judging from the CSV file extract your regional settings are not default En-US.

That's what the UseCulture parameter takes care of.

Try it like this

$lines = Import-Csv c:\report.csv -UseCulture 
foreach ($line in $lines) {     $line.Name
}

Note that you do not have to use "echo" to display something on the console in PowerShell.

Just place the value you want to see on the pipeline.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

nanodk
Contributor
Contributor
Jump to solution

Hi Lucd,

It seems like, when I create a ned port group, then all uplink adaptors is listed under "unused uplinks"

How do I put e.g. dvUplink1 and dvUplink2 in "Active Uplinks"

Regards Kim

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That happens when you assign a VMhost to the dvSwitch


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
nanodk
Contributor
Contributor
Jump to solution

Net really.

I just created a new vcenter version 5.01b.

I have 2 hosts in it. If i create a new port group from my script, then both dvuplinks is listed as unused.

If I create a vm and assign it the new port group, the it will not work, since the dvuplinks is still listed as unused.

any ideas?

\\\Kim

0 Kudos
LucD
Leadership
Leadership
Jump to solution

After you create the dvSwitch you need to assign the ESXi hosts to it.

In the vSphere client go to the ESXi server, Configuration, Networking, vSphere Distributed Switch and click Manage Physical Adapters.

Thta's where you link a physcial NIC on the ESXi server to an uplink on a dvSwitch


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
nanodk
Contributor
Contributor
Jump to solution

Yes, i know, that i have done.

The problem is, when i create a new portgroup with the script, the the dvuplinks are listed as unused.

If i do the same with the vsphere client, then the dvuplinks are listed as Active Uplinks.

\\\ Kim

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you show the script you used to include the portgroup ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
nanodk
Contributor
Contributor
Jump to solution

function Get-dvSwitch{
  param([parameter(Position = 0, Mandatory = $true)][string]$DatacenterName,
  [parameter(Position = 1, Mandatory = $true)][string]$dvSwitchName)
  function Get-dvSwitchInThisLocation{
    param(
    [parameter(Position = 0, Mandatory = $true)]
    [VMware.Vim.ManagedObjectReference]$LocationMoRef,
    [parameter(Position = 1, Mandatory = $true)][string]$dvSwitchName
    )
    $location = Get-View $LocationMoRef
    foreach($child in $location.ChildEntity){
      if($child.Type -eq "Folder"){
        Get-dvSwitchInThisLocation $child $dvSwitchName
      }
      elseif($child.Type -eq "VmwareDistributedVirtualSwitch"){
        $temp = Get-View $child
        if($temp.Name -eq $dvSwitchName){
          $temp
        }
      }
    }
  }
  Get-dvSwitchInThisLocation (Get-Datacenter $DatacenterName).Extensiondata.NetworkFolder $dvSwitchName
}
function Get-VLANRanges{
  param ([int[]]$ids)
  $return = @()
  $nr = 0
  $start = $ids[$nr]
  $end = -1
  while($nr -lt ($ids.Count)){
    if(($ids[$nr + 1]-$ids[$nr]) -gt 1){
      $end = $ids[$nr]
      $nrange = New-Object VMware.Vim.NumericRange
      $nrange.start = $Start
      $nrange.end = $end
      $return += $nrange
      $start = $ids[$nr + 1]
      $end = -1
    }
    $nr++
  }
  if($end -lt 0){
    $nrange = New-Object VMware.Vim.NumericRange
    $nrange.start = $Start
    $nrange.end = $ids[-1]
    $return += $nrange
  }
  $return
}
function New-dvSwPortgroup{
  param([parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true)][VMware.Vim.VmwareDistributedVirtualSwitch]$dvSw,
  [parameter(Position = 1, Mandatory = $true)][string]$PgName,
  [int]$PgNumberPorts = 64,
  [string]$PgBinding = "earlyBinding",
  [string]$PgVLANType = "none",
  [int[]]$PgVLANId,
  [switch]$SecPolPromiciousMode = $false,
  [switch]$SecPolMacChanges = $true,
  [switch]$SecPolForgedTransmits = $true,
  [switch]$TeamingCheckDuplex = $false,
  [switch]$TeamingCheckErrorPercent = $false,
  [string]$TeamingCheckSpeed = $false,
  [switch]$TeamingFullDuplex = $true,
  [int]$TeamingPercentage,
  [int]$TeamingSpeed,
  [string]$TeamingPolicy = "loadbalance_srcid",
  [switch]$TeamingNotifySwitches = $true,
  [switch]$TeamingRollingOrder = $false,
  [switch]$TeamingReversePolicy = $true,
  [string[]]$TeamingActiveUplink,
  [string[]]$TeamingStandbyUplink
  )
  process{
    $teamingPolicies = "loadbalance_ip",
    "loadbalance_srcmac",
    "loadbalance_srcid",
    "failover_explicit",
    "loadbalance_loadbased"
    $spec = New-Object VMware.Vim.DVPortgroupConfigSpec
    $spec.Name = $PgName
    $spec.Type = $PgBinding
    $spec.numPorts = $PgNumberPorts
    $spec.defaultPortConfig = New-Object VMware.Vim.VMwareDVSPortSetting
    switch($PgVLANType.ToLower()){
      "vlan" {
        $spec.defaultPortConfig.VLAN = New-Object VMware.Vim.VmwareDistributedVirtualSwitchVlanIdSpec
        $spec.defaultPortConfig.VLAN.vlanId = $PgVLANId[0]
      }
      "vlan trunking" {
        $spec.defaultPortConfig.VLAN = New-Object VMware.Vim.VmwareDistributedVirtualSwitchTrunkVlanSpec
        $spec.defaultPortConfig.VLAN.vlanId = Get-VLANRanges $PgVLANId
      }
      "private vlan" {
        $spec.defaultPortConfig.VLAN = New-Object VMware.Vim.VmwareDistributedVirtualSwitchPvlanSpec
        $spec.defaultPortConfig.VLAN.pvlanId = $PgVLANId[0]
      }
      Default{}
    }
    $spec.defaultPortConfig.securityPolicy = New-Object VMware.Vim.DVSSecurityPolicy
    $spec.defaultPortConfig.securityPolicy.allowPromiscuous = New-Object VMware.Vim.BoolPolicy
    $spec.defaultPortConfig.securityPolicy.allowPromiscuous.Value = $SecPolPromiciousMode
    $spec.defaultPortConfig.securityPolicy.forgedTransmits = New-Object VMware.Vim.BoolPolicy
    $spec.defaultPortConfig.securityPolicy.forgedTransmits.Value = $SecPolForgedTransmits
    $spec.defaultPortConfig.securityPolicy.macChanges = New-Object VMware.Vim.BoolPolicy
    $spec.defaultPortConfig.securityPolicy.macChanges.Value = $SecPolMacChanges
    $spec.defaultPortConfig.uplinkTeamingPolicy = New-Object VMware.Vim.VmwareUplinkPortTeamingPolicy
    $spec.defaultPortConfig.uplinkTeamingPolicy.failureCriteria = New-Object VMware.Vim.DVSFailureCriteria
    if($TeamingCheckDuplex){
      $spec.defaultPortConfig.uplinkTeamingPolicy.failureCriteria.checkDuplex = $TeamingCheckDuplex
      $spec.defaultPortConfig.uplinkTeamingPolicy.failureCriteria.fullDuplex = $TeamingFullDuplex
    }
    if($TeamingCheckErrorPercent){
      $spec.defaultPortConfig.uplinkTeamingPolicy.failureCriteria.checkErrorPercent = $TeamingCheckErrorPercent
      $spec.defaultPortConfig.uplinkTeamingPolicy.failureCriteria.percentage = $TeamingPercentage
    }
    if("exact","minimum" -contains $TeamingCheckSpeed){
      $spec.defaultPortConfig.uplinkTeamingPolicy.failureCriteria.checkSpeed = $TeamingCheckSpeed
      $spec.defaultPortConfig.uplinkTeamingPolicy.failureCriteria.speed = $TeamingSpeed
    }
    $spec.defaultPortConfig.uplinkTeamingPolicy.notifySwitches = New-Object VMware.Vim.BoolPolicy
    $spec.defaultPortConfig.uplinkTeamingPolicy.notifySwitches.Value = $TeamingNotifySwitches
    if($teamingPolicies -contains $TeamingPolicy){
      $spec.defaultPortConfig.uplinkTeamingPolicy.policy = New-Object VMware.Vim.StringPolicy
      $spec.defaultPortConfig.uplinkTeamingPolicy.policy.Value = $TeamingPolicy
    }
    $spec.defaultPortConfig.uplinkTeamingPolicy.reversePolicy = New-Object VMware.Vim.BoolPolicy
    $spec.defaultPortConfig.uplinkTeamingPolicy.reversePolicy.Value = $TeamingReversePolicy
    $spec.defaultPortConfig.uplinkTeamingPolicy.rollingOrder = New-Object VMware.Vim.BoolPolicy
    $spec.defaultPortConfig.uplinkTeamingPolicy.rollingOrder.Value = $TeamingRollingOrder
    $spec.defaultPortConfig.uplinkTeamingPolicy.uplinkPortOrder = New-Object VMware.Vim.VMwareUplinkPortOrderPolicy
    $spec.defaultPortConfig.uplinkTeamingPolicy.uplinkPortOrder.activeUplinkPort = $TeamingActiveUplink
    $spec.defaultPortConfig.uplinkTeamingPolicy.uplinkPortOrder.standbyUplinkPort = $TeamingStandbyUplink
    $taskMoRef = $dvSw.AddDVPortgroup_Task($spec)
    $task = Get-View $taskMoRef
    while("running","queued" -contains $task.Info.State){
      $task.UpdateViewData("Info")
    }
    $task.Info.Result
  }
}
Connect-VIServer <ip> -User administrator -Password <password>
$DatacenterName = "Hoerskaetten";
$dvSwitchName = "Prod-dvSwitch";
$dvSw = Get-dvSwitch -DatacenterName $datacenterName -dvSwitchName $dvSwitchName
$PGs = Import-Csv c:\report.csv -UseCulture
foreach ($PG in $PGs){
$dvSwPg = New-dvSwPortgroup $dvSw $PG.Name -PgNumberPorts $PG.NumPorts -PgVLANType "VLAN" -PgVLANId $PG.VLanId
}
echo "done"
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You seem to be missing the part where you connect the ESXi hosts to the dvSwitch.

See for example the Add-dvSwHost function in my dvSwitch scripting – Part 1 – Creation post.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos