VMware Cloud Community
faherne_CTI
Enthusiast
Enthusiast
Jump to solution

Query List of vCenters and output vCenter Server Name and Unique ID

Hi,

I'm looking to query a textfile list of vCenters and output each vCenter server's Name and vcenter Unique ID to CSV: | vCenter Name |  vCenter Unique ID |

$vCenterServerList = Connect-viserver -Server (Get-Content C:\Scripts\vCenterList.txt)

Foreach ($vCenter in $vCenterServerList)

{

$si = Get-View ServiceInstance

$setting = Get-View $si.Content.Setting

$vCenterUniqueID = $setting.QueryOptions("instance.id") | Select -ExpandProperty Value

select $.vCenter.Name,$vCenterUniqueID | Export-Csv C:\Scripts\vCenter-UniqueID-List.csv

}

Currently I'm getting the following error:

At line:1 char:43

+ ... ew $si.Content.Setting | $setting.QueryOptions("instance.id") | Selec ...

+                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expressions are only allowed as the first element of a pipeline.

    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException

    + FullyQualifiedErrorId : ExpressionsMustBeFirstInPipeline

Any suggestions on how I can fix this?

Thanks,

Fin

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this

Connect-viserver -Server (Get-Content C:\Scripts\vCList.txt) > $null

$report = foreach($vc in $global:DefaultVIServers){ 

    $si = Get-View ServiceInstance -Server $vc

    $setting = Get-View $si.Content.Setting -Server $vc

    $setting.QueryOptions("instance.id") | 

    Select @{N='vCenter';E={$vc.Name}},Value

}

$report | Export-Csv C:\Scripts\vCenter-UniqueID-List.csv 


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

View solution in original post

6 Replies
vijayrana968
Virtuoso
Virtuoso
Jump to solution

This is the same one-liner you can user to export vCenter name and Uuid LucD​ provided you on another forum, unless you are looking for something else.

Connect-viserver -Server (Get-Content C:\vcenterList.txt) | select Name,InstanceUuid | Export-Csv C:\Scripts\vCenter-Uuid.csv

0 Kudos
faherne_CTI
Enthusiast
Enthusiast
Jump to solution

Unfortunately it isn't. That was my mistake last time around. Each vCenter has a:

1.) vCenter Server Unique ID

--> A number from 0-63

2.) vCenter Instance ID ((UUID)

--> vCenter GUID

I'm looking to identify duplicate vCenter Server Unique IDs.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this

foreach($vc in $global:DefaultVIServers){

    $si = Get-View ServiceInstance -Server $vc

    $setting = Get-View $si.Content.Setting -Server $vc

    $setting.QueryOptions("instance.id") |

    Select @{N='vCenter';E={$vc.Name}},Value

}


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

0 Kudos
faherne_CTI
Enthusiast
Enthusiast
Jump to solution

Hi Luc,

If I wanted to modify your code to:

1.) Read in a textfile with a list of vCenter servers, then

2.) Output the results to a CSV file with 2 columns,

would the code look something like as follows?

$vc = Connect-viserver -Server (Get-Content C:\Scripts\vCList.txt)

foreach($vc in $global:DefaultVIServers){

    $si = Get-View ServiceInstance -Server $_

    $setting = Get-View $si.Content.Setting -Server $_

    $setting.QueryOptions("instance.id") |

    Select @{N='vCenter';E={$vc.Name}},Value | Select $N, $E | Export-Csv C:\Scripts\vCenter-UniqueID-List.csv

}

I'm currently getting the following error from the above script:

Select : The value of a parameter was null; one of the following types was expected: {System.String,

System.Management.Automation.ScriptBlock}.

At line:6 char:48

+     Select @{N='vCenter';E={$vc.Name}},Value | Select $N, $E | Export ...

+                                                ~~~~~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [Select-Object], NotSupportedException

    + FullyQualifiedErrorId : DictionaryKeyUnknownType,Microsoft.PowerShell.Commands.SelectObjectCommand

Thanks,

Fin

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this

Connect-viserver -Server (Get-Content C:\Scripts\vCList.txt) > $null

$report = foreach($vc in $global:DefaultVIServers){ 

    $si = Get-View ServiceInstance -Server $vc

    $setting = Get-View $si.Content.Setting -Server $vc

    $setting.QueryOptions("instance.id") | 

    Select @{N='vCenter';E={$vc.Name}},Value

}

$report | Export-Csv C:\Scripts\vCenter-UniqueID-List.csv 


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

faherne_CTI
Enthusiast
Enthusiast
Jump to solution

That worked a treat Smiley Happy

Thanks Luc!

0 Kudos