VMware Cloud Community
CenturaBro
Enthusiast
Enthusiast
Jump to solution

Script to show what vCenter a VM is on when connecting to multiple vcenters?

When i run a report against multiple vCenters I'm looking to show the vcenter the VM is on as a column in the output.  I cant seem to get it to work.  Anyone ever achieve something like this before?

something like adding the vcenter as a member of a property:

Something along the lines of this

$vCenterView = get-vm | sort | Get-view

foreach ($VMs in $vCenterview){

    $strVC = $null ; $strVC = $vCenterView.client.serviceurl.split("/", [StringSplitOptions]'RemoveEmptyEntries')[1].toUpper()

    $VMs | Add-Member -type NoteProperty -name vCenter -value $strVC

    }

    $VMXNetString = $vCenterView | select name, vCenter

I'm trying to format the get-view.client.serviceurl as this is the only way i can see to select the vcenter the VM is connected to.

the above script does not work correct.. it will only show 1vcenter for all VM's regardless of the vcenter its actually on.

Reply
0 Kudos
1 Solution

Accepted Solutions
CenturaBro
Enthusiast
Enthusiast
Jump to solution

Just in case anyone else has an issue like this i was able to solve it by using the origional get-vm variable instead of piping to get-view.  I was able to utilize $_.extensiondata to get the same result as get-view and was also able to use $_.uid  and split that output to show only the vCenter the VM is located on instead of a URL like so:

$VMXNET3 = $GetAllVMs | Get-NetworkAdapter | Where-object {$_.Type -ne "Vmxnet3"} | select parent, type, @{N="vCenter";E={(($_.uid).split("@")[1]).split(":")[0]}} | ConvertTo-Html -Fragment -PreContent '<h3>Showing any NIC that is not VMXNET3</h3>' | Out-String

also just for Shts n Giggles i created global member that will be added to every Get-VM that can use "vCenter" as an object that can be selected, then added to all the outputs involved like so:

foreach ($vm0 in $GetAllVMs){

$VCTR0 = (($vm0.uid).split("@")[1]).split(":")[0]

$VM0 | Add-Member -type NoteProperty -name vCenter -value $VCTR0 -Force

}

Either of these ways can be utilized as a solution.

View solution in original post

Reply
0 Kudos
1 Reply
CenturaBro
Enthusiast
Enthusiast
Jump to solution

Just in case anyone else has an issue like this i was able to solve it by using the origional get-vm variable instead of piping to get-view.  I was able to utilize $_.extensiondata to get the same result as get-view and was also able to use $_.uid  and split that output to show only the vCenter the VM is located on instead of a URL like so:

$VMXNET3 = $GetAllVMs | Get-NetworkAdapter | Where-object {$_.Type -ne "Vmxnet3"} | select parent, type, @{N="vCenter";E={(($_.uid).split("@")[1]).split(":")[0]}} | ConvertTo-Html -Fragment -PreContent '<h3>Showing any NIC that is not VMXNET3</h3>' | Out-String

also just for Shts n Giggles i created global member that will be added to every Get-VM that can use "vCenter" as an object that can be selected, then added to all the outputs involved like so:

foreach ($vm0 in $GetAllVMs){

$VCTR0 = (($vm0.uid).split("@")[1]).split(":")[0]

$VM0 | Add-Member -type NoteProperty -name vCenter -value $VCTR0 -Force

}

Either of these ways can be utilized as a solution.

Reply
0 Kudos