Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to block adding of a network drive on a TS server

Status
Not open for further replies.

Rust2

IS-IT--Management
Feb 8, 2008
5
US
I have a script that performs 3 things for a user on their workstation:
1) maps a printer
2) sets an environment variable
3) maps a drive letter "P:"

This "P:" drive is mapped from a TS server that the user also has the ability to log into when they are away remotely. On the TS server there is a physical drive "P:" that is the same drive. The script is supposed to block the "P:" mapping when they log in remotely through TS but it errors on the if then statement. Is there a easy way to fix this? Here is the script:

objNetwork.AddWindowsPrinterConnection "\\SVCTAG-DXB78F1\SAVIN802"
Set oShell = WScript.createObject("WScript.Shell")
Set oEnv = oShell.Environment("USER")
oEnv("PP_PRINT") = "SAVIN802"
Set objNetwork = Createobject("wscript.Network")
If not exists "P:" Then
objNetwork.MapNetworkDrive "P:" , "\\SVCTAG-DXB78F1\Data"
End IF

Thanks for any insight, JR
 
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.DriveExists("P") Then
Set objNetwork = CreateObject("wscript.Network")
objNetwork.MapNetworkDrive "P:", "\\SVCTAG-DXB78F1\Data"
End If

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
That fixed the drive mapping issue, but I guess I should have realized that the printer add would create the same problem since it exists on the server as well. I have commented the printer code out:

Set oShell = WScript.createObject("WScript.Shell")
Set oEnv = oShell.Environment("USER")
oEnv("PP_PRINT") = "SAVIN802"
' objNetwork.AddWindowsPrinterConnection "\\SVCTAG-DXB78F1\SAVIN802"
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.DriveExists("P") Then
Set objNetwork = CreateObject("wscript.Network")
objNetwork.MapNetworkDrive "P:", "\\SVCTAG-DXB78F1\Data"
End If

Do you think there is a way to accomplish the same If then effect with the printer as well? Thanks for the help so far!
 
try this:
Code:
Set oShell = WScript.createObject("WScript.Shell")
Set oEnv = oShell.Environment("USER")
oEnv("PP_PRINT") = "SAVIN802"

If Not DoesPrinterExist("\\SVCTAG-DXB78F1\SAVIN802") then
objNetwork.AddWindowsPrinterConnection "\\SVCTAG-DXB78F1\SAVIN802"
end if

Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.DriveExists("P") Then
  Set objNetwork = CreateObject("wscript.Network")
  objNetwork.MapNetworkDrive "P:", "\\SVCTAG-DXB78F1\Data"
End If

Function DoesPrinterExist(strPrinter)
'if the printer is a remote printer use \\printserver\printer for the name

strComputer = "."

FindPrinter = False

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colInstalledPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_Printer")

on error resume next
For Each objPrinter in colInstalledPrinters

printername = objPrinter.Name
If Ucase(printername) = Ucase(strPrinter) then
DoesPrinterExist = True
end if

Next
	
end Function
 
oops i made a typo in the function where it says FindPrinter = False needs to be DoesPrinterExist = False
 
I can follow the logic of your code, and I added your typo change but the script errors on Line 6, Char 1:
Error Object required 'objNetwork'
Code 800A01A8

Could it be the version of Server? I'm running Server 2003 R2.

Set oShell = WScript.createObject("WScript.Shell")
Set oEnv = oShell.Environment("USER")
oEnv("PP_PRINT") = "SAVIN802"

If Not DoesPrinterExist("\\SVCTAG-DXB78F1\SAVIN802") then
objNetwork.AddWindowsPrinterConnection "\\SVCTAG-DXB78F1\SAVIN802"
End if

Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.DriveExists("P") Then
Set objNetwork = CreateObject("wscript.Network")
objNetwork.MapNetworkDrive "P:", "\\SVCTAG-DXB78F1\Data"
End If

Function DoesPrinterExist(strPrinter)
'if the printer is a remote printer use \\printserver\printer for the name

strComputer = "."

DoesPrinterExist = False

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer")

on error resume next
For Each objPrinter in colInstalledPrinters

printername = objPrinter.Name
If Ucase(printername) = Ucase(strPrinter) then
DoesPrinterExist = True
end if

Next

end Function
 
You missed this line:
Set objNetwork = CreateObject("wscript.Network")

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks a bunch guys! That seems to have done the trick. In case anybody happens accross this and finds it usefull, its important that the printer name and share name are the same. Current working code:
Code:
Set oShell = WScript.createObject("WScript.Shell")
Set oEnv = oShell.Environment("USER")
oEnv("PP_PRINT") = "SAVIN802"

If Not DoesPrinterExist("SAVIN802") then
  Set objNetwork = CreateObject("wscript.Network")
  objNetwork.AddWindowsPrinterConnection "\\SVCTAG-DXB78F1\SAVIN802"
End if

Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.DriveExists("P") Then
  Set objNetwork = CreateObject("wscript.Network")
  objNetwork.MapNetworkDrive "P:", "\\SVCTAG-DXB78F1\Data"
End If

Function DoesPrinterExist(strPrinter)
'if the printer is a remote printer use \\printserver\printer for the name

strComputer = "."

DoesPrinterExist = False

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colInstalledPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_Printer")

on error resume next
For Each objPrinter in colInstalledPrinters

printername = objPrinter.Name
If Ucase(printername) = Ucase(strPrinter) then
DoesPrinterExist = True
end if

Next
    
end Function
 
One other thing that I can't seem to find good direction on is a user selected environment varible. In the first part of the script a "PP_PRINT" user variable is set for each user. In my environment this is determined by your location, I have a handfull of users who could be working in a different location. Would there be a way to prompt the user for them to pick their location and then have it set the associated varible:

Choose your location:

1) Eastside
2) Westside
3) Central

Eastside: PP_PRINT=Savin802
Westside: PP_PRINT=Savin802B
Central: PP_PRINT=SAVIN802C

For error handling purposes I guess it would need to continue to prompt the user if he selects something other than 1, 2, or 3 and time out to a default of 1 after 10 seconds. Is this something that can be included in this script or should it call another script?

Code:
' Sets PP_PRINT environment variable in users session on workstation and TS session
Set oShell = WScript.createObject("WScript.Shell")
Set oEnv = oShell.Environment("USER")
oEnv("PP_PRINT") = "SAVIN802"

' Adds savin printer on local workstation and not in TS session
If Not DoesPrinterExist("SAVIN802") then
  Set objNetwork = CreateObject("wscript.Network")
  objNetwork.AddWindowsPrinterConnection "\\SVCTAG-DXB78F1\SAVIN802"
End if

' Adds P drive on local workstation and not in TS session
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.DriveExists("P") Then
  Set objNetwork = CreateObject("wscript.Network")
  objNetwork.MapNetworkDrive "P:", "\\SVCTAG-DXB78F1\Data"
End If

Function DoesPrinterExist(strPrinter)
'if the printer is a remote printer use \\printserver\printer for the name

strComputer = "."

DoesPrinterExist = False

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colInstalledPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_Printer")

on error resume next
For Each objPrinter in colInstalledPrinters

printername = objPrinter.Name
If Ucase(printername) = Ucase(strPrinter) then
DoesPrinterExist = True
end if

Next
    
end Function
 
Well here is a different approach on that. Just create 3 scripts that sets the enviorment variable, one for each region accordingly and set each on in a Group Policy and apply the respective GPO to the respective group of users. Does that make sense to you?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top