wallabyfan2
Contributor
Contributor

Add ESX Host to DVS

I am attempting to add a ESXi 4.1 Host to a Distributed Virtual Switch, however I am not getting far.

I get the following error after tyring to run the below script:

Cannot overwrite variable Host because it is read-only or constant.

At line:1 char:1

+ <<<< P:\test_scripts\DVS_Add_ESX.ps1 -VCServer testVC -VCUser TestUser -VCPassword pass1 -dcName testdc-dvSwitch dvSwitch-Cluster1 -H

ost testhost.lab.com

+ CategoryInfo : WriteError: (Host:String) [], SessionStateUnauth

orizedAccessException

+ FullyQualifiedErrorId : VariableNotWritable

**Since I am providing input for varabile HOST it should be writable, correct?!

#############################################################################################################

          1. Script Information #########################################################################################

#############################################################################################################

#Name: DVS_Add_ESX.ps1

#Author: BANK & INSURANCE

#Created: 12 November 2010

#Revisions: 01 Inital version

#Purpose: Routine to add ESX Host to DVS - cgi-bin (3) ESX-Config WFL

#NOTE: Sychronous with only one host being actioned at a time.

  1. ## INPUTS

  2. -VCServer : vCenter Serverhostname or IP address

  3. -VCUser : vCenter Server useraccount

  4. -VCPassword : vCenter Server password

  5. -dcName : DC Name

  6. -dvSwitch : Target DVS to add Host

  7. -Host : Host requiring addition to DVS

  8. ##

########################################################################################################################

param($VCServer, $VCUser, $VCPassword, $dcName, $dvSwitch, $Host)

  1. Connecting to the vCenter server

Connect-VIServer -server $VCServer -user $VCUser -password $VCPassword -ErrorVariable Err # -ErrorAction -WarningAction SilentlyContinue

if ($Err) {

Write-Log $TaskID "Critical" "Deploy halted. $Err" "$wflEID"

Close-Exit

} else {

Write-Log $TaskID "Info" "Connected to vCenter $VCServer" "$wflEID"

}

  1. FUNCTION 1 - Get Host

#function Get-dvSwHostCandidate{

#param($container, $recursive, $dvs)

#

#$dvSwMgr.QueryCompatibleHostForExistingDvs($dc.MoRef, $true, $dvs)

#}

  1. FUNCTION 2 - Add Host to DVS

function Add-dvSwHost{

param($dvSwitch, $hostMoRef, $pnic, $nrUplink)

  1. Create variable for a specific DVS Configuration

$spec = New-Object VMware.Vim.DVSConfigSpec

  1. ?

$tgthost = New-Object VMware.Vim.DistributedVirtualSwitchHostMemberConfigSpec

$tgthost.operation = "add"

$tgthost.backing = New-Object VMware.Vim.DistributedVirtualSwitchHostMemberPnicBacking

0..($nrUplink - 1) | % {

$tgthost.Backing.PnicSpec += New-Object VMware.Vim.DistributedVirtualSwitchHostMemberPnicSpec

$tgthost.Backing.PnicSpec[$_].pnicDevice = $pnic[$_]

}

$tgthost.host = $hostMoRef

$spec.Host = $tgthost

$dvSwitch.UpdateViewData()

$spec.ConfigVersion = $dvSwitch.Config.ConfigVersion

  1. Add Host to dvSwitch

$taskMoRef = $dvSwitch.ReconfigureDvs_Task($spec)

  1. Wait for the task to finish

$task = Get-View $taskMoRef

while("running","queued" -contains $task.Info.State){

$task.UpdateViewData("Info")

}

}

param($dvSwitchUplinkBasename, $dvSwitchUplinkNumber)

$dvSwitchUplinkBasenam = "BaseUplinks"

$dvSwitchUplinkNumber = 4

$dvSwMgr = Get-View (Get-View ServiceInstance).content.dvSwitchManager

$dvSwMoRef = New-dvSwitch $datacenterName $dvSwitchName $dvSwitchUplinkBasename $dvSwitchUplinkNumber

$dvSw = Get-View -Id $dvSwMoRef

$dc = Get-Datacenter $datacenterName | Get-View

$candidates = Get-dvSwHostCandidate $dc.MoRef $true $dvSwMoRef

$candidates | % {

$esx = Get-View $_

if($esx.runtime.connectionState -eq "connected"){

$pnicInUse = @()

foreach($vswitch in $esx.Config.Network.Vswitch ){

foreach($pnic in $vswitch.pnic){

$pnicInUse += $pnic

}

}

foreach($vswitch in $esx.Config.Network.ProxySwitch ){

foreach($pnic in $vswitch.pnic){

$pnicInUse += $pnic

}

}

$pnicFree = @()

foreach($pnic in $esx.Config.Network.Pnic){

if(!($pnicInUse -contains $pnic.Key)){

$pnicFree += $pnic.Device

}

}

if($pnicFree.Count -ge $dvSwitchUplinkNumber){

Add-dvSwHost $dvSw $esx.MoRef $pnicFree $dvSwitchUplinkNumber

}

}

}

  1. Connecting to the vCenter server

Connect-VIServer -server $VCServer -user $VCUser -password $VCPassword -ErrorVariable Err # -ErrorAction SilentlyContinue

if ($Err) {

Write-Log $TaskID "Critical" "Deploy halted. $Err" "$wflEID"

Close-Exit

} else {

Write-Log $TaskID "Info" "Connected to vCenter $VCServer" "$wflEID"

}

$dvSwMoRef = New-dvSwitch $datacenterName $dvSwitchName $dvSwitchUplinkNames

0 Kudos
1 Reply
RvdNieuwendijk
Leadership
Leadership

You are probably using the free version of ESXi 4.1 which is read-only.

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos