1

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?

2
  • write-host converts to string Commented Nov 27, 2024 at 1:23
  • @js2010 AH! Thank you! Piping the output to Format-List gives me exactly what I was looking for. Commented Dec 2, 2024 at 17:41

2 Answers 2

1

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.

    • Additionally, when you pass a collection (something that is enumerable) by argument, 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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I'll keep this in mind. Once I have the script further developed I won't be using Write-Host. I'm only using it for the moment while I sort out what I'm looking for.
@theillien, but even then you're better off with Out-Host (for anything other than instance of .NET primitive types, i.e. strings and numbers, loosely speaking).
0

I've found that piping the output of Get-AzWvdSessionHost to Format-List displays what I'm looking for.

Get-AzWvdSessionHost -HostPoolName $hostPool.Name -ResourceGroupName $resourceGroup | Format-List

Output:

AgentVersion                 : 1.0.8431.5100
AllowNewSession              : True
AssignedUser                 : 
FriendlyName                 : 
HealthCheckResult            : {}
Id                           : /subscriptions/sub/resourcegroups/rgroup1/providers/Microsoft.DesktopVirtualization/hostpools/CACSTST-Host 
                               pool-0702/sessionhosts/680702.local
LastHeartBeat                : 8/19/2024 5:13:19 PM
LastUpdateTime               : 8/17/2024 12:19:49 AM
Name                         : Hostpool-0702/680702.local
OSVersion                    : 10.0.19045.4412
ObjectId                     : 4d5f998b-7857-4c90-93cf-2ab1693253a1
ResourceId                   : /subscriptions/sub/resourceGroups/rg1/providers/Microsoft.Compute/virtualMachines/ocst68-0702
Session                      : 0
Status                       : Unavailable
StatusTimestamp              : 8/17/2024 12:19:49 AM
SxSStackVersion              : rdp-sxs240209880
SystemDataCreatedAt          : 7/2/2024 8:16:43 PM
SystemDataCreatedBy          : 
SystemDataCreatedByType      : 
SystemDataLastModifiedAt     : 
SystemDataLastModifiedBy     : 
SystemDataLastModifiedByType : 
Type                         : Microsoft.DesktopVirtualization/hostpools/sessionhosts
UpdateErrorMessage           : 
UpdateState                  : Succeeded
VirtualMachineId             : 835638ba-e701-4617-8725-46cdcc0a27a8

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.