Aggiungere dv portgroup via script ad un distribuited virtual switch

Posted on 4 febbraio 2011 di

0


Può capitare che avendo un elenco di vlan da importare su una infrastruttura virtuale, lo si debba fare non su normali vSwitch degli esx ma bensì su un distribuited virtual switch (Enterprise Plus). La cosa non è quindi così semplice perchè non esistono comandi per il dvSwitch così come per i vSwitch da vmware POWERCLI.
Il Vostro script powershell sarà così fatto:


################### Puntamento al vcenter di interesse ###########################
Connect-VIServer Vcenter01.xxx.dom
################### Funzione Get-dvSwitch (grazie a LUCD.info) ###########################
function Get-dvSwitch{
param([parameter(Position = 0, Mandatory = $true)][string]$DatacenterName,
[parameter(Position = 1, Mandatory = $true)][string]$dvSwitchName)
$dcNetFolder = Get-View (Get-Datacenter $DatacenterName | Get-View).NetworkFolder
$found = $null
foreach($net in $dcNetFolder.ChildEntity){
if($net.Type -eq "VmwareDistributedVirtualSwitch"){
$temp = Get-View $net
if($temp.Name -eq $dvSwitchName){
$found = $temp
}
}
}
$found
}
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
}
}
############ Variabili da compilare per identificazione ############
$datacenterName = "DATACENTER ESX"
$dvSwitchName = "dvSwitch0"
$dvPgNumPorts = 32
$dvSw = Get-dvSwitch -DatacenterName $datacenterName -dvSwitchName $dvSwitchName
############ Portugroup senza Vlan ##########
# No VLAN
#$dvSwPg = New-dvSwPortgroup $dvSw "dvPg1"
#$dvSwPg = Get-dvSwitch $datacenterName $dvSwitchName | New-dvSwPortgroup -PgName "dvPg2"
#$dvSwPg = New-dvSwPortgroup $dvSw "dvPg3" -PgNumberPorts $dvPgNumPorts
#$dvSwPg = New-dvSwPortgroup $dvSw "dvPg4" -PgBinding "earlyBinding" -PgVLANType "none"
#$dvSwPg = New-dvSwPortgroup $dvSw "dvPg5" -PgBinding "ephemeral" -PgVLANType "none"
#$dvSwPg = New-dvSwPortgroup $dvSw "dvPg6" -PgBinding "lateBinding" -PgVLANType "none"
########### Portugroup con Vlan ##########
# VLAN
$dvSwPg = New-dvSwPortgroup $dvSw "dvPg-test-marco" -PgNumberPorts $dvPgNumPorts -PgVLANType "VLAN" -PgVLANId 3000
########### Portugroup con Vlan Trunkate ##########
# VLAN Trunking
#$dvSwPg = New-dvSwPortgroup $dvSw "dvPg8" -PgNumberPorts $dvPgNumPorts -PgVLANType "VLAN Trunking" -PgVLANId (2023..2027)
#$dvSwPg = New-dvSwPortgroup $dvSw "dvPg9" -PgNumberPorts $dvPgNumPorts -PgVLANType "VLAN Trunking" -PgVLANId 2023,2025,2027
########### Portugroup con Vlan private ##########
# PVLAN
#$dvSwPg = New-dvSwPortgroup $dvSw "dvPg10" -PgNumberPorts $dvPgNumPorts -PgBinding "lateBinding" -PgVLANType "Private VLAN" -PgVLANId 2031
########### Portugroup con settaggio Mac fisso ##########
# MAC addr changes
#$dvSwPg = New-dvSwPortgroup $dvSw "dvPg11" -SecPolMacChanges:$false
########### Portugroup con teaming attivo attivo ##########
# Teaming
#$dvSwPg = New-dvSwPortgroup $dvSw "dvPg12" -TeamingPolicy "loadbalance_srcid" -TeamingActiveUplink "dvUplink1","dvUplink2"

Annunci