VMware Cloud Community
DZ1
Hot Shot
Hot Shot

Too much space in the output

I'm writing this script and when the output occurs, there is too much space between the -Name and the -Value of the PSObject, here is what it looks like:

Total Templates : 60

Total VMs       : 499  "Too much space"

Total ESXi Hosts : 36

Here is what I want, the -Name, a space and then the -Value.  The colon is in there as part of the object (Why is the colon automatically there anyway, and can it be changed to something else?  Maybe a dash -)

Total Templates: 60

Total VMs: 499

Total ESXi Hosts: 36

The script I'm workign on is larger, I just decided to play around with the formattting, so I made something short so I can have fast results, and not wait for all the data to process.

Here is the script that produced this

Function MyTest {
$AllTemps = Get-template
$Allhosts = Get-VMHost
$allVMs = Get-vm
$VM = New-Object PSObject
$VM | Add-Member -MemberType NoteProperty -Name "Total Templates" -Value ($alltemps).count
$VM | Add-Member -MemberType NoteProperty -Name "Total VMs `t" -Value ($allvms).count
$MYhost = New-Object PSObject
$MYhost | Add-Member -MemberType NoteProperty -Name "Total ESXi Hosts" -Value ($Allhosts).count
Write $VM, $MYhost | fl
}
Send-MailMessage -SmtpServer '1.18.1.5' -From "<m@com>" -To "<t@com>" -Subject "Test" -body (MyTest | Out-String)
0 Kudos
2 Replies
mattboren
Expert
Expert

Hello, DZ1-

There are a couple of things causing that extra space after the name of the property:

    1. the use of the Format-List ("fl") in the function -- this makes the left columns all the same width by adding spaces to shorter strings
    2. the property name itself has " `t" at the end of it, which is a space character followed by a tab character

So, even if you weren't using Format-List, the extra characters in the property name cause that extra space.  The use of Format-List is also the reason for the colons.

You could better control the output by just returning an object from the function, and then creating the desired string.  A quick update to the function to make it return just an object with the names/values populated:

Function MyTest2 {
   
New-Object -Type PSObject -Property @{
       
"Total Templates" = (Get-Template | Measure-Object).Count
       
"Total VMs" = (Get-VM | Measure-Object).Count
       
"Total ESXi Hosts" = (Get-VMHost | Measure-Object).Count
    }
## end new-object
} ## end fn

Then, you could make any kind of string you want from the output.  To output a few lines with the names and values separated by a dash, as you suggested, you could do something like:

MyTest2 | %{$oTmp = $_; Get-Member -Input $oTmp -MemberType NoteProperty} | %{"$($_.Name)- $($oTmp.$($_.Name))"}

Sample output:

Total ESXi Hosts- 171
Total Templates- 12
Total VMs- 3901

How does that do for you?

0 Kudos
DZ1
Hot Shot
Hot Shot

Sorry, I forgot to mention, I was playing around with the "`t", I was going to try and make a tab for each line to even it out, I had the fl in there because the actual script was not looking right (the example is just something I used to play with), so I used format-list because I'm having different information in different sections.  I should have removed the tab, but I was trying different things, and I forgot to take it out.  I will try what you suggested though...thank you. Smiley Happy

0 Kudos