- Jun 4, 2003
- 2,417
- 0
- 0
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","\\hqfile1\HQ IT","\\hqfile1\HQMICR1","\\hqfile1\HQ Payroll HP P3005","\\hqfile1\HQPayroll","\\hqfile1\HQ_Paroll_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("SELECT DeviceID FROM Win32_Printer WHERE Local='False'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
Set objNetwork = CreateObject("WScript.Network")
For Each objItem In colItems
strPrinter = objItem.DeviceID
'wscript.echo strPrinter
if NOT InArray(strPrinter,DoNotDeleteArray) then
objNetwork.RemovePrinterConnection objItem.DeviceID, True, True
end if
Next
End Sub
Sub MapPrinter(sPrinterPath,sPrinterDefault)
On Error Resume Next
WSHNetwork.AddWindowsPrinterConnection sPrinterPath
WScript.Sleep 300
If sPrinterDefault = "1" Or sPrinterDefault = UCase("TRUE") Then
WSHNetwork.SetDefaultPrinter sPrinterPath
End If
End Sub
Function FindRegKey
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = strResistryLoc
strValueName = strTestRegistry
objReg.GetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strValue
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:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = strResistryLoc
strValueName = strTestRegistry
strValue = CSTR(Version)
Return = objReg.CreateKey(HKEY_CURRENT_USER, strKeyPath)
objReg.SetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strValue
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","\\hqfile1\HQ IT","\\hqfile1\HQMICR1","\\hqfile1\HQ Payroll HP P3005","\\hqfile1\HQPayroll","\\hqfile1\HQ_Paroll_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("SELECT DeviceID FROM Win32_Printer WHERE Local='False'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
Set objNetwork = CreateObject("WScript.Network")
For Each objItem In colItems
strPrinter = objItem.DeviceID
'wscript.echo strPrinter
if NOT InArray(strPrinter,DoNotDeleteArray) then
objNetwork.RemovePrinterConnection objItem.DeviceID, True, True
end if
Next
End Sub
Sub MapPrinter(sPrinterPath,sPrinterDefault)
On Error Resume Next
WSHNetwork.AddWindowsPrinterConnection sPrinterPath
WScript.Sleep 300
If sPrinterDefault = "1" Or sPrinterDefault = UCase("TRUE") Then
WSHNetwork.SetDefaultPrinter sPrinterPath
End If
End Sub
Function FindRegKey
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = strResistryLoc
strValueName = strTestRegistry
objReg.GetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strValue
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:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = strResistryLoc
strValueName = strTestRegistry
strValue = CSTR(Version)
Return = objReg.CreateKey(HKEY_CURRENT_USER, strKeyPath)
objReg.SetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strValue
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
