VMware Cloud Community
tianhailong666
Enthusiast
Enthusiast
Jump to solution

use foreach(), the output of export-csv was repeat for vmhost.

Hi Master,

I just encounter an issue which made me confused and not have a soluation till now , Anybody who could help me on it ? thanks!

Situation;

I want to get ESXi host Modules info via ESXCLI for powercli script. but output are still have repeat vmhostname, is there have modity for it , below is my orginal code.

#$cred = get-credential

$vCenter = "win2012c.demo.com"

$vmhosts = ( get-cluster -name "prod"  | get-vmhost )

Connect-VIserver -server $vCenter

$esxcli = @()

foreach( $vmhost in $vmhosts )

{

$esxcli += get-esxcli -vmhost $vmhost

$result = $EsxCli.software.vib.list() | where {$_.Name -match "mega"} | select Name,Version,Vendor,CreationDate,@{n="VMhost";e={$vmhost.name}}

}

$result |  export-csv -path c:\modules.csv -notype .

Modules.jpg

Attached is result. you can see it,   it contains repeat server name,     just I have there VMhost name  which are myesxi01,myesxi02 and myesxi03.

Reply
0 Kudos
1 Solution

Accepted Solutions
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

In your initial script you have created an array for esxcli = @() and in the foreach loop you are adding the hostname '$esxcli += get-esxcli -vmhost $vmhost' which repeats the same hostname and stores in array.

You are executing the cmdlet to get desired result from '$result = $EsxCli.software.vib.list() | where {$_.Name -match "mega"} | select Name,Version,Vendor,CreationDate,@{n="VMhost";e={$vmhost.name}}' and you are not storing the output for each variable and variable is not part of array.

So I have changed the array location as $result = @() and adding the storing the result as $result += $EsxCli.software.vib.list() | where {$_.Name -match "mega"} | select Name,Version,Vendor,CreationDate,@{n="VMhost";e={$vmhost.name}}

Hope this helps. Please do let me know if you need any further explanation.

Mark this question as correct if it really helps you.

View solution in original post

Reply
0 Kudos
3 Replies
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

Try like this

#$cred = get-credential

$vCenter = "win2012c.demo.com"

$vmhosts = ( get-cluster -name "prod"  | get-vmhost )

Connect-VIserver -server $vCenter

$result = @()

foreach( $vmhost in $vmhosts )

{

$esxcli = get-esxcli -vmhost $vmhost

$result += $EsxCli.software.vib.list() | where {$_.Name -match "mega"} | select Name,Version,Vendor,CreationDate,@{n="VMhost";e={$vmhost.name}}

}

$result |  export-csv -path c:\modules.csv -notype .

Reply
0 Kudos
tianhailong666
Enthusiast
Enthusiast
Jump to solution

SivaramsharmarHi Sivaramsharmar,


Thank you very much and that does work now !  BTW, Could you tell me what's reason for it ?  or what's difference to compare with just I firist to Scripts?  thanks.Modules2.jpg

Reply
0 Kudos
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

In your initial script you have created an array for esxcli = @() and in the foreach loop you are adding the hostname '$esxcli += get-esxcli -vmhost $vmhost' which repeats the same hostname and stores in array.

You are executing the cmdlet to get desired result from '$result = $EsxCli.software.vib.list() | where {$_.Name -match "mega"} | select Name,Version,Vendor,CreationDate,@{n="VMhost";e={$vmhost.name}}' and you are not storing the output for each variable and variable is not part of array.

So I have changed the array location as $result = @() and adding the storing the result as $result += $EsxCli.software.vib.list() | where {$_.Name -match "mega"} | select Name,Version,Vendor,CreationDate,@{n="VMhost";e={$vmhost.name}}

Hope this helps. Please do let me know if you need any further explanation.

Mark this question as correct if it really helps you.

Reply
0 Kudos