Pages: [1]
Print
Author Topic: List vSwitches From all ESX Hosts [solved]  (Read 3083 times)
wintersa
Newbie
*
Posts: 5


View Profile
« on: February 18, 2009, 06:32:46 AM »

Hello I`m trying to list all vSwitches from all our 9 ESX hosts. What I have so far is this;

function getvSwitch
{
  foreach ($VMHost in $VMHosts)
       {
         $VMHostArray = @($VMHost)
         $vSwitches = Get-VirtualSwitch -VMHost $VMHostArray | sort-object Name
       }
           foreach ($vSwitch in $vSwitches)
            {
               $myCol = @()
               #$myCol += addline 'Host' $VMHostArray.Name
               $myCol += addline 'Naam' $vSwitch.Name
               $myCol += addline 'Poorten' $vSwitch.NumPorts
               $myCol += addline 'Poorten beschikbaar' $vSwitch.NumPortsAvailable
               $myCol += addline 'MTU' $vSwitch.MTU            
               #write-host
               write-host $VMHostArray
               write-host $myCol   
            }
}

Output in powershell looks like this;

s-esx09.xxx.lan
@{Item=Naam; Waarde=vSwitch0} @{Item=Poorten; Waarde=64} @{Item=Poorten beschikbaar; Waarde=59} @{Item=MTU; Waarde=1500}
s-esx09.xxx.lan
@{Item=Naam; Waarde=vSwitch1} @{Item=Poorten; Waarde=64} @{Item=Poorten beschikbaar; Waarde=60} @{Item=MTU; Waarde=1500}
s-esx09.xxx.lan
@{Item=Naam; Waarde=vSwitch2} @{Item=Poorten; Waarde=64} @{Item=Poorten beschikbaar; Waarde=60} @{Item=MTU; Waarde=1500}
s-esx09.xxx.lan
@{Item=Naam; Waarde=vSwitch3} @{Item=Poorten; Waarde=64} @{Item=Poorten beschikbaar; Waarde=61} @{Item=MTU; Waarde=1500}
s-esx09.xxx.lan
@{Item=Naam; Waarde=vSwitch4} @{Item=Poorten; Waarde=64} @{Item=Poorten beschikbaar; Waarde=20} @{Item=MTU; Waarde=1500}
s-esx09.xxx.lan
@{Item=Naam; Waarde=vSwitch5} @{Item=Poorten; Waarde=64} @{Item=Poorten beschikbaar; Waarde=61} @{Item=MTU; Waarde=1500}

As you can see is the function only listing the vSwitches from the last server. But I want this;

ESXServer01
vSwitch0
vSwitch1

ESXServer02
vSwitch0
vSwitch1
vSwitch2

etc....

Who have done this before and can give me some assistant.
« Last Edit: February 19, 2009, 05:23:56 AM by wintersa » Logged
esarakaitis
Administrator
Sr. Member
*****
Posts: 256


8223109 sack57@hotmail.com littleking57 rootinfortwayne
View Profile WWW Email
« Reply #1 on: February 18, 2009, 07:04:56 AM »

Code:
get-vmhost | foreach-object `
{$vmhost=$_
(get-view $_.id).config.network.vswitch | `
select @{name="Hostname"; expression={$vmhost.name}}, @{name="VSwitch"; expression={$_.name}}, @{name="BeaconStatus"; expression={$_.spec.policy.nicteaming.failurecriteria.checkbeacon}}}
Logged
wintersa
Newbie
*
Posts: 5


View Profile
« Reply #2 on: February 18, 2009, 08:21:15 AM »

thanks for you fast reply but the output with your code in powershell is this;

Hostname
--------

s-esx04.xxx.lan
s-esx04.xxx.lan
s-esx04.xxx.lan
s-esx07.xxx.lan
s-esx07.xxx.lan
s-esx07.xxx.lan
s-esx07.xxx.lan
s-esx07.xxx.lan
s-esx07.xxx.lan
s-esx06.xxx.lan
s-esx06.xxx.lan
s-esx06.xxx.lan
s-esx03.xxx.lan
s-esx03.xxx.lan
s-esx03.xxx.lan
s-esx05.xxx.lan
s-esx05.xxx.lan
s-esx05.xxx.lan
s-esx09.xxx.lan
s-esx09.xxx.lan
s-esx09.xxx.lan
s-esx09.xxx.lan
s-esx09.xxx.lan
s-esx09.xxx.lan
s-esx01.xxx.lan
s-esx01.xxx.lan
s-esx01.xxx.lan
s-esx08.xxx.lan
s-esx08.xxx.lan
s-esx08.xxx.lan
s-esx08.xxx.lan
s-esx02.xxx.lan
s-esx02.xxx.lan
s-esx02.xxx.lan
Logged
esarakaitis
Administrator
Sr. Member
*****
Posts: 256


8223109 sack57@hotmail.com littleking57 rootinfortwayne
View Profile WWW Email
« Reply #3 on: February 18, 2009, 10:48:54 AM »

i cannot duplicate your result, what version of the toolkit/poweshell are you using, this is my result:
Code:
Hostname                                VSwitch                                                            BeaconStatus
--------                                -------                                                            ------------
ohaephqvm007.domain.com                  vSwitch0                                                                   True
ohaephqvm007.domain.com                  vSwitch1                                                                  False
ohaephqvm007.domain.com                  vSwitch2                                                                   True
ohaephqvm007.domain.com                  vSwitch3                                                                   True
ohaephqvm007.domain.com                  vSwitch4                                                                   True

Logged
esarakaitis
Administrator
Sr. Member
*****
Posts: 256


8223109 sack57@hotmail.com littleking57 rootinfortwayne
View Profile WWW Email
« Reply #4 on: February 18, 2009, 10:51:29 AM »

this is the same script broken down to multi-line:
Code:
get-vmhost | foreach-object `
{$vmhost=$_
(get-view $_.id).config.network.vswitch | `
select  @{name="Hostname"; expression={$vmhost.name}},
            @{name="VSwitch"; expression={$_.name}},
            @{name="BeaconStatus"; expression={$_.spec.policy.nicteaming.failurecriteria.checkbeacon}}}

Logged
wintersa
Newbie
*
Posts: 5


View Profile
« Reply #5 on: February 19, 2009, 05:23:38 AM »

Well found out what the problem was;  I worked with an older version of VMware Toolkit on my work then I have at home, updated the toolkit  to version 1.5 and everything works. Thank you for your help.
Logged
esarakaitis
Administrator
Sr. Member
*****
Posts: 256


8223109 sack57@hotmail.com littleking57 rootinfortwayne
View Profile WWW Email
« Reply #6 on: February 19, 2009, 07:29:41 AM »

anytime... buuut is the script i posted the one you wanted? Smiley
Logged
wintersa
Newbie
*
Posts: 5


View Profile
« Reply #7 on: February 19, 2009, 08:30:44 AM »

well it does what I asked Cheesy but what I am trying to find out is this;

instead the output of this
@{name="Hostname"; expression={$vmhost.name}},
@{name="VSwitch"; expression={$_.name}},
@{name="Ports"; expression={$_.numPorts}},
@{name="Ports Available"; expression={$_.numPortsAvailable}},
@{name="MTU"; expression={$_.mtu}}, 
@{name="VMnic"; expression={$_.pnic}},
@{name="BeaconStatus"; expression={$_.spec.policy.nicteaming.failurecriteria.checkbeacon}}

just into an array
$myCol = @()
$myCol += addline 'Hostname' $Variable
$myCol += addline 'vSwitch' $Variable
$myCol += addline 'Ports' $Variable
$myCol += addline 'Ports Available' $Variable
$myCol += addline 'MTU' $Variable
$myCol += addline 'VMnic' $Variable
$myCol += addline 'BeaconStatus' $Variable

addline is another function that will display nice tables.
"addline 'Item' '$variable' "

« Last Edit: February 20, 2009, 09:57:30 AM by wintersa » Logged
ewannema
Administrator
Newbie
*****
Posts: 33


View Profile WWW
« Reply #8 on: February 19, 2009, 10:00:01 AM »

Hmm, I am not sure I know exactly what you are doing, but here is an attempt.  You can save up all of the objects and process them afterwards.  You can get trickier with this, but this is probably the simplest adaptation.  You will have to modify it to add the other items you want.

Code:
$myCol = @()
get-vmhost | foreach-object `
{$vmhost=$_
(get-view $_.id).config.network.vswitch | % {`
$myCol += $_ | select @{name="Hostname"; expression={$vmhost.name}},
            @{name="VSwitch"; expression={$_.name}},
            @{name="BeaconStatus"; expression={$_.spec.policy.nicteaming.failurecriteria.checkbeacon}}}
}

$myCol | ForEach-Object {
addline 'Hostname' $_.Hostname
addline 'vSwitch' $_.vSwitch
addline 'BeaconStatus' $_.BeaconStatus
}
Optionally you could just take the output of the original script and do | ConvertTo-Html.
« Last Edit: February 19, 2009, 10:02:07 AM by esarakaitis » Logged
esarakaitis
Administrator
Sr. Member
*****
Posts: 256


8223109 sack57@hotmail.com littleking57 rootinfortwayne
View Profile WWW Email
« Reply #9 on: February 19, 2009, 10:04:07 AM »

wintersa, have you played with the build in export functions like ewannama suggested? even export to csv, etc..?

just curious as to what your goal is
Logged
wintersa
Newbie
*
Posts: 5


View Profile
« Reply #10 on: February 20, 2009, 07:19:49 AM »

Thank you people for you fantastic help, my goal is to learn powershell and the VMware API / SDK. I`m a Linux bash programmer and sometimes I need some functionality that I use in my Bash Scripts.  Like putting information from one function to another function to manipulate the output.
Logged
esarakaitis
Administrator
Sr. Member
*****
Posts: 256


8223109 sack57@hotmail.com littleking57 rootinfortwayne
View Profile WWW Email
« Reply #11 on: February 20, 2009, 07:48:47 AM »

great, we're all in the same boat! Smiley
Logged
Pages: [1]
Print
Jump to: