mhmservices
asked on
printer Add/Delete Batch Script not working
I'm working on a vb batch script that adds and deletes printers. I'm not much of a scripter but have pieced together the code below. It seems that the deletion portion works as well as the skip portion but the install portion isn't working.
'On Error Resume Next
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Const Version = 1
Const strComputer = "."
Const strResistryLoc = "SOFTWARE\Helpdesk"
Const strTestRegistry = "map-printers"
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Dim DoNotDeleteArray
DoNotDeleteArray = Array("\\hqfile1\HQFinance ","\\hqfil e1\HQ IT","\\hqfile1\HQMICR1","\ \hqfile1\H Q Payroll HP P3005","\\hqfile1\HQPayrol l","\\hqfi le1\HQ_Par oll_2")
if NOT FindRegKey then
'Delete all network printers
DeleteNetworkPrinters()
'USAGE: MapPrinter "\\SERVER\PRINTER SHARE", "True"
MapPrinter "\\hqfile1\hqguest", "False"
MapPrinter "\\hqfile1\hq_ap", "False"
MapPrinter "\\hqfile1\itcopier", "False"
MapPrinter "\\hqfile1\hqrecruiting", "False"
MapPrinter "\\hqfile1\hqmarketing", "False"
MapPrinter "\\hqfile1\hrcopier", "False"
MapPrinter "\\hqfile1\hqreceiving", "False"
CreateRegKey()
End if
Sub DeleteNetworkPrinters()
On Error Resume Next
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("S ELECT DeviceID FROM Win32_Printer WHERE Local='False'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
Set objNetwork = CreateObject("WScript.Netw ork")
For Each objItem In colItems
strPrinter = objItem.DeviceID
'wscript.echo strPrinter
if NOT InArray(strPrinter,DoNotDe leteArray) then
objNetwork.RemovePrinterCo nnection objItem.DeviceID, True, True
end if
Next
End Sub
Sub MapPrinter(sPrinterPath,sP rinterDefa ult)
On Error Resume Next
WSHNetwork.AddWindowsPrint erConnecti on sPrinterPath
WScript.Sleep 300
If sPrinterDefault = "1" Or sPrinterDefault = UCase("TRUE") Then
WSHNetwork.SetDefaultPrint er sPrinterPath
End If
End Sub
Function FindRegKey
Set objReg = GetObject("winmgmts:{imper sonationLe vel=impers onate}!\\" & strComputer & "\root\default:StdRegProv" )
strKeyPath = strResistryLoc
strValueName = strTestRegistry
objReg.GetStringValue HKEY_CURRENT_USER,strKeyPa th,strValu eName,strV alue
if IsNull(strValue) then
strValue = 0
end if
if Version > CInt(strValue) then
FindRegKey = False
else
FindRegKey = True
end if
End Function
Sub CreateRegKey
Set objReg = GetObject("winmgmts:{imper sonationLe vel=impers onate}!\\" & strComputer & "\root\default:StdRegProv" )
strKeyPath = strResistryLoc
strValueName = strTestRegistry
strValue = CSTR(Version)
Return = objReg.CreateKey(HKEY_CURR ENT_USER, strKeyPath)
objReg.SetStringValue HKEY_CURRENT_USER,strKeyPa th,strValu eName,strV alue
End Sub
Function InArray(item,A)
Dim i
For i=0 To UBound(A) Step 1
If A(i) = item Then
InArray=True
Exit Function
End If
Next
InArray=False
End Function
'On Error Resume Next
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Const Version = 1
Const strComputer = "."
Const strResistryLoc = "SOFTWARE\Helpdesk"
Const strTestRegistry = "map-printers"
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Dim DoNotDeleteArray
DoNotDeleteArray = Array("\\hqfile1\HQFinance
if NOT FindRegKey then
'Delete all network printers
DeleteNetworkPrinters()
'USAGE: MapPrinter "\\SERVER\PRINTER SHARE", "True"
MapPrinter "\\hqfile1\hqguest", "False"
MapPrinter "\\hqfile1\hq_ap", "False"
MapPrinter "\\hqfile1\itcopier", "False"
MapPrinter "\\hqfile1\hqrecruiting", "False"
MapPrinter "\\hqfile1\hqmarketing", "False"
MapPrinter "\\hqfile1\hrcopier", "False"
MapPrinter "\\hqfile1\hqreceiving", "False"
CreateRegKey()
End if
Sub DeleteNetworkPrinters()
On Error Resume Next
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("S
Set objNetwork = CreateObject("WScript.Netw
For Each objItem In colItems
strPrinter = objItem.DeviceID
'wscript.echo strPrinter
if NOT InArray(strPrinter,DoNotDe
objNetwork.RemovePrinterCo
end if
Next
End Sub
Sub MapPrinter(sPrinterPath,sP
On Error Resume Next
WSHNetwork.AddWindowsPrint
WScript.Sleep 300
If sPrinterDefault = "1" Or sPrinterDefault = UCase("TRUE") Then
WSHNetwork.SetDefaultPrint
End If
End Sub
Function FindRegKey
Set objReg = GetObject("winmgmts:{imper
strKeyPath = strResistryLoc
strValueName = strTestRegistry
objReg.GetStringValue HKEY_CURRENT_USER,strKeyPa
if IsNull(strValue) then
strValue = 0
end if
if Version > CInt(strValue) then
FindRegKey = False
else
FindRegKey = True
end if
End Function
Sub CreateRegKey
Set objReg = GetObject("winmgmts:{imper
strKeyPath = strResistryLoc
strValueName = strTestRegistry
strValue = CSTR(Version)
Return = objReg.CreateKey(HKEY_CURR
objReg.SetStringValue HKEY_CURRENT_USER,strKeyPa
End Sub
Function InArray(item,A)
Dim i
For i=0 To UBound(A) Step 1
If A(i) = item Then
InArray=True
Exit Function
End If
Next
InArray=False
End Function
Sorry, this is not batch script; it is VBScript.
You didn't create the WSHNetwork object before you used it. To be more consistent, you could change it to objNetwork as used earlier in the script.
It would also be better to remove the quotes from "False" when calling the the MapPrinter subroutine and change its usage from a string to a binary value as below.
It would also be better to remove the quotes from "False" when calling the the MapPrinter subroutine and change its usage from a string to a binary value as below.
Sub MapPrinter(sPrinterPath, blnPrinterDefault)
On Error Resume Next
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection sPrinterPath
WScript.Sleep 300
If blnPrinterDefault = True Then
objNetwork.SetDefaultPrinter sPrinterPath
End If
End Sub
ASKER
Shift-3,
Thank you for your help. Should I just replace my sub mapprinter with what you posted? Is it that simple?
Thank you for your help. Should I just replace my sub mapprinter with what you posted? Is it that simple?
That, and removing the quotes in the earlier section, would fix this specific problem.
'USAGE: MapPrinter "\\SERVER\PRINTER SHARE", True
MapPrinter "\\hqfile1\hqguest", False
MapPrinter "\\hqfile1\hq_ap", False
MapPrinter "\\hqfile1\itcopier", False
MapPrinter "\\hqfile1\hqrecruiting", False
MapPrinter "\\hqfile1\hqmarketing", False
MapPrinter "\\hqfile1\hrcopier", False
MapPrinter "\\hqfile1\hqreceiving", False
ASKER
Shift-3,
I will make those changes and test agian. I may not be able to test until tomorrow but I'll post back with my results.
I will make those changes and test agian. I may not be able to test until tomorrow but I'll post back with my results.
ASKER
Shift-3,
I've been testing the script and it seems to work well although it maps an unrecognized printer. It shows up as "HQ on hq" which is wrong. I'm going back through to see if I can figure out which entry is causing this.
I have a question also, is there a way to allow the user to select which printer they want as default?
I've been testing the script and it seems to work well although it maps an unrecognized printer. It shows up as "HQ on hq" which is wrong. I'm going back through to see if I can figure out which entry is causing this.
I have a question also, is there a way to allow the user to select which printer they want as default?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Great information, my questions are thus:
1. Is that script ready to go our would I need to customize it to my situation any?
2. Would it be better to make that code its own script and call it from the other one or embed it at the end of my existing script?
1. Is that script ready to go our would I need to customize it to my situation any?
2. Would it be better to make that code its own script and call it from the other one or embed it at the end of my existing script?