All pastes #793679 Raw Edit

Something

public text v1 · immutable
·published 2007-11-22 14:21 UTC
rendered paste body
On Error Resume Next
strComputer = "."
strMsgTitle = "STS Drive/print map script " 'title for dialog boxes
strResult = "" 'result string for final status dialog box
Set WshNetwork = CreateObject("WScript.Network")
strHostname = WshNetwork.Computername

'see if we are on \\server - if yes, skip to drive mapping 
if strHostname = "SERVER" Then DriveMap()

'---------------------------------------------------------------------
'setup Network Printers
'---------------------------------------------------------------------
'CUSTOMIZE SCRIPT HERE
'(constant) list of network printers that *should* be installed.
'They are listed here as key/item pairs. 
'Key is the printer share path in "quotes"
'Item is set to "notfound" initially
Set colNetPrinters = CreateObject("Scripting.Dictionary")
	colNetPrinters.Add "\\server\KONICA-C450PCL", "notfound"
	colNetPrinters.Add "\\server\HPLaserJ", "notfound"
	colNetPrinters.Add "\\server\HPOffice", "notfound"

'also give the path, in quotes, of the printer you want to set as default.
'It may be one of the printers listed above. 
strDefaultPrinter = "\\server\HPLaserJ"
'END OF USER CUSTOMIZED PARTS

'iterate through locally installed printers
'and mark off the network printers that are found
Set objWMIService = GetObject("winmgmts:" _
	& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters =  objWMIService.ExecQuery _
	("Select * from Win32_Printer")
For Each objPrinter in colInstalledPrinters
	PrinterCounter=PrinterCounter + 1
  'commented out debug section
	'Wscript.Echo
	'Wscript.Echo "Printer " & PrinterCounter
	'Wscript.Echo "  Name: " & objPrinter.Name
	'Wscript.Echo "  Server: " & objPrinter.Servername
	'Wscript.Echo "  Default: " & objPrinter.Default
	'Wscript.Echo "  Sharename: " & objPrinter.Sharename
	strPrintMapName=objPrinter.Servername + "\" + objPrinter.Sharename
	if colNetPrinters.Exists(strPrintMapName) Then
		colNetPrinters.Item(strPrintMapName) = "found"
	end if
	'Catch the 'to be default' printer and save it for the default
	'printer clause below
	if strPrintMapName = strDefaultPrinter Then
		set objDefaultPrinter = objPrinter
	end if
Next

'Now look through list of 'should be installed' printers,
'if any are not already mapped/installed, this is corrected
arrNetPrinters = colNetPrinters.Keys
for each strPrinter in arrNetPrinters
	'debug code commented out
	'wscript.echo strPrinter + " " +  colNetPrinters.Item(strPrinter)
	if colNetPrinters.Item(strPrinter) = "notfound" Then
		Err.Clear
		WshNetwork.AddWindowsPrinterConnection strPrinter
		If Err.Number <> 0 Then
			strResult = strResult & "Problem setting up missing printer: " _
				& strPrinter & vbCR & "ErrorNum: " & Err.Number & vbCR _
				& Err.Description & vbCR
			Else strResult = strResult & "Missing printer " & strPrinter _
				& " was installed successfully." & vbCR
		end If
	end if
next 

'Check and set the Default Printer
if not objDefaultPrinter.Default Then
	Err.Clear
	WshNetwork.SetDefaultPrinter strDefaultPrinter
	If Err.Number <> 0 Then 
		strResult = strResult _
			& "Problem setting strDefaultPrinter as default printer:" _
			& vbCR & "ErrorNum: " & Err.Number & vbCR & Err.Description
	Else strResult = strResult & "Default printer was set to " _
		& strDefaultPrinter &vbCR
	end if
end if
DriveMap()


'---------------------------------------------------------------------
'setup the s-drive share
'---------------------------------------------------------------------
sub DriveMap()
'remove any existing mapping
WshNetwork.RemoveNetworkDrive "S:"
'ignore any errors on de-mapping the S: drive
Err.Clear 
WshNetwork.MapNetworkDrive "s:", "\\server\s-drive"
If Err.Number <> 0 Then 
	strResult = strResult & "Problem mapping the S-drive: " _
		& vbCR & "ErrorNum: " & Err.Number & vbCR & Err.Description & vbCR
 Else strResult = strResult & "s-drive was mapped successfully." & vbCR
end If

'---------------------------------------------------------------------
'Cleanup
'---------------------------------------------------------------------

'Finally, print what happened
if not strResult = "" Then
	strResult = strResult & vbCR & "Drive/print map script has completed; you may close this dialog."
	MsgBox strResult,,strMsgTitle
end if
wscript.quit
end Sub