Printer Scripts with Admin Rights

zemp1212 used Ask the Experts™
ok so i have a script that most likely work for replacing printers and replacing the default printer...

problem is I need to RUNAS  domain admin credentials in there

below is what I have. i looked through other posts...but I am such a beginner at Vbs, I am lost...

strComputer = "."
strLogFileShare = "\\testprint\Software\batch\"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForAppending = 8
Set objShell = CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
arrPrinterShares = Array( _
      "\\lincoln\JD Admin Laser:\\TestPrint\JD Admin Laser-NEW", _
"\\lincoln\Lib Central Cataloging:\\TestPrint\Lib Central Cataloging-NEW"_
Set objNetwork = CreateObject("WScript.Network")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colPrinters = objWMIService.ExecQuery _
    ("Select * From Win32_Printer Where Local = False")
strLogFile = strLogFileShare & objNetwork.UserName & ".txt"
Set objOutputFile = objFSO.OpenTextFile(strLogFile, intForAppending, True)
objOutputFile.WriteLine "Logged in at: " & Now & " on " & objNetwork.ComputerName
On Error Resume Next
If colPrinters.Count <> 0 Then
    For Each objPrinter In colPrinters
            For intPrinter = LBound(arrPrinterShares) To UBound(arrPrinterShares)
                  strOldPrinter = Split(arrPrinterShares(intPrinter), ":")(0)
                  strNewPrinter = Split(arrPrinterShares(intPrinter), ":")(1)
                  boolManual = False
                  If Left(strOldPrinter, 7) = "MANUAL=" Then
                        boolManual = True
                        strOldPrinter = Replace(strOldPrinter, "MANUAL=", "")
                  End If
                  If UCase(objPrinter.DeviceID) = UCase(strOldPrinter) Then
                  		objOutputFile.WriteLine "Current printer " & UCase(objPrinter.DeviceID) & " matches with " & UCase(strOldPrinter) & ", replacing with " & strNewPrinter
                        If boolManual = True Then
                              objOutputFile.WriteLine "MANUAL: Please obtain the account code for the printing from " & objPrinter.DeviceID & " and then replace that with " & strNewPrinter & " and set the account code."
                        ElseIf objPrinter.Default = True Then
                              objNetwork.RemovePrinterConnection objPrinter.DeviceID, True, True
                              objNetwork.AddWindowsPrinterConnection strNewPrinter
                              objNetwork.SetDefaultPrinter strNewPrinter
                              objOutputFile.WriteLine "Replaced " & objPrinter.DeviceID & " with " & strNewPrinter & " and set it as default"
                              objNetwork.RemovePrinterConnection objPrinter.DeviceID, True, True
                              objNetwork.AddWindowsPrinterConnection strNewPrinter
                              If Err.Number = 0 Then
                              	objOutputFile.WriteLine "Replaced " & objPrinter.DeviceID & " with " & strNewPrinter
                              	objOutputFile.WriteLine "Failed to replace " & objPrinter.DeviceID & " with " & strNewPrinter
                              End If
                        End If                              
                  	objOutputFile.WriteLine "Current printer " & UCase(objPrinter.DeviceID) & " does not match with " & UCase(strOldPrinter)
                  End If
            If Err.Number <> 0 Then
                  objOutputFile.WriteLine Err.Number & ": " & Err.Description & VbCrLf & objPrinter.DeviceID & " --> " & strNewPrinter
            End If
End If
On Error GoTo 0
Set objOutputFile = Nothing
'MsgBox "Done"

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Mapping and unmapping network printers and setting a default does not normally require administrative privileges.  Is there a reason you need to run this under alternate credentials?


maybe its the way its doing it then...

I get an error

Windows script Host

error:Permission Denied

but it works when I am logged in using my Domain Admin account...

On what line does the error come up?
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.


Line: 21

Char: 1
Ok.  The issue is that users don't have sufficient permissions to write to the log file.  Grant them Write permission to the directory defined in the strLogFileShare variable on line 2 and it should work correctly.  

Remember to check both share and NTFS permissions.


Perfect that was it...missed the full control in the sharing...


Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial