I run a powershell logon script when opening RDS session on Windows 2012 R2 Server. I also run a powershell script in startup folder.
- When asking for
CLIENTNAME
environment variable in the logon script, I get the previously opened session client name, not the current one. If I delete the registryHKCU\Environment\Clientname
property before closing the previous session, I just get a null value for ClientName during logon. - When asking the ClientName variable in the script launched after session is opend, I get the current client name (as expected).
I have two clients :
TestClient1
with IP address10.100.20.201
TestClient2
with IP address10.100.20.202
The RDS server is SRV-XA01
.
I also use a small utility named gettscip.exe
to get the client IP address
I log on TestClient1
then I close the session. Then I start a session from TestClient2
(dates are in french format dd/mm/YYYY
).
Logon script :
29/12/2016 10:57:07 : --- New Session opening on SRV-XA01 ---
29/12/2016 10:57:07 : * Client Name (from env:CLIENTNAME) :
29/12/2016 10:57:10 : * IP address : 10.100.20.202
29/12/2016 10:57:11 : * ClientName from [Environment]::GetEnvironmentVariables("user").ClientName : TestClient1
29/12/2016 10:57:11 : --- Logon script successfully ended ! ---
Second script :
29/12/2016 10:57:25 : --- Script running after session is opened ---
29/12/2016 10:57:25 : * Client Name (from env:CLIENTNAME) : TestClient2
29/12/2016 10:57:25 : --- Script ended ---
In logon script the env:CLIENTNAME
returns a null value, [Environment]::GetEnvironmentVariables("user").ClientName
returns the previous client name. After login, everything is OK.
The IP Address I get is the good one from the current Client.
So why is the ClientName environment variable not updated at the time the logon script is running ?
Edit : scripts code.
Logon script :
$DomainName = "domain.fr"
# Nom de l'utilisateur
$UserName = $env:USERNAME
$ScriptsDir = "\\$DomainName\Scripts"
$LogDir = "$ScriptsDir\ScriptsXA7\Logs"
$global:LogFile = "$LogDir\$UserName.log"
Import-Module "$ScriptsDir\Systeme\Modules\Write-Log"
# Serveur Citrix
$ComputerName = $env:COMPUTERNAME
# Poste client
$ClientName = $env:CLIENTNAME
Write-Log "--- New Session opening on $ComputerName ---"
Write-Log "* Client Name (from env:CLIENTNAME) : $ClientName"
$IPExe = "$PSScriptRoot\IP\gettscip.exe"
# On lance l'exécutable qui va remonter l'adresse IP et on la stocke dans la variable `$ip`
$ip = Invoke-Expression $IPExe
$ip = $ip -replace "WTSClientAddress: ",""
# On place cette adresse IP dans la variable d'environnement `CLIENTIP`
[Environment]::SetEnvironmentVariable("CLIENTIP", $ip, "User")
Write-Log "* IP address : $ip"
Write-Log "* ClientName from [Environment]::GetEnvironmentVariables("user").ClientName : $([Environment]::GetEnvironmentVariables("user").ClientName)"
Write-Log "--- Logon script successfully ended ! ---"
Second Script :
$DomainName = "domain.fr"
# Nom de l'utilisateur
$UserName = $env:USERNAME
$ScriptsDir = "\\$DomainName\Scripts"
$LogDir = "$ScriptsDir\ScriptsXA7\Logs"
$global:LogFile = "$LogDir\$UserName.log"
Import-Module "$ScriptsDir\Systeme\Modules\Write-Log"
# Serveur Citrix
$ComputerName = $env:COMPUTERNAME
# Poste client
$ClientName = $env:CLIENTNAME
Write-Log "--- Script running after session is opened ---"
Write-Log "* Client Name (from env:CLIENTNAME) : $ClientName"
Write-Log "--- Script ended ---"