862Views2Replies

Author Options:

What is wrong with this vbs script? Answered

It is a program to add or remove printers on a local machine, and it works locally, but not when trying to run through group policy in the AD.  Btw, this is a friend's, not mine, so I can only answer so many questions.  Please help.

Here is the code:

PrnName = "Kyocera FS-1100 KX"
Set objWshShell = WScript.CreateObject("WScript.Shell" )

'retrieve username and computername
strComputer = objWshShell.ExpandEnvironmentStrings("%COMPUTERNAME%" )
strUserName = objWshShell.ExpandEnvironmentStrings("%username%" )
strHome = objWshShell.ExpandEnvironmentStrings("%HOMEPATH%" )
strDrive = objWshShell.ExpandEnvironmentStrings("%HOMEDRIVE%" )

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2" )

objWMIService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege", True

Set colInstalledPrinters = objWMIService.ExecQuery("Select * from Win32_Printer" )

Set dr = objWMIService.ExecQuery("Select * from Win32_Printer where DeviceID = 'Kyocera FS-1100 KX'" )

Set fs = CreateObject("Scripting.FileSystemObject" )
folder = strDrive & strHome

if fs.FolderExists(folder) and dr.Count <> 0 then
For Each objPrinter in colInstalledPrinters
if objPrinter.DeviceID = PrnName then
objPrinter.SetDefaultPrinter()
end if
Next
else
'if fs.FolderExists(folder) and dr.Count = 0 then
'For Each objPrinter in colInstalledPrinters
'objPrinter.Delete_
'Next
'end if

if (fs.FolderExists(folder)) and (dr.Count = 0 or colInstalledPrinters.Count = 0) then
' Add a l'imprimante
''''''''''''''''''''''''''
Set objPrinter = objWMIService.Get("Win32_Printer" ).SpawnInstance_
objPrinter.DriverName = PrnName
objPrinter.PortName = "USB001"
objPrinter.DeviceID = PrnName
objPrinter.Location = "Imprimante installée sur la machine " & strComputer
objPrinter.Comment = "Imprimante " & PrnName
objPrinter.Network = False
objPrinter.Put_
objPrinter.SetDefaultPrinter()
end if

For Each objPrinter in colInstalledPrinters
if objPrinter.DeviceID = "Microsoft XPS Document Writer" or objPrinter.DeviceID = "PDF4U Adobe Creator" or objPrinter.DeviceID = "Microsoft Office Document Image Writer" then
objPrinter.Delete_
end if
Next

' restart spooler

Set objSpoolerSvc = objWMIService.Get("Win32_Service.Name='spooler'" )
iReturn = objSpoolerSvc.StopService()
iReturn = objSpoolerSvc.StartService()
wscript.quit

end if

'For Each User In objlocal
'If lcase(User.Name) = lcase(strUserName) Then
'Set objUser = GetObject("WinNT://" & strComputer & "/username, " & strUserName)

'msgbox User.Name & " already exists." & vbCrLf & "The password was re-set."
'WScript.Quit
'End If
'Next
'End Sub

'strCommand = "rundll32 printui.dll,PrintUIEntry /y /n Kyocera FS-1100 KX /f /r USB001"
'intResult = objWshShell.run ("rundll32 printui.dll,PrintUIEntry /p /n Kyocera FS-1100 KX" , 0, true)

Discussions

The forums are retiring in 2021 and are now closed for new topics and comments.
0
mwrenfro
mwrenfro

10 years ago

If your friend has the authority to do so, I'd immediately look at installing Group Policy Preferences. Preferences can be applied to systems running XP (with a client-side extension installed) or higher (no extra client-side parts required), and preferences can be managed on systems running Vista, Server 2008, or higher.

One introduction to configuring printers with GPP is here.

0
geekvig
geekvig

Answer 10 years ago

I asked him about it, but he hasn't gotten back yet.  He should have the authority to do so, but I'm not sure.  I also don't know what OS he has, but I think it's XP.  He lives in another country, so I don't have access to his machines or anything like that.