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?
Try
Select-Object -ExpandProperty Name
or else
Select-Object @{N='Name';E={$_.Name}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try
Select-Object -ExpandProperty Name
or else
Select-Object @{N='Name';E={$_.Name}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks LucD...
Select-Object @{N='Name';E={$_.Name}}
This didnt worked. Output as above.
Select-Object -ExpandProperty Name
This works perfectly.
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
......
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
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 Users | Error Machines Count | Error Machines |
100 | 2 | Machine: 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...:(
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
Something missing LucD....I get only the column headers in the output....
Also if it is possible move the third column outside of the table.
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
Yes LucD....I noticed that....But now am get only empty values.
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
No LucD...Still the values are empty....:)
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
Yes LucD...I checked and this returns the values as we expect....
I wonder why first two columns are aslo not working....
Probably a HTML thing.
Can you attach the generated HTML as a file?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
No lose for LucD....Now its working....But dont know how😄
Probably a dirty bit somewhere in the datacenter 😁
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference