?
Solved

Roaming profile breaks default printer

Posted on 2009-04-27
12
Medium Priority
?
729 Views
Last Modified: 2012-08-13
Before setting up active directory, users would login with the same local username on every PC.  This local user was named user1 and the default printer was setup for that user.  Now with AD, when a user logs in, the default printer becomes the "Microsoft XPS Document Writer" instead of the default printer that was assigned to user1.  Is there any way to set the default printer thru a local account so that each user that logs in with roaming profiles will get the same default printer?
0
Comment
Question by:bpl5000
  • 8
  • 4
12 Comments
 
LVL 5

Author Comment

by:bpl5000
ID: 24247204
I should also add that we have hundreds of printers, all different models so I would be unable to run a script that sets the printer by name.  We will have many users logging into different PCs with different default printers.  We previously used Novell so they were logging onto the network with Novell and logging on local with the same username (user1) on all PCs.

Maybe I could use a script that would grab user1's default printer and set that to be the current users default printer.
0
 
LVL 27

Expert Comment

by:bluntTony
ID: 24348386
You'd need to interrogate the registry, and to do this, you'd need to get the user's SID first using WMI. Then you can grab the value of the default printer setting and edit the current user's registry.

Give me a few moments and I'll post a script. This can then be ran during login.

Tony
0
 
LVL 27

Accepted Solution

by:
bluntTony earned 2000 total points
ID: 24348575
This script will do the following,

Get the SID for the user placed in strLocalUser
Use this SID to get the default printer from the registry
Apply this printer as the current user's default printer.

Change strLocalUser to 'user1' and it should do it.

Do bear in mind though that this script is pretty basic and does assume that the printer in question is already mapped for the current user. If you want more added to it let me know.

Tony
On Error Resume Next
 
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set objNetwork = CreateObject("WScript.Network")
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
 
strLocalUser = "user1"
 
'strLocalSID = objWMIService.Get("Win32_UserAccount.Name='" & strLocalUser &"',Domain='" & objNetwork.ComputerName & "'").SID
Const HKU  = &H80000003
strValueName = "Device"
strKeyPath = strLocalSID & "\Software\Microsoft\Windows NT\CurrentVersion\Windows"
objReg.GetStringValue HKU,strKeyPath,strValueName,strValue
 
strPrinter = Left(strValue,InStr(strValue,",")-1)
objNetwork.SetDefaultPrinter strPrinter

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 27

Expert Comment

by:bluntTony
ID: 24348727
You'll have to uncomment line 9 - sorry!
0
 
LVL 5

Author Comment

by:bpl5000
ID: 24692939
Sorry for taking so long to get back to this.  I tried testing it by putting it into a VB6 program.  I commented out line 1 and I get this error:
"Invalid use of Null"

On line 13, strKeyPath ends up being set to NULL.  I thought this was happening because I thought line 13 needed to be:
strKeyPath = strLocalSID & "\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device"

I thought it needed "Device" on the end.  Still it returns Null.  Any ideas?
0
 
LVL 5

Author Comment

by:bpl5000
ID: 24692967
I made a mistake in my last post.  strKeyPath is fine, and the error is on line 13, which is:

objReg.GetStringValue HKU, strKeyPath, strValueName, strValue

strValue ends up being Null.
0
 
LVL 5

Author Comment

by:bpl5000
ID: 24692993
I also now see where the value name "Device" is given on line 11.  Now I realize that I don't need to add that to the path.
0
 
LVL 5

Author Comment

by:bpl5000
ID: 24693122
I also tried running the script from a vbs file and it gives the same error.
0
 
LVL 5

Author Comment

by:bpl5000
ID: 24695431
I think the problem has to do with this line...
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")

I need access to the registry of user1 and I'm assuming the "impersonate" is suppose to allow me access to user1's registry, but it's not allowing me access.  If I change it to an area of the registry that the current logged in user has access to, then it works.  Any ideas?
0
 
LVL 5

Author Comment

by:bpl5000
ID: 24702062
bluntTony, you are a genius!  It does work great, I just had to load a hive from user1's registry.  The updated code is posted below.

Thanks bluntTony... you're awesome!
On Error Resume Next
Const HKU = &H80000003
Set objshell = CreateObject("Wscript.shell")
Set objNetwork = CreateObject("WScript.Network")
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
objshell.Exec ("%comspec% /k reg.exe load HKU\TempUser1 c:\Docume~1\User1\ntuser.dat")
WScript.Sleep (200)
strValueName = "Device"
strKeyPath = "TempUser1\Software\Microsoft\Windows NT\CurrentVersion\Windows"
objReg.GetStringValue HKU, strKeyPath, strValueName, strValue
strPrinter = Left(strValue, InStr(strValue, ",") - 1)
objNetwork.SetDefaultPrinter strPrinter
objshell.Exec ("%comspec% /k reg.exe unload HKU\TempUser1")

Open in new window

0
 
LVL 5

Author Closing Comment

by:bpl5000
ID: 31575291
Excellent code, bluntTony!  Just had to add a command to load a hive from the user's HKU and then it worked our application.
0
 
LVL 27

Expert Comment

by:bluntTony
ID: 24702324
Glad you got it sorted! Sorry I didn't reply - didn't get any email notifications for your replies for some reason...
0

Featured Post

Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Had a business requirement to store the mobile number in an environmental variable. This is just a quick article on how this was done.
Group policies can be applied selectively to specific devices with the help of groups. Utilising this, it is possible to phase-in group policies, over a period of time, by randomly adding non-members user or computers at a set interval, to a group f…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question