We use a label printer in our application that is statically mapped within. This is done for speed so that users aren't prompted each time a job is sent. Because terminal server dynamically adds the session id to the end of the printer name it caused a problem. This solution uses a logon script with a batch file and VBscript.
We assign the static portion of the printer name in the config file for the app and append the dynamic portion of the name via the script.
Batch file:
Code:
qwinsta > "%UserProfile%\getsession.txt"
call getsession.vbs
QWINSTA returns the following:
Code:
SESSIONNAME USERNAME ID STATE TYPE DEVICE
console administrator 0 Active wdcon
rdp-tcp 65536 Listen rdpwd
>rdp-tcp#70 administrator 3 Active rdpwd
kcc 2 Disc rdpwd
VBscript
:
Code:
option explicit
on error resume next
dim objFSO, objShell, file1, file2, file3, strSpath, strPath, strFolder, strLine
Set objShell = CreateObject("WScript.Shell")
strSpath = objShell.ExpandEnvironmentStrings("%UserProfile%") & "\GetSession.txt"
strPath = "L:\APPS\LAB\blah.ini" 'This file must exist
strFolder = "L:\APPS\LAB\tmp.txt"
Main
Sub Main()
Copy()
if not file1 is nothing then set file1 = nothing
if not file2 is nothing then set file2 = nothing
if not file3 is nothing then set file3 = nothing
if not objFSO is nothing then set objFSO = nothing
End Sub
Sub Copy()
set objFSO = CreateObject("Scripting.FileSystemObject")
if err.number <> 0 then
exit sub
end if
if not objFSO.FileExists(strPath) then
exit sub
end if
if objFSO.FileExists(strFolder) then
objFSO.DeleteFile(strFolder)
end if
set file1 = objFSO.OpenTextFile(strPath)
if err.number <> 0 then
exit sub
end if
set file2 = objFSO.CreateTextFile(strFolder)
if err.number <> 0 then
exit sub
end if
Do while not file1.AtEndofStream
strLine = file1.ReadLine
select Case Mid(strLine,1,8)
case "LBLPATH2"
file2.WriteLine "LBLPATH2='\\TS-SERVER-1\LABEL (from BCL) in session " & GetSession(strSPath) & "'"
case else
file2.WriteLine strLine
end select
loop
if err.number <> 0 then
file1.close
file2.close
exit sub
end if
file1.close
set file1 = nothing
file2.close
set file2 = nothing
objFSO.DeleteFile strPath, true
objFSO.MoveFile strFolder, strPath
End Sub
Function GetSession(SessionPath)
set objFSO = CreateObject("Scripting.FileSystemObject")
set file3 = objFSO.OpenTextFile(SessionPath)
Do while not file3.AtEndofStream
strLine = file3.ReadLine
select Case Mid(strLine,1,1)
case ">"
GetSession = Trim(Mid(strLine,42,5))
end select
loop
file3.close
set file3 = nothing
End Function