Client IPaddress not showing

Jun 4, 2014 at 6:18 PM
Can anyone explain the following? When I issue a query using the get-tssession command, I can see the client IP address. If I use a variable to save the query, I don't see the IP address:
PS D:\Users\user1\Documents\Scripts> $computer = "server1"
PS D:\Users\user1\Documents\Scripts> get-tssession -ComputerName $computer -ErrorAction SilentlyContinue

Server    SessionID State      IPAddress    ClientName  WindowStationName UserName
------    --------- -----      ---------    ----------  ----------------- --------
server1   0         Connected                           Console
server1   65536     Listening                           RDP-Tcp
server1   1         Active     10.20.19.200 CLIENTWS1   RDP-Tcp#6         THEDOMAIN\user1


PS D:\Users\user1\Documents\Scripts> $results = get-tssession -ComputerName $computer -ErrorAction SilentlyContinue
PS D:\Users\user1\Documents\Scripts> $results

Server    SessionID State      IPAddress    ClientName  WindowStationName UserName
------    --------- -----      ---------    ----------  ----------------- --------
server1   0         Connected                           Console
server1   65536     Listening                           RDP-Tcp
server1   1         Active                  CLIENTWS1   RDP-Tcp#6         THEDOMAIN\user1
Coordinator
Jun 6, 2014 at 8:54 AM
I never noticed this before and it happens to me as well.
If you need a workaround, pipe the command to the select-object cmdlet and select all properties.

$results = get-tssession -ComputerName $computer -ErrorAction SilentlyContinue | select-object *
Aug 21, 2014 at 8:14 PM
It's because ClientIPAddress isn't a string. It's an object with the following properties:
Address            : 2030741696
AddressFamily      : InterNetwork
ScopeId            :
IsIPv6Multicast    : False
IsIPv6LinkLocal    : False
IsIPv6SiteLocal    : False
IsIPv6Teredo       : False
IsIPv4MappedToIPv6 : False
IPAddressToString  : 192.168.10.121
It should work if you call ClientIPAddress.IPAddressToString
Aug 27, 2014 at 12:43 PM
Edited Aug 27, 2014 at 1:05 PM
As a further example, I call it this way:
function Convert-DateString ([String]$Date, [String]$Format) {return ([datetime]::ParseExact($Date, "dddd, MMMM d, yyyy h:mm:ss tt", $null)).ToString($Format)}
$sessions=@{}
$sessionNumber = 1
Import-Module PSTerminalServices
get-tsservers -WarningAction silentlyContinue | % {$_.ServerName} | % {get-tssession -ComputerName $_ -Filter {$_.username -eq $user}} | % {
    $session = New-Object PSObject -Prop (@{
        'PC'=$_.ClientName;
        'IP'=$_.ClientIPAddress.IPAddressToString;
        'User'=$_.UserName;
        'Status'=$_.ConnectionState;
        'Server'=$_.Server.ServerName;
        'ConnectTime'=$_.ConnectTime.DateTime;
        'LastInputTime'=$_.LastInputTime.DateTime
    })
    $sessions.add($sessionNumber,$session)
    $sessionNumber += 1
}
    
$table = `
    @{Label='#';Expression={$_.Name}}, `
    @{Label='User';Expression={$sessions.Get_Item($_.Name).User}}, `
    @{Label='IP';Expression={$sessions.Get_Item($_.Name).IP}}, `
    @{Label='PC';Expression={$sessions.Get_Item($_.Name).PC}}, `
    @{Label='Server';Expression={$sessions.Get_Item($_.Name).Server}}, `
    @{Label='Status';Expression={$sessions.Get_Item($_.Name).Status}}, `
    @{Label='ConnectTime';Expression={Convert-DateString -Date $sessions.Get_Item($_.Name).ConnectTime -Format "MM/dd/yyyy hh:mm:ss tt"}}, `
    @{Label='LastInputTime';Expression={Convert-DateString -Date $sessions.Get_Item($_.Name).LastInputTime -Format "MM/dd/yyyy hh:mm:ss tt"}}
$sessions.GetEnumerator() | Sort-Object -Property '#' | Format-Table $table -AutoSize
That will make a table showing all RDS sessions for a specific $user.
(I'm unsure why "$sessionNumber [plus]= 1" is showing up as "$sessionNumber += 1" above... but that will need to be fixed if you're copying and pasting the code.)