VMware Cloud Community
jamie20
Enthusiast
Enthusiast
Jump to solution

Output only the value in get-hvmachinesummary

Hi Guys,

A simple query , But I cant able to get. Anyone help me out.

I am running the following command:

$o = Get-HVMachineSummary | where { 'Error' -contains $_.Base.BasicState } | select -ExpandProperty Base | Select-Object Name
write-host "VMName is" $o

 

The output is:

VMName is @{Name=VM1}

Expected output is:

VMName is VM1

 

How to get rid of @{Name=} .

 

Any help?

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try

Select-Object -ExpandProperty Name

or else

Select-Object @{N='Name';E={$_.Name}}


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

View solution in original post

18 Replies
LucD
Leadership
Leadership
Jump to solution

Try

Select-Object -ExpandProperty Name

or else

Select-Object @{N='Name';E={$_.Name}}


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

jamie20
Enthusiast
Enthusiast
Jump to solution

Thanks LucD...

Select-Object @{N='Name';E={$_.Name}}

This didnt worked. Output as above.

Select-Object -ExpandProperty Name

This works perfectly.

0 Kudos
jamie20
Enthusiast
Enthusiast
Jump to solution

LucD...Am having another doubt now...Expanding the code further.

$machine = Get-HVMachineSummary | where { 'ERROR' -contains $_.Base.BasicState } | select -ExpandProperty Base | Select-Object -ExpandProperty Name
$desktop = Get-HVMachineSummary | where { 'ERROR' -contains $_.Base.BasicState } | select -ExpandProperty Base | Select-Object -ExpandProperty DesktopName
write-host "Machine:" $machine "present in Desktoppool:" $desktop

Output I got is:

Machine: VM1 VM2 present in Desktoppool D1D2

 

Expected output is:

Machine: VM1 present in Desktoppool D1

Machine: VM2 present in Desktoppool D2

......

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is because you have multiple objects returned.

Then you better use a loop, something like this for example

Get-HVMachineSummary | where { 'ERROR' -contains $_.Base.BasicState } | 
ForEach-Object -Process {
    Write-Host "Machine: $($_.Base.Name) present in Desktoppool $($_.Base.DesktopName)"
}


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

jamie20
Enthusiast
Enthusiast
Jump to solution

Hi LucD,

Sorry to trouble you again. Your script is working fine.

But my requirement is am bringing the info as a mail report.

Below is the script what I made

#Horizon job
Connect-HVServer -server 10.0.0.1 -User vcadmin -Password welcome -Domain company.domain
$concurrent_users = ((get-hvlocalsession).sessiondata | where-object{($_.sessionstate -eq "connected") -and ($_.sessiontype -eq "desktop")}).count
$error_machines_count = (Get-HVMachineSummary | where { 'ERROR' -contains $_.Base.BasicState }).count
$machine = Get-HVMachineSummary | where { 'ERROR' -contains $_.Base.BasicState } | select -ExpandProperty Base | Select-Object -ExpandProperty Name
$desktop = Get-HVMachineSummary | where { 'ERROR' -contains $_.Base.BasicState } | select -ExpandProperty Base | Select-Object -ExpandProperty DesktopName
Disconnect-HVServer -Confirm:$false

#Column Creation
$HtmlTable = "<table border='1' align='Left' cellpadding='2' cellspacing='0' style='color:black;font-family:arial,helvetica,sans-serif;text-align:left;'>
<tr style ='font-size:13px;font-weight: normal;background: #0789f7'>
<th align=left><b>Concurrent Users</b></th>
<th align=left><b>Error Machines Count</b></th>
<th align=left><b>Error Machines</b></th>
</tr>"

# Insert data
$HtmlTable += "<tr style='font-size:13px;background-color:#FFFFFF'>
<td><center>" + $concurrent_users + "</center></td>
<td><center>" + $error_machines_count + "</center></td>
<td>Machine: " + $machine + " present in Desktoppool: " + $desktop + " </td>
</tr>"
$HtmlTable += "</table>"

# Send Mail Inputs
$smtpserver = *******************
$from = *******************
$to = *******************
$subject = *******************
$body = $HtmlTable

Send-MailMessage -smtpserver $smtpserver -from $from -to $to -subject $subject -body $body -bodyashtml

##################

Output I get:

Concurrent UsersError Machines CountError Machines
1002Machine: VM1 VM2 present in Desktoppool D1D2

 

Am fine with the first two columns...The third column is what am not getting...I dont know how to alter the script with for loop...:(

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You will need to do that again in a loop.
Something like this for example.

Connect-HVServer -Server 10.0.0.1 -User vcadmin -Password welcome -Domain company.domain

$concurrent_users = ((get-hvlocalsession).sessiondata | Where-Object { ($_.sessionstate -eq "connected") -and ($_.sessiontype -eq "desktop") }).count
$error_machines_count = (Get-HVMachineSummary | where { 'ERROR' -contains $_.Base.BasicState }).count

#Column Creation
$HtmlTable = "<table border='1' align='Left' cellpadding='2' cellspacing='0' style='color:black;font-family:arial,helvetica,sans-serif;text-align:left;'>
<tr style ='font-size:13px;font-weight: normal;background: #0789f7'>
<th align=left><b>Concurrent Users</b></th>
<th align=left><b>Error Machines Count</b></th>
<th align=left><b>Error Machines</b></th>
</tr>"

# Insert data
Get-HVMachineSummary | where { 'ERROR' -contains $_.Base.BasicState } | 
ForEach-Object -Process {
    $HtmlTable += "<tr style='font-size:13px;background-color:#FFFFFF'>
<td><center>" + $concurrent_users + "</center></td>
<td><center>" + $error_machines_count + "</center></td>
<td>Machine: $($_.Base.Name) present in Desktoppool: $($_.Base.DesktopName)</td>
</tr>"
}

$HtmlTable += "</table>"

Disconnect-HVServer -Confirm:$false

# Send Mail Inputs
$smtpserver = *******************
$from = *******************
$to = *******************
$subject = *******************
$body = $HtmlTable

Send-MailMessage -SmtpServer $smtpserver -From $from -To $to -Subject $subject -Body $body -BodyAsHtml

But that also means that the $concurrent_users and $error_machines_count number will appear on each line in the table.
Perhaps you should move that outside of the table?


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

0 Kudos
jamie20
Enthusiast
Enthusiast
Jump to solution

Something missing LucD....I get only the column headers in the output....

jamie20_1-1615928543512.png

Also if it is possible move the third column outside of the table.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You did notice that the Disconnect is now behind the Foreach loop?

Not sure what you mean by "outside of the table"?


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

0 Kudos
jamie20
Enthusiast
Enthusiast
Jump to solution

Yes LucD....I noticed that....But now am get only empty values.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The quotes might have been incorrect, give it a try with this version.

Connect-HVServer -Server 10.0.0.1 -User vcadmin -Password welcome -Domain company.domain

$concurrent_users = ((get-hvlocalsession).sessiondata | Where-Object { ($_.sessionstate -eq "connected") -and ($_.sessiontype -eq "desktop") }).count
$error_machines_count = (Get-HVMachineSummary | where { 'ERROR' -contains $_.Base.BasicState }).count

#Column Creation
$HtmlTable = "<table border='1' align='Left' cellpadding='2' cellspacing='0' style='color:black;font-family:arial,helvetica,sans-serif;text-align:left;'>
<tr style ='font-size:13px;font-weight: normal;background: #0789f7'>
<th align=left><b>Concurrent Users</b></th>
<th align=left><b>Error Machines Count</b></th>
<th align=left><b>Error Machines</b></th>
</tr>"

# Insert data
Get-HVMachineSummary | where { 'ERROR' -contains $_.Base.BasicState } | 
ForEach-Object -Process {
    $HtmlTable += "<tr style='font-size:13px;background-color:#FFFFFF'>" +
        "<td><center>" + $concurrent_users + "</center></td>" +
        "<td><center>" + $error_machines_count + "</center></td>" +
        "<td>Machine: $($_.Base.Name) present in Desktoppool: $($_.Base.DesktopName)</td>" +
        "</tr>"
}

$HtmlTable += "</table>"

Disconnect-HVServer -Confirm:$false

# Send Mail Inputs
$smtpserver = *******************
$from = *******************
$to = *******************
$subject = *******************
$body = $HtmlTable

Send-MailMessage -SmtpServer $smtpserver -From $from -To $to -Subject $subject -Body $body -BodyAsHtml


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

0 Kudos
jamie20
Enthusiast
Enthusiast
Jump to solution

No LucD...Still the values are empty....:)

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Strange 🤔

You did check that the following is returning something?

Get-HVMachineSummary | where { 'ERROR' -contains $_.Base.BasicState } | 
ForEach-Object -Process {
    Write-Host "Machine: $($_.Base.Name) present in Desktoppool $($_.Base.DesktopName)"
}


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

0 Kudos
jamie20
Enthusiast
Enthusiast
Jump to solution

Yes LucD...I checked and this returns the values as we expect....

I wonder why first two columns are aslo not working....

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Probably a HTML thing.
Can you attach the generated HTML as a file?


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

0 Kudos
jamie20
Enthusiast
Enthusiast
Jump to solution

Yes LucD....

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The loop doesn't seem to be executed.
I just added a Write-Host to see if the foreach loop is entered at all.
If that doesn't show up in the console, I'm lost.

Connect-HVServer -Server 10.0.0.1 -User vcadmin -Password welcome -Domain company.domain

$concurrent_users = ((get-hvlocalsession).sessiondata | Where-Object { ($_.sessionstate -eq "connected") -and ($_.sessiontype -eq "desktop") }).count
$error_machines_count = (Get-HVMachineSummary | where { 'ERROR' -contains $_.Base.BasicState }).count

#Column Creation
$HtmlTable = "<table border='1' align='Left' cellpadding='2' cellspacing='0' style='color:black;font-family:arial,helvetica,sans-serif;text-align:left;'>
<tr style ='font-size:13px;font-weight: normal;background: #0789f7'>
<th align=left><b>Concurrent Users</b></th>
<th align=left><b>Error Machines Count</b></th>
<th align=left><b>Error Machines</b></th>
</tr>"

# Insert data
Get-HVMachineSummary | where { 'ERROR' -contains $_.Base.BasicState } | 
ForEach-Object -Process {
    Write-Host "Machine: $($_.Base.Name) present in Desktoppool $($_.Base.DesktopName)"
    
    $HtmlTable += "<tr style='font-size:13px;background-color:#FFFFFF'>" +
        "<td><center>" + $concurrent_users + "</center></td>" +
        "<td><center>" + $error_machines_count + "</center></td>" +
        "<td>Machine: $($_.Base.Name) present in Desktoppool: $($_.Base.DesktopName)</td>" +
        "</tr>"
}

$HtmlTable += "</table>"

Disconnect-HVServer -Confirm:$false

# Send Mail Inputs
$smtpserver = *******************
$from = *******************
$to = *******************
$subject = *******************
$body = $HtmlTable

Send-MailMessage -SmtpServer $smtpserver -From $from -To $to -Subject $subject -Body $body -BodyAsHtml




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

0 Kudos
jamie20
Enthusiast
Enthusiast
Jump to solution

No lose for LucD....Now its working....But dont know how😄

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Probably a dirty bit somewhere in the datacenter 😁


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

0 Kudos