Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Checking which printer is default printer IN WSH

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,1016
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Checking which printer is default printer IN WSH
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Checking which printer is default printer IN WSH - 4/30/2004 4:31:15 AM   
  dddane

 

Posts: 1
Score: 0
Joined: 4/30/2004
From:
Status: offline
I'm trying to map each user's printers to a new print server with their login script in WSH.

I have the code (below) to successfully do this. However, the ONLY thing missing is setting the default printer. I know that there is a method that DOES set the default printer, but I first need to know what their old default printer was. I haven't been able to find this. I found code in VB that will use API calls to do this--but AFAIK these won't work from WSH.

The code I have (slightly sloppy, I know, but it works) is this:


oldPrinter="\\PRSRV1\Color4002"
newPrinter="\\PRSRV2\Color4002"

For i = 0 to oPrinters.Count - 1 Step 2

if Trim(oPrinters.Item(i+1)) = oldPrinter then
'Printer found. Lets add the replacement, if successful delete the old.

WshNetwork.AddWindowsPrinterConnection newPrinter

If Err <> 0 OR blnError = True Then
'Printer was NOT added

Else

WshNetwork.RemovePrinterConnection oldPrinter, true, true
End If 'end if err<>0

End If 'end if printer matches current

Next

'END ITERATION SEARCHING FOR PRINTER


I do this for each printer we have on our network. I don't worry about specifying drivers, as I made a point to use the same driver version on both servers (for anyone copying, this may create an issue if the new print server has a different driver and the user it is being added from does not have local administrative rights). Each user has 5-10 printers that are actually remapped.

Any help on this? Thanks :)
-Dane
 
 
Post #: 1
 
 Re: Checking which printer is default printer IN WSH - 6/25/2004 9:26:00 AM   
  jbird

 

Posts: 33
Score: 0
Joined: 6/25/2004
From: USA
Status: offline
I'm not sure this will help at all. I am having a similar problem trying to identify the default printer and have been unable to locate a WSH function to do so. I have been able to identify a registry key which contains the name of the default printer but I need some help to figure out how to apply the information in a network setting. The registry key is "HKEY_USERS\S-1-5-21-1123561945-1957994488-839522115-185428\Software\Microsoft\Windows NT\Current Version\Windows\Device". The problem is that I don't think the second portion "S-1-5..." is a standard name for the key. Maybe you can check your registry and let me know.

(in reply to dddane)
 
 
Post #: 2
 
 Re: Checking which printer is default printer IN WSH - 6/27/2004 7:02:11 AM   
  mbouchard


Posts: 1916
Score: 16
Joined: 5/15/2003
From: USA
Status: offline
Is it possible to use WMI? if so this might help:

quote:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer")
For Each objPrinter in colInstalledPrinters
MSG = msg & "Name: " & objPrinter.Name & vbcr
MSG = msg & "Location: " & objPrinter.Location & vbcr
MSG = msg & "Default: " & objPrinter.Default & vbcr
Next

msgbox msg

Here is a link to a couple MS resources that have WMI samples.
http://www.microsoft.com/technet/community/scriptcenter/default.mspx

Mike

(in reply to dddane)
 
 
Post #: 3
 
 Re: Checking which printer is default printer IN WSH - 6/28/2004 4:27:47 AM   
  jbird

 

Posts: 33
Score: 0
Joined: 6/25/2004
From: USA
Status: offline
The following code will retrieve the information you are looking for but it also returns some other information at the beginning and end of the string. You will need to pick out the portion you want. I needed both the printer name and server name but I think you only need the printer.

Option Explicit

Const HKEY_CURRENT_USER = &H80000001
Const ForWriting = 2
Const ForReading = 1

Dim objReg
Dim strPrinter
Dim strServer
Dim strComputer
Dim strValue
Dim strKey
Dim strEntryName
Dim strDevice
Dim objFSO
Dim objFile
Dim strCharacter
Dim strFileName

strFileName = "printer.txt"
strEntryName = "Device"
strKey = "Software\Microsoft\Windows NT\CurrentVersion\Windows"
strComputer = InputBox ("Please enter the computer name:")

Set objReg=GetObject ("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\default:StdRegProv")

objReg.GetStringValue HKEY_CURRENT_USER, strKey, strEntryName, strValue

Sorry but the code may be a little difficult to read in this window.

(in reply to dddane)
 
 
Post #: 4
 
 Re: Checking which printer is default printer IN WSH - 7/9/2004 4:46:05 AM   
  EDLiN

 

Posts: 4
Score: 0
Joined: 7/9/2004
From: USA
Status: offline
This is my first post. This thread helped me with a project so I figured I would post my cut final. Code is a combination of previous posts with a little tweekin and overcomes the XP/2K3 objPrinter.default limitation. It outputs a list to c:\printer.lst with DEFAULT next to the default printer on 2K,XP, and 2K3 systems. Drive letters were hard coded for my purposes.. but im sure you guys can figure it out. The idea is to have this run on every logon to a terminal server to dump printers to a client home drive. Then another script is run across the home drives which collects all of the data into a spreadsheet so that profiles can be rebuilt and printers re-assigned. Printer lists are first output to the home drives because clients do not all have permissions to write to a common area. Anyway, works great on my end.. Thanks for the other posts which got me to this point. Here ya go:
----------------------------------------------------------------

Option Explicit

dim objWMIService
dim ColInstalledPrinters
dim PrinterTextFile
dim objPrinter
dim DefaultPrinterString
dim objReg
dim FSO
dim outstr

Const HKEY_CURRENT_USER = &H80000001
Const ForReading = 1, ForWriting = 2, ForAppending = 8

Set objReg=GetObject ("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
objReg.GetStringValue HKEY_CURRENT_USER, "Software\Microsoft\Windows NT\CurrentVersion\Windows", "Device", DefaultPrinterString

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

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

Set FSO = CreateObject("Scripting.FileSystemObject")

if fso.driveexists("C:") then
Set PrinterTextFile = FSO.CreateTextFile("C:\Printer.lst", ForAppending)
if fso.fileexists("C:\Printer.Lst") then
For Each objPrinter in colInstalledPrinters
outstr=""
outstr=objPrinter.Name & "," & objPrinter.Location
outstr=outstr & ","
if instr(DefaultPrinterString,",")>0 then
if lcase(mid(DefaultPrinterString,1,instr(DefaultPrinterString,",")-1))=lcase(objPrinter.Name) then
outstr=outstr & "DEFAULT"
end if
end if
PrinterTextFile.writeline outstr
Next
PrinterTextFile.close
end if
end if

Set FSO = Nothing
Set objWMIService = Nothing
Set objReg = Nothing
Set colInstalledPrinters = Nothing

(in reply to dddane)
 
 
Post #: 5
 
 Re: Checking which printer is default printer IN WSH - 7/9/2004 5:00:17 AM   
  EDLiN

 

Posts: 4
Score: 0
Joined: 7/9/2004
From: USA
Status: offline
Incidently, S-1-5-21-1123561945-1957994488-839522115-185428 posted previously is not a common key. It includes a unique identifier for the user (SID). If you wanted to implement something that could know which key to use, there is are free tools available called user2sid and sid2user which will allow you to dump your sid. Use a vbscript/wsh call to call the executable, output through stdio to a text file, and then parse the stream. From a security standpoint, that number isnt really something you would necessarily want to post since it could allow an intruder to enumerate domain level accounts. Even more fun is if you change the last sequence of numbers (185428) to 500 and enumerate the SID you get the top level domain administrator account for the domain that you belong to. Misty water colored memories.

(in reply to dddane)
 
 
Post #: 6
 
 Re: Checking which printer is default printer IN WSH - 7/28/2004 6:52:19 AM   
  rhcsgcsc

 

Posts: 4
Score: 0
Joined: 7/28/2004
From:
Status: offline
Hope this helps someone:

'********************************************************************
On Error Resume Next
Set DfltPrnt = WScript.CreateObject("WScript.Shell")
RegDfltPrntValue = DfltPrnt.RegRead ("HKEY_CURRENT_USER\Software\Microsoft\Windows" &_ "NT\CurrentVersion\Windows\Device")
CommaPos = Instr(1, RegDfltPrntValue, ",")
DfltPrntValue = left(RegDfltPrntValue,CommaPos-1)
wscript.echo DfltPrntValue
'********************************************************************

(in reply to dddane)
 
 
Post #: 7
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Checking which printer is default printer IN WSH Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts