VMware Cloud Community
DZ1
Hot Shot
Hot Shot
Jump to solution

Please help with adding a member to PSObject

Thanks for everyone that has helped so far, I have this script that I am working on, I have this one section that does not display correctly, it's in bold, the rest is working correctly.  Basically, when it retrieves the names of the data centers, they come in as a table, showing that it's a hash table..similar to this: 

vSphere Data Center(s)  { @{Name=DC1}, @{Name=DC2} } and so on

I tried changing some things around, I would use Select-Object Name, and try some subexpressions, but they just don't work.  The output of the other data looks like this:

ESXihostCount                : 4

vSphere Version: 5.0.0       : 9

vSphere Version: 4.1.0       : 7                        I changed the numbers around...I know, no reason to, but I'm paranoid and I like to stay in that habit.

HW Model: ProLiant BL490c G6 : 1

HW Model: ProLiant BL460c G7 : 2

HW Model: ProLiant BL460c G1 : 1

HW Model: ProLiant BL460c G6 : 1

HW Model: ProLiant DL160 G6  : 1

I just want the curley braces removed, and I'm not sure how.

Portion of the Script

$ALLDataCenter = Get-Datacenter
$ALLCluster = Get-Cluster
$AllESXiHost = Get-VMHost
$ALLVM = Get-VM
$VMView = $ALLVM | Get-View
#Creating a new Powershell Object to store information (for data center)
$vDataCenter = New-Object -TypeName PSObject
#$vSphereDC = $ALLDataCenter | Select Name
#$vSphereDC | Foreach { Add-Member -InputObject $vDataCenter -MemberType NoteProperty -Name "$(vSphere Data Center(s) -Value $_.Name)" }
#$vSphereDC | Add-Member -MemberType NoteProperty -Name "vSphere Data Center(s)" -Value ($ALLDataCenter[0]).Name
$vDataCenter | Add-Member -MemberType NoteProperty -Name "vSphere Data Center(s)" -Value ($ALLDataCenter | select Name)
#Creating a new Powershell Object to store information (for ESXi Hosts)
$ESXiInfo = New-Object -TypeName PSObject
$vVersions = $AllESXiHost | Group Version | Select Name, Count
$HWModel = $AllESXiHost | Group Model | Select Name, Count
$ESXiInfo | Add-Member -MemberType NoteProperty -Name ESXihostCount -Value ($AllESXiHost).count
$vVersions | Foreach { Add-Member -InputObject $ESXiInfo -MemberType NoteProperty -Name "vSphere Version: $($_.Name)" -Value $_.Count }
$HWModel | Foreach { Add-Member -InputObject $ESXiInfo -MemberType NoteProperty -Name "HW Model: $($_.Name)" -Value $_.Count }

Thanks in advance

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

That's because you have more than 1 value in that field.

One trick you can use is to convert that array to a string.

Suppose the variable $dc contains the array with names, then you can do

[string]::Join(',',$dc)

This will generate a string like "DC1,DC2,DC3"


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

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

Instead of

Select-Object -Property Name

try

Select-Object -ExpandProperty Name

That way you get a value and not the object that is normally returned by Select-Object.

You can of course also do

(Select-Object -Property Name).Name

but that is not really "elegant" and clean PowerShell


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

DZ1
Hot Shot
Hot Shot
Jump to solution

Thank you for your help...again Smiley Happy

I really want to slap myself, I browse the community forums and I saw you suggest the same solution for a different member.  I event went to powershell help and read about.  In fact, I think you helped me with something similar to this on another script...of course, when it came time for me to use this, I completely forgot about it. 

I do have 1 small issue, for the most part when I use any of your suggestions, they work.  They still show up as a tablue however, it looks better than before, but the output is now:

vSphere Data Center(s) : {DC1, DC2, DC3}

I tried to pipe my info to fl, but that does not change anything.  Any suggestions?  It still looks much better though.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That's because you have more than 1 value in that field.

One trick you can use is to convert that array to a string.

Suppose the variable $dc contains the array with names, then you can do

[string]::Join(',',$dc)

This will generate a string like "DC1,DC2,DC3"


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

0 Kudos
DZ1
Hot Shot
Hot Shot
Jump to solution

I know you have heard this many times, but you are the man.  That worked perfectly.  I really want to be like you when I grow up...thank you so much. 

0 Kudos