Forum Discussion
Invoke-Command on multiple sessions - Accessing session-specific values within a HashTable
- Feb 29, 2020
Hey michelloubier, short answer is to check out the $PSSenderInfo variable.
Longer answer:
$PSSenderInfo is an automatic variable that only exists inside a PSSession and gives you a bunch of info about the session and the originating connection source.
The property inside that variable that will be of the most help is the Connection String:
$PSSenderInfo.ConnectionString
This tells you a lot about the connection, including the port used, but the key here is that the host name matches the name supplied when opening the connection. As an example here's the output and how it changes when connecting to my home server by name vs by IP address:
http://localserver:5985/wsman?PSVersion=5.1.18362.628
http://192.168.1.50:5985/wsman?PSVersion=5.1.18362.628
You could muck around with string manipulation... but it'll be a lot easier to turn it into an System.Uri object and just pull out the host property:
([Uri] $PSSenderInfo.ConnectionString).Host
Bringing this back around to your example, assign that host property to your $serverName variable and you should be good to go.
Hey michelloubier, short answer is to check out the $PSSenderInfo variable.
Longer answer:
$PSSenderInfo is an automatic variable that only exists inside a PSSession and gives you a bunch of info about the session and the originating connection source.
The property inside that variable that will be of the most help is the Connection String:
$PSSenderInfo.ConnectionString
This tells you a lot about the connection, including the port used, but the key here is that the host name matches the name supplied when opening the connection. As an example here's the output and how it changes when connecting to my home server by name vs by IP address:
http://localserver:5985/wsman?PSVersion=5.1.18362.628
http://192.168.1.50:5985/wsman?PSVersion=5.1.18362.628
You could muck around with string manipulation... but it'll be a lot easier to turn it into an System.Uri object and just pull out the host property:
([Uri] $PSSenderInfo.ConnectionString).Host
Bringing this back around to your example, assign that host property to your $serverName variable and you should be good to go.