Hello All,
I am very new to VBScript and am trying to work with an existing one that my organization has been using for a while. Unfortunately we've encountered the above error during a mass roll-out and cannot get the darn thing to run regardless of permissions of who is running it. The script is hit or miss as SOME workstations will allow it and others will not. We're working in a Windows 7/XP mixed environment.
The script is SUPPOSED to either DELETE or ADD a network printer for all users on a workstation. I know that an alternative is to use Group Policy in AD but that's not an option here.
Any ideas?
Set objShell = CreateObject("Wscript.Shell")
Set objEnv = objShell.Environment("Process")
PrinterFlag = 1
'********************************************************************************
'* Gather Information: Workstation Name, Printer Server, and Printer Name *
'********************************************************************************
ADResult = MsgBox("Click 'YES' to Add or 'NO' to Delete a printer. Click 'CANCEL' to cancel the request", vbQuestion + vbyesNoCancel, "Add or Delete?")
If ADResult = vbYes then AddOrDelete = "/ga"
If ADResult = vbNo then AddOrDelete = "/gd"
If ADResult = vbCancel then Wscript.Quit Result
ComputerName = objEnv("ComputerName")
ComputerName = InputBox("Enter the Name of the Workstation you wish to Install the printer on.", "Workstation Name", ComputerName)
Set objComputer = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & ComputerName & "\root\cimv2")
PrinterServer = "PRINTER"
PrinterServer = InputBox("Enter the NAME of the Server that hosts the printer you wish to Install.", "Server Name", PrinterServer)
Set objServer = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & PrinterServer & "\root\cimv2")
PrinterShare = ""
PrinterShare = InputBox("Enter the NAME of the printer you wish to Install, (i.e. K0507). The name is Case Sensitive.", "Printer Name", PrinterShare)
PrinterString = "rundll32 printui.dll,PrintUIEntry " & AddOrDelete & " /c\\" & ComputerName & " /n\\" & PrinterServer & "\" & PrinterShare
'********************************************************************************
'* Start SPOOLER Service *
'********************************************************************************
Set objService = objComputer.Get("Win32_Service='Spooler'")
Result = objService.StartService()
If ADResult = vbYes then
' ********************************************************************************
' * Check to see if the printer exists *
' ********************************************************************************
Set colPrinters = objServer.ExecQuery("Select * from Win32_Printer Where Name = '" & PrinterShare & "'")
For Each objPrinter in colPrinters
If objPrinter.Name = PrinterShare Then
' ********************************************************************************
' * Printer was Found *
' ********************************************************************************
Result = objShell.Run(PrinterString,,True)
If Result <> 0 Then MsgBox "There was an error executing the command. The return code is: " & Result
DefResult = MsgBox("Default Printer? 'YES' or 'NO'", vbQuestion + vbyesNo, "Default Printer?")
If DefResult = vbYes then
' ********************************************************************************
' * Get list of SID's for remote machine and Set their Default Printer *
' ********************************************************************************
Set objReg = GetObject("winmgmts:\\" & ComputerName & "\root\default:StdRegProv")
objReg.EnumKey &H80000002, "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList", colSIDs
For Each objSID In colSIDs
objReg.SetStringValue &H80000003, objSID & "\Software\Microsoft\Windows NT\CurrentVersion\Windows", "Device", "\\" & PrinterServer & "\" & PrinterShare & "," & "winspool,Ne00:"
Next
End If
MsgBox "Printer, \\" & PrinterServer & "\" & PrinterShare & " was successfully INSTALLED on " & ComputerName & ".",vbOKOnly + vbInformation,"Printer INSTALLED"
PrinterFlag = 0
Exit For
End If
Next
If PrinterFlag <> 0 Then MsgBox "Printer, \\" & PrinterServer & "\" & PrinterShare & " does not exist.",vbOKOnly + vbCritical,"Printer NOT Found"
Else
' ********************************************************************************
' * Delete the Printer *
' ********************************************************************************
Result = objShell.Run(PrinterString,,True)
If Result <> 0 Then MsgBox "There was an error executing the command. The return code is: " & Result
MsgBox "Printer, \\" & PrinterServer & "\" & PrinterShare & " was successfully DELETED on " & ComputerName & ".",vbOKOnly + vbInformation,"Printer DELETED"
End If
'********************************************************************************
'* Stop SPOOLER Service *
'********************************************************************************
Result = objService.StopService()
'********************************************************************************
'* Start SPOOLER Service *
'********************************************************************************
Result = objService.StartService()
Wscript.Quit 0