DanielCrider
VMware Employee
VMware Employee

How do you locate an object? spec.configVersion for ReconfigureDVPortgroup_Task

Hi! I am very new to Powershell and I am still learning the basics. I have some code that I put together by watching an action in Onyx. The code is designed to set a vDS port group named "Production" to block all traffic. Here is the code:

Get-vds | out-null

Get-dvportgroup | ForEach-Object {

If ($_.name -like "Production") {

$dvportgrpID = $_.id

$dvportgrpname = $_.name

$spec = New-Object VMware.Vim.DVPortgroupConfigSpec

$spec.configVersion = "5"

$spec.defaultPortConfig = New-Object VMware.Vim.VMwareDVSPortSetting

$spec.defaultPortConfig.blocked = New-Object VMware.Vim.BoolPolicy

$spec.defaultPortConfig.blocked.inherited = $false

$spec.defaultPortConfig.blocked.value = $true

$_this = Get-View -Id $dvportgrpID

$_this.ReconfigureDVPortgroup_Task($spec)

}

My problem is finding the value of $spec.configVersion. It seems to be an integer that is incrementing every time I use vSphere Client to block all traffic on the port group or each time I successfully run the script. If I can guess the right number and manually load it in the script the script works great. If I enter an incorrect value or leave it out the script fails. I know there is got to be a way to find $spec.configVersion. But right now I don't know even where to begin to look. Like I said, I am VERY new at this.

Thanks!

0 Kudos
9 Replies
LucD
Leadership
Leadership

I suspect you could use the QueryDvsByUuid method to find the current verson number (which is a string btw) via the returned object, which is a DistributedVirtualSwitch.


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

0 Kudos
DanielCrider
VMware Employee
VMware Employee

Can you give me more info. Remember. I am a total nebie at this. I tried:

$myvds = get-vds

$vdsview = get-view $myvds[1].uuid

$query = $vdsview.querydvsbyuuid

echo $vdsview -


gives nothing

$vdsview | get-member -


gives "No ogject has been specified to get-member" error.

0 Kudos
admin
Immortal
Immortal

You can get that version in

$_this.config.ConfigVersion

You have to set your spec to the current value, don't increment it. Config versions are seldom required in vSphere API, in fact I think this is the first time I've ever seen one required.

=====

Carter Shanklin

Read the PowerCLI Blog
[Follow me on Twitter|http://twitter.com/cshanklin]

0 Kudos
ykalchev
VMware Employee
VMware Employee

You should call Get-View cmdlet with vds ID parameter instead of uuid and just look at Config.ConfigVersion property as Carter mentioned.

$myvds = get-vds
$vdsview = get-view $myvds[1].Id
$version = $vdsview.Config.ConfigVersion

Regards,

Yasen

Yasen Kalchev, vSM Dev Team
LucD
Leadership
Leadership

Correct, see dvSwitch scripting – Part 1 – Creation for a sample script.


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

DanielCrider
VMware Employee
VMware Employee

Yasen, Luc, and Carter --

First -- THANKS! By using what you all gave me, I managed to get it working. Yasen, your suggestions really helped. I will post my revised script on the board in a few moments for all to see.

Second -- My major question here is how did you know? If I see something called "$spec.configVersion" that is a part of "VMware.Vim.DVPortgroupConfigSpec" how do I find out that is actually referring to .Config.ConfigVersion in the view of Get-dvportgroup?

Third -- I have one more major vexing problem. Several of my "damage scripts" for this troubleshooting course call things in PowerCLI. Since this is a Troubleshooting class I am deliberately trying to break things on the student servers. I use Invoke-Expression to do most of the damage. But if I am breaking the ESX host communications channel (which several of my scripts need to do) my script hangs waiting for something to come back from Invoke-Expression. Even if I use an error return code to send something back it never moves forward. The script works, but I have to CNTRL-C the script and restart PowerCLI. Example:

Write-host "Damaging " $ESXHost

cd "C:\program files\Vmware\Vmware vSphere CLI\bin"

$auth = " --server " + $ESXHost + " --username root " + "

--password " + $root

$vclicom = ".\vicfg-vswitch.pl " + $auth + " -U vmnic0 vSwitch0"

invoke-expression $vclicom -ErrorAction silentlycontinue

The script will hang at invoke-expression and never come back. Is there anyway to prevent the hang and keep the script moving?

(BTW, I am sure there is a way to disconnect the vmnic0 from vSwitch0 in PowerCLI. But as I recall when I tried it that way it also hung, and I had to remove anything that was using the vSwitch (vmk ports, vswif ports, etc.) before I could disconnect the uplink. So there are several "bad things" that I am doing that I can get away with in vCLI that are harder to do in PowerCLI.)

My thanks for any help you can send.

Daniel Crider

Content Developer

Education Design and Development

VMware

301 Sunset Mountain Road

Lipan, Texas 76462

Office: 940-769-9023

Cell: 254-592-6815 (call office first)

0 Kudos
DanielCrider
VMware Employee
VMware Employee

My thanks to all of you for your help. I now have my script working. Here is the new script:

                                                                                • block all ports code
$myvds = get-vds Write-Host "...Connected to vDS" Get-dvportgroup | ForEach-Object {
  1. ------- ReconfigureDVPortgroup_Task -------
  2. if ($ConnectError -ne $Null) "`aERROR: ${ConnectError}"
If ($_.name -like "Production") { $dvportgrpID = $_.id $dvportgrpname = $_.name $portview = get-view $_.id $version = $portview.Config.ConfigVersion $ver = $version $spec = New-Object VMware.Vim.DVPortgroupConfigSpec #$spec.changeVersion = get-date -uformat "%Y-%m-%dT%H:%M:%SZ" $spec.configVersion = $ver $spec.defaultPortConfig = New-Object VMware.Vim.VMwareDVSPortSetting $spec.defaultPortConfig.blocked = New-Object VMware.Vim.BoolPolicy $spec.defaultPortConfig.blocked.inherited = $false $spec.defaultPortConfig.blocked.value = $true $_this = Get-View -Id $dvportgrpID $_this.ReconfigureDVPortgroup_Task($spec) | Out-Null } # end if name = production } # foreach $mydvpg in get-dvportgroup loop write-host "All ports closed on Production for " $TargetPair.ESX

However, my basic question remains. How did you know that the $spec.ConfigVersion object was located in the view of the portgroup Config.ConfigVersion? Is there some way to search for "ConfigVersion" and find it?

0 Kudos
LucD
Leadership
Leadership

For your 2nd question, by consulting the VMware vSphere API Reference Documentation.

The ReconfigureDVPortgroup_Task method requires a DVPortgroupConfigSpec parameter.

The API Reference says for the configVersion property

The (perhaps a bit cryptic) description says that the value should be the same as the ConfigInfo.configVersion property in the DVPortgroupConfigInfo object.

The DVPortgroupConfigInfo object is pointed to by the config property of the DistributedVirtualPortgroup managed object.


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

0 Kudos
DanielCrider
VMware Employee
VMware Employee

Thanks Luc!

I have a lot to learn about following these reference documents. I actually looked right at this reference -- and did not see the connection. I STILL don't see the connection -- but maybe if I sit down and read through this 5 or 6 times and follow the links I will see how I can find the next one.

0 Kudos