Skip to content

Instantly share code, notes, and snippets.

@jcpowermac
Created December 11, 2013 17:44
Show Gist options
  • Save jcpowermac/7915035 to your computer and use it in GitHub Desktop.
Save jcpowermac/7915035 to your computer and use it in GitHub Desktop.
vSphere 5.0 DVS port issue
function Set-PortIdAdvanced {
Param([object] $VmView, [object] $PortId )
$netdev = ($VmView.Config.Hardware.Device | Where {$_.MacAddress})
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.deviceChange = New-Object VMware.Vim.VirtualDeviceConfigSpec[] (1)
$spec.deviceChange[0] = New-Object VMware.Vim.VirtualDeviceConfigSpec
$spec.deviceChange[0].operation = "edit"
$spec.deviceChange[0].device = New-Object VMware.Vim.VirtualVmxnet3
$spec.deviceChange[0].device.key = $netdev.Key;
$spec.deviceChange[0].device.deviceInfo = New-Object VMware.Vim.Description
$spec.deviceChange[0].device.deviceInfo.label = $netdev.DeviceInfo.Label;
$spec.deviceChange[0].device.deviceInfo.summary = "vm.device.VirtualVmxnet3.DistributedVirtualPortBackingInfo.summary"
$spec.deviceChange[0].device.backing = New-Object VMware.Vim.VirtualEthernetCardDistributedVirtualPortBackingInfo
$spec.deviceChange[0].device.backing.port = New-Object VMware.Vim.DistributedVirtualSwitchPortConnection
$spec.deviceChange[0].device.backing.port.switchUuid = $netdev.Backing.Port.SwitchUuid
$spec.deviceChange[0].device.backing.port.portgroupKey = $netdev.Backing.Port.PortgroupKey
$spec.deviceChange[0].device.backing.port.portKey = $PortId
$spec.deviceChange[0].device.connectable = New-Object VMware.Vim.VirtualDeviceConnectInfo
$spec.deviceChange[0].device.connectable.startConnected = $true
$spec.deviceChange[0].device.connectable.allowGuestControl = $true
$spec.deviceChange[0].device.connectable.connected = $true
$spec.deviceChange[0].device.connectable.status = "ok"
$spec.deviceChange[0].device.controllerKey = $netdev.ControllerKey;
$spec.deviceChange[0].device.unitNumber = $netdev.UnitNumber
$spec.deviceChange[0].device.addressType = "assigned"
$spec.deviceChange[0].device.macAddress = $netdev.MacAddress;
$spec.deviceChange[0].device.wakeOnLanEnabled = $true
$VmView.ReconfigVM_Task($spec)
$vmname = $VmView.Name;
if(!$?) {
Write-Host -ForegroundColor Red "Change the port id failed, VMotion $vmname to another host"
}
}
function Check-DvsIssue
{
Param(
[object] $VmView,
[object] $VMHost,
[string] $User,
[string] $Password
);
$netdev = ($VmView.Config.Hardware.Device | Where {$_.MacAddress})
$PortId = $netdev.Backing.Port.PortKey;
$hostdvports = New-Object System.Collections.ArrayList
$hostdvports.Clear();
$viserver = Connect-VIServer -Server $VMHost -User $User -Password $Password
(Get-View -Id (Get-VirtualSwitch -Server $viserver).Id).FetchDVPorts($null)| % {
$hostdvports.Add($_.Key);
}
Disconnect-VIServer -Server $viserver -Confirm:$false | Out-Null
return $hostdvports.Contains($PortId);
}
function Get-UnusedPort
{
Param(
[string] $DvsName,
[string] $PortGroupName
);
$emptyports = New-Object System.Collections.ArrayList
$emptyports.Clear();
Write-Host -ForegroundColor Green "Getting Available Port Ids"
$pgkey = (Get-VirtualPortGroup -Name $PortGroupName).Key
$vsid = (Get-VirtualSwitch -Name $DvsName).Id
$dvs = Get-View -Id $vsid
$dvports = $dvs.FetchDVPorts($null);
Write-Host -ForegroundColor Green "Adding to Array"
$dvports | % {
if( $_.portgroupKey -eq $pgkey ) {
if( $_.connectee -eq $null ) {
$emptyports.Add($_.key) | Out-Null;
}
}
}
$emptyports.Sort();
$location = Get-Random -Minimum 0 -Maximum ($emptyports.Count - 1)
if( $emptyports.Count -gt 0 ) {
return $location
} else { return -1; }
}
function Set-FixDvsBug {
Param(
[string] $DvsName,
[string] $PortGroupName,
[object] $VM
);
Set-PowerCLIConfiguration -DefaultVIServerMode multiple -Confirm:$false | out-Null
$i = 0;
$vms = @();
if($VM -eq $null ) {
Write-Host -ForegroundColor Green "Getting VMs"
$vms = get-vm
}
else {
$vms += $VM
}
$vms | % {
Write-Progress -activity "Checking Virtual Machines" -status "Percent: " -percentComplete (($i / $vms.length) * 100)
$na = $_ | Get-NetworkAdapter;
if( $_.PowerState -eq "PoweredOn" ) {
if( !$na.ConnectionState.Connected ) {
Write-Host -ForegroundColor red "Virtual Machine $_ is not connected to the port group"
$na | Set-NetworkAdapter -Connected:$true -Confirm:$false -ErrorAction SilentlyContinue | Out-Null
if(!$?) {
$vm = Get-View -Id $_.Id -Property Name,Summary,Config.Hardware.Device
if(!(Check-DvsIssue -VmView $vm -VMHost $_.Host -User root -Password Phx@dm1n ) ) {
Write-Host -ForegroundColor red "Set-NetworkAdapter -Connected:`$true failed, changing port id on $_";
$newportid = Get-UnusedPort -DvsName $DvsName -PortGroupName $PortGroupName;
if($newportid -gt 0 ){
Write-Host "Using Port Id: $newportid";
Set-PortIdAdvanced -VmView $vm -PortId $newportid
}
}
}
}
}
$i++;
}
Set-PowerCLIConfiguration -DefaultVIServerMode single -Confirm:$false | Out-Null
}
Set-FixDvsBug -DvsName "foo" -PortGroupName "foo-pg";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment