I'm trying to figure out how to view the properties of Azure AVD session hosts via script. Instead, all I'm getting is a serialized object. How do I get it to output the properties? I've tried Format-List
, but that has no effect.
$resourceGroup = "Group1"
$hostPoolObjects = Get-AzWvdHostPool -ResourceGroupName $resourceGroup
foreach ($hostPool in $hostPoolObjects) {
$sessionHosts = Get-AzWvdSessionHost -HostPoolName $hostPool.Name -ResourceGroupName $resourceGroup
foreach ($sessionHost in $sessionHosts) {
Write-Host $sessionHost
}
}
}
Output:
Microsoft.Azure.PowerShell.Cmdlets.DesktopVirtualization.Models.Api202209.SessionHost
Microsoft.Azure.PowerShell.Cmdlets.DesktopVirtualization.Models.Api202209.SessionHost
Microsoft.Azure.PowerShell.Cmdlets.DesktopVirtualization.Models.Api202209.SessionHost
Microsoft.Azure.PowerShell.Cmdlets.DesktopVirtualization.Models.Api202209.SessionHost
Microsoft.Azure.PowerShell.Cmdlets.DesktopVirtualization.Models.Api202209.SessionHost
Microsoft.Azure.PowerShell.Cmdlets.DesktopVirtualization.Models.Api202209.SessionHost
...
What am I missing in the code?
The following general advice applies:
Write-Host
is typically the wrong tool to use, unless the intent is to write to the display only, bypassing the success output stream and with it the ability to send output to other commands, capture it in a variable, or redirect it to a file.
Write-Host
, performs space-separated, single-line output formatting based on simple .ToString()
calls, which with complex objects typically results in unhelpful representations, which is what you saw - see next point.To output a value, use it by itself; e.g, $value
, instead of Write-Host $value
(or use Write-Output $value
, though that is rarely needed); see this answer.
When printing to the display or when implicitly stringifying output for outside callers in PowerShell CLI calls, PowerShell's rich, for-display output-formatting system is used to create the string representations.
In-session, if you truly want to bypass the success output stream while still printing richly formatted representations to the display only, pipe to Out-Host
.
Therefore, the idiomatic reformulation of your code is the following, using pipeline-based parameter binding and relying on the pipeline's auto-enumeration behavior:
Get-AzWvdHostPool -ResourceGroupName Group1 | Get-AzWvdSessionHost
The Get-AzWvdSessionHost
cmdlet's output objects determine the particular shape of the for-display representations, either by virtue of predefined formatting data associated with their type or, in its absence, whether the output objects have 4 or fever (public) properties (in which case Format-Table
is implicitly used) or more (in which case Format-List
is used).
Either way, you're free to pipe to the various Format-*
cmdlets to modify the default for-display representation, but note that using these cmdlets is only ever appropriate for display formatting, not for outputting data.