Just a thought, The reason I had needed the script for connecting to and setting default printers was to provide my support folks with a simple way to adjust users default printers.
This is what is in place currently.
Any other ideas and comments are welcome!!.
--------------------------------------------------------------
This script is used to great AD Global Security groups based on printer Names. (Local IP Printers on each TS application Server)
Code:
On Error Resume Next
' Get Command-line input to get the bank #'
set args = Wscript.Arguments
strFile = args.Item(0)
If strFile = "" Then cmdLineArgError
Const ForReading = 1
Const ADS_GROUP_TYPE_GLOBAL_GROUP = &h2
Const ADS_GROUP_TYPE_SECURITY_ENABLED = &h80000000
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("DefaultNamingContext")
'WScript.Echo "RootDSE: " & objRootDSE
'WScript.Echo "Domain: " & strDNSDomain
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(strFile, ForReading)
Do While objTextFile.AtEndOfStream <> True
strLine = objtextFile.ReadLine
If inStr(strLine, ",") Then
arrDHCPRecord = split(strLine, ",")
i = i + 1
End If
[!] strBank = (Left(arrDHCPRecord(1),3))
' WScript.Echo "LDAP://ou=" & strBank & "," & strDNSDomain
[/!]
Set objOU = GetObject("LDAP://ou=" & strBank & "," & strDNSDomain)
Set objGroup = objOU.Create("Group", "cn=" & arrDHCPRecord(1))
objGroup.Put "sAMAccountName", arrDHCPRecord(1)
objGroup.Put "groupType", ADS_GROUP_TYPE_GLOBAL_GROUP Or _
ADS_GROUP_TYPE_SECURITY_ENABLED
objGroup.SetInfo
Loop
Sub cmdLineArgError()
MsgBox"Please drop the Banks csv file" & vbCrLf &_
"on this script to create the needed" & vbCrLf &_
"security groups to set default printers.",0
If Error = "1" Then MsgBox("Canceled")
End Sub
The Part in red will need to be adapted to needs. We prefix printer Names with Branch numbers so this works like this.
Printer Name = "100 Hp Printer Someware"
In AD, Branch 100 has an OU with it's user objects and groups.
PrinterPorts.cvs looks like this.
Code:
10.191.125.23,100 BR51_Manager,HP LaserJet 5Si
And here's the script I use to make the PrinterPorts.csv file.
Code:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer")
For Each objPrinter in colInstalledPrinters
If objPrinter.local = -1 Then
If objPrinter.PortName > "TS" Then
Else
Add2Log Mid(objPrinter.PortName,4,15) & "," &_
objPrinter.DeviceID & "," &_
objPrinter.DriverName
End If
End If
Next
'objRecordSet.MoveNext
'Loop
Sub Add2Log (txt) ' txt is the text we deliver into the sub
' Declare the log file name
Const Myfile = ".\PrinterPorts.csv" ' Log file name
' Open it for Append
Const ForAppending = 8 ' Append mode
' Declare the FileSystemObject and File variables
' Create a new FileSystemObject object
Set fso = CreateObject("Scripting.FileSystemObject")
' Open the file and force creation, if it doesn't exist already
Set file = fso.OpenTextFile(MyFile, ForAppending, TRUE)
file.WriteLine (txt) ' append log
' Clean up
Set file = Nothing
End Sub
Then this section of code is used to set the default printer and allow for an external script if the client has special needs.
Code:
For Each GroupObj In UserObj.Groups
' WScript.Echo UCase(GroupObj.Name) ' (Insert echo command for troubleshooting)
sExePath = "cscript //nologo "
sSwitches0 = "\\corebanks\SYSVOL\corebanks.jackhenry.com\scripts\"
sFile1 = GroupObj.Name & " Printers.vbs"
If fso.FileExists(sSwitches0 & sFile1) = True Then
' WScript.Echo "Found it."
WshShell.Run sExePath & Chr(34) & sSwitches0 & sFile1 & Chr(34), 2, True
Exit For
Else
' WScript.Echo "Setting Local Default Printer."
WSHNetwork.SetDefaultPrinter GroupObj.Name
End If
Next
Just seemed like this solution may come in handy for Dbyte.
Oh, and the earlier posted printer script, which db4ever so graciously IMPROVED, was when we were attempting to deploy a print server which causes the need for the ability to run an external script.
Hope someone finds this useful.
Thanks
John Fuhrman
Titan Global Services