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?!
#############################################################################################################
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.
## INPUTS
-VCServer : vCenter Serverhostname or IP address
-VCUser : vCenter Server useraccount
-VCPassword : vCenter Server password
-dcName : DC Name
-dvSwitch : Target DVS to add Host
-Host : Host requiring addition to DVS
##
########################################################################################################################
param($VCServer, $VCUser, $VCPassword, $dcName, $dvSwitch, $Host)
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"
}
FUNCTION 1 - Get Host
#function Get-dvSwHostCandidate{
#param($container, $recursive, $dvs)
#
#$dvSwMgr.QueryCompatibleHostForExistingDvs($dc.MoRef, $true, $dvs)
#}
FUNCTION 2 - Add Host to DVS
function Add-dvSwHost{
param($dvSwitch, $hostMoRef, $pnic, $nrUplink)
Create variable for a specific DVS Configuration
$spec = New-Object VMware.Vim.DVSConfigSpec
?
$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
Add Host to dvSwitch
$taskMoRef = $dvSwitch.ReconfigureDvs_Task($spec)
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
}
}
}
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
You are probably using the free version of ESXi 4.1 which is read-only.
Regards, Robert