Skip to content

Instantly share code, notes, and snippets.

@bohack
Created March 20, 2018 16:33
Show Gist options
  • Save bohack/8c9dad393e1275a7aa4f447313ec2d28 to your computer and use it in GitHub Desktop.
Save bohack/8c9dad393e1275a7aa4f447313ec2d28 to your computer and use it in GitHub Desktop.
VDI Printer Script
' Author: Jon Buhagiar
' Date: 10/19/17
' Purpose: This script will check the ViewClient_Broker_Tags. If it is External
' or 10.8x.0.0 (wireless) then it will quit and allow TPAutoConnect to
' redirect the client host default printer. If it is not External or 10.8x
' it is assumed to be Internal, the script will then record the current
' default printer and set the default printer via the regKey. Edit the
' strIPExclude with a CSV value.
'
' Usage: Use a GPO applied to the VDI computers for logon and logoff scripts for
' users, then turn on GPO loopback processing in merged mode. Supply the
' argument of logon or logoff depending on use.
Option Explicit
const HKEY_CURRENT_USER = &H80000001
Dim objWSHShell, objUserEnv
Dim objWMI, objPrinter, colPrinters
Dim objWSHNetwork, objPrinterPath
Dim regKey, strIPExclude, strClientIP
Dim strBrokerTAG, strPrinter, strCommand
Dim aryExIPs, strIPaddr
Dim objShell, objFSO, strHomeFolder, dbFile, bolDebug, strComputer
Const fsoForAppend = 8
'Set Debugging
bolDebug = True
If bolDebug Then
Set objShell = CreateObject("WScript.Shell")
strHomeFolder = objShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\.TPPrint.log"
strComputer = objShell.ExpandEnvironmentStrings("%COMPUTERNAME%")
Set objFSO = CreateObject("Scripting.FileSystemObject")
If NOT (objFSO.FileExists (strHomeFolder)) Then
Set dbFile = objFSO.CreateTextFile(strHomeFolder, True)
Else
Set dbFile = objFSO.OpenTextFile(strHomeFolder, fsoForAppend)
End If
dbFile.WriteLine(Now() & " ----------------")
dbFile.WriteLine(strComputer)
dbFile.WriteLine(Now() & " " & WScript.Arguments.Item(0))
End If
'User Set Variables
regKey = "HKCU\Printers\VMViewDefault"
strIPExclude = "10.8,10.0,192."
aryExIPs = split(strIPExclude,",")
Set objUserEnv = GetObject( "winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
objUserEnv.GetStringValue HKEY_CURRENT_USER,"Volatile Environment","ViewClient_Broker_Tags",strBrokerTAG
objUserEnv.GetStringValue HKEY_CURRENT_USER,"Volatile Environment","ViewClient_IP_Address",strClientIP
If bolDebug Then dbFile.WriteLine(Now() & " Broker Tag = " & strBrokerTAG) 'debug
If bolDebug Then dbFile.WriteLine(Now() & " Client IP = " & strClientIP) 'debug
' Check if VDI session is external or internal and if IP matches.
If len(strBrokerTAG) > 1 Then
If lcase(left(strBrokerTAG,8)) = "external" Then
If bolDebug Then dbFile.WriteLine(Now() & " Tag external - exiting") 'debug
Set objUserEnv = Nothing
Wscript.Quit
End If
' Check if VDI Client IP address matches exclusion.
For Each strIPaddr in aryExIPs
If left(strClientIP,len(strIPaddr))=strIPaddr then
If bolDebug Then dbFile.WriteLine(Now() & " In exclusion array - exiting") 'debug
Set objUserEnv = Nothing
Wscript.Quit
End If
Next
' Check what to do when the script is called.
Select Case lcase(WScript.Arguments.Item(0))
Case "logon"
WScript.Sleep 10000
If bolDebug Then dbFile.WriteLine(Now() & " Calling reset default printer sub") 'debug
Call ResetDefaultPrinter()
Case "logoff"
If bolDebug Then dbFile.WriteLine(Now() & " Calling recording printer sub") 'debug
Call RecordDefaultPrinter()
End Select
End If
Set objUserEnv = Nothing
Set objWSHShell = Nothing
If bolDebug Then
dbFile.Close
Set objFSO = Nothing
Set objShell = Nothing
End If
Sub RecordDefaultPrinter()
' Get the default printer and write it to regKey
Set objWSHShell = CreateObject( "WScript.Shell" )
Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
Set colPrinters = objWMI.ExecQuery("SELECT * FROM Win32_Printer")
For Each objPrinter in colPrinters
If objPrinter.Default = TRUE Then
If bolDebug Then dbFile.WriteLine(Now() & " Record default printer recorded = " & objPrinter.Name) 'debug
objWshShell.RegWrite regKey,objPrinter.Name,"REG_SZ"
If bolDebug Then dbFile.WriteLine(Now() & " Recorded = Success") 'debug
End If
Next
Set objWMI = Nothing
Set colPrinters = Nothing
If bolDebug Then dbFile.WriteLine(Now() & " Exiting...") 'debug
End Sub
Sub ResetDefaultPrinter()
' This sets the printer back to the prior default
On Error Resume Next
Set objWSHShell = CreateObject( "WScript.Shell" )
Set objWSHNetwork = WScript.CreateObject("WScript.Network")
strPrinter = objWshShell.RegRead(regKey)
If bolDebug Then dbFile.WriteLine(Now() & " Read default printer = " & strPrinter) 'debug
objWSHNetwork.AddWindowsPrinterConnection(strPrinter)
strCommand = "rundll32 printui.dll,PrintUIEntry /y /n " & "" & strPrinter & ""
If bolDebug Then dbFile.WriteLine(Now() & " Set default printer set to = " & strPrinter) 'debug
objWSHShell.run strCommand,0,FALSE
Set objPrinterPath = Nothing
Set objWSHNetwork = Nothing
If bolDebug Then dbFile.WriteLine(Now() & " Recorded = Success") 'debug
On Error Goto 0
If bolDebug Then dbFile.WriteLine(Now() & " Exiting...") 'debug
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment