Forum Discussion

michelloubier's avatar
michelloubier
Copper Contributor
Feb 26, 2020

Invoke-Command on multiple sessions - Accessing session-specific values within a HashTable

Hi,

we need to execute a script block on multiple servers (eg "S1", "S2" and "S3").

 

The logic is exactly the same on all servers (Create dirs, copy / move/ rename files, etc.)... But, for example, our "BasePath" can differ from one server to another.

 

Our implementation looks like this:

 

 

 

$myConfig = @{"S1"= @{BasePath="c:\someDir"}; "S2"= @{BasePath="c:\someOtherdir"}; "S3"= @{BasePath="D:\yetAnotherDir"} }

$s = new-PSSession -ComputerName "S1","S2","S3"
Invoke-Command -Session $s -ScriptBlock { 
    $serverName = $env:COMPUTERNAME
    $c = $using:myConfig
    $serverConfig = $c[$serverName]
    
    #Do something with $serverConfig.BasePath
    $serverConfig.BasePath
}

 

 

 

 

A hashTable is initialized with various parameters.  Keys are set to the same values as the -ComputerName parameter of the "New-PSSession" commandlet.

 

Within the script block (Invoke-Command), we use the hashTable to retrieve the server-specific value based on the "$env:COMPUTERNAME" value.

 

Here, the code assumes that the supplied name of the server will be the same as the value of "$env:COMPUTERNAME".... For example, if someone pass the fully qulified name of the server, our code will not work.

 

 

When a session is created, a session ID is assigned.  We could use the "session ID" to create a link between the session and our server-specific configuration entry.

 

Is there a way to retrieve the "Session ID" of the "current session" within the "Invoke-Command".  So our code could look like thie

 

 

Invoke-Command -Session $s -ScriptBlock { 
    $sessionID = [Get current Session ID] <<<-------
    $c = $using:myConfig
    $serverConfig = $c[$sessionID]
    
    #Do something with $serverConfig.BasePath
    $serverConfig.BasePath
}

 

 

 

Thanks.

 

  • 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.

     

Resources