Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

check for existance of a registry key?

Status
Not open for further replies.

sparkbyte

Technical User
Sep 20, 2002
879
US
I need a way to check to see if a registry exists.

This one to be exact.
HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows

We are having lots of problems with terminal services roaming profiles and printer settings. Well, actually, users are loosing their default printer setting. Thing is, it is getting un set, not changed, but unset and cannot set it once logged on. This causes Adobe reader to not print anything because it HAS to be able to see a default printer assignment and will say there is no printer installed if if can't see that a default printer has been assigned.

So I have written a script that logs a users default printer. But as part of this script I would like to add the ability to also log wheter or not this registry key exists because of the following request from MS.

MS Support said:
This is in continuation to the conversation we had on Thursday. I would like to do the following checks on the server at the time of the issue.

1) Please check if the following key is available on the terminal session while the user is experiencing the issue.
[!]HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows[/!]

2) Please download the User Profile Hive Cleanup Service and install it on any one Terminals server Farm in question and see if that makes any difference. The tool can be downloaded from
[blue][/blue]


Thanks

John Fuhrman
Titan Global Services
 
Thanks PHV, but I had already seen this article. My problem is that what MS is asking is if the FOLDER exists not the key. I can't seem to be able to figure out how to test for the existance of a registry folder. Shoot for that matter a key. I can read/set any registry key, but I am having trouble getting an "If Exist" working.



Thanks

John Fuhrman
Titan Global Services
 
Hey, did some more digging and found this.

This is kind of hard to do because of what you can read.

If you try to read a nonexistent key *or* a key with no default value set, you
will get an identical error number back either way. The trick is to look for
the error description text; the error says "Unable to read" if there is an
actual but unspecified root there. The following function will do what you are
after.

Make sure you have a terminal "\" on the key you are trying to read.

Code:
WScript.Echo IsRegKey("HKLM\Software\")
WScript.Echo IsRegKey("HKLM\saljkdhflaksdf\")

Function IsRegKey(sKey)
Dim Sh
Set Sh = CreateObject("WScript.Shell")
On Error Resume Next
IsRegKey = False
ret = Sh.RegRead(sKey)
If Err.Number = 0 Then
IsRegKey = True
ElseIf CBool(Instr(Err.Description,"Unable")) Then
IsRegKey = True
End If
Err.Clear: On Error Goto 0
End Function

I'll have to see if this will do what I need.

Thanks

John Fuhrman
Titan Global Services
 
OK this works, but when the "key" does not exist it stalls for a very long time. 2 to 5 minutes! Don't get any errors though.

any ideas how to get around this??

Code:
'*******************************************************************
'	Author:	John Fuhrman
'			Lenexa Outlink Data Center
'			10910 W. 87th
'			Lenexa, Ks 66215
'
'	Date:	07/09/2007
'
'	Purpose:
'	Log users default printer to a comma delimited file.
'
'*******************************************************************

On Error Resume Next

'Grab the user name
Set WSHNetwork = CreateObject("WScript.Network")
	strUser = WSHNetwork.UserName
	strDomain = WSHNetwork.UserDomain
	strComputer = wshnetwork.ComputerName
	strBank = (Left(strUser,3)) ' Grab the first 3 characters of the username
Set WSHNetwork = Nothing 
	
Call LogDefaultPrinter()

Sub LogDefaultPrinter()
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
' Only Get printers that are set to Default.
Set colItems = objWMIService.ExecQuery("Select * from Win32_Printer where default=true")    'not semi-synchronous to use .count
If colItems.count=0 Then 
'	    wscript.echo "No default printer set."
			If IsNumeric(strBank) = True Then 
				Add2Log  Chr(34) & strBank &  Chr(34) & "," &  Chr(34) &_
				 strUser & Chr(34) & "," & Chr(34) & strComputer & Chr(34) &_
				"," & Chr(34) & "No Default Printer Set" & Chr(34) & "," &_ 
				Chr(34) & Trim(IsRegKey("HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\")) & Chr(34)

			Else
				' If not Bank user ID then Bank 001
				Add2Log Chr(34) & "001" & Chr(34) &  "," & Chr(34) &_
				strUser & Chr(34) & "," & Chr(34) & strComputer &_
				Chr(34) & "," & Chr(34) & "No Default Printer Set" & Chr(34)  & "," &_
				Chr(34) & Trim(IsRegKey("HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\")) & Chr(34)
			End If
Else 
	For Each objItem in colItems
		If objItem.Default = True Then 
			' Write to Log when the user has a Bank ID.
			If IsNumeric(strBank) = True Then 
				Add2Log  Chr(34) & strBank &  Chr(34) & "," &  Chr(34) &_
				 strUser & Chr(34) & "," & Chr(34) & strComputer & Chr(34) &_
				"," & Chr(34) & objItem.DeviceID & Chr(34) & "," & Chr(34) &_
				Chr(34) & (Mid(objItem.PortName,4)) & Chr(34) & "," &_
				Chr(34) & Trim(IsRegKey("HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\")) & Chr(34)
			Else
				' If not Bank user ID then Bank 001
				Add2Log Chr(34) & "001" & Chr(34) &  "," & Chr(34) &_
				strUser & Chr(34) & "," & Chr(34) & strComputer &_
				Chr(34) & "," & Chr(34) & objItem.DeviceID & Chr(34) & "," &_
				Chr(34) & (Mid(objItem.PortName,4)) & Chr(34) & "," &_
				Chr(34) & Trim(IsRegKey("HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\")) & Chr(34)
			End If
		Else 
		End If 	
	Next 
End If

' Clean up memory by removing objects created 
Set objWMIService = Nothing 
Set colItems = Nothing 

End Sub 

Sub Add2Log(txt) ' txt is the text we deliver into the sub  
Dim fso
Set fso = CreateObject("scripting.filesystemobject")

' Declare the log file name  
Myfile = "\\corebanks\SYSVOL\corebanks.jackhenry.com\LogFiles\" &_
		 "DefaultPrinters.csv"

' Open it for Append  
Const ForAppending = 8 ' Append mode  
' Declare the FileSystemObject and File variables 
  	
' Create a new FileSystemObject object  
Set fso = CreateObject("Scripting.FileSystemObject")
' Open the file and force creation, if it doesn't exist already  
Set file = fso.OpenTextFile(MyFile, ForAppending, TRUE)
file.WriteLine (txt) ' append log  
' Clean up  
Set file = Nothing 
Set fso = Nothing 
End Sub


Function IsRegKey(sKey)
Dim WshShell
set WshShell = CreateObject("wscript.Shell")
On Error Resume Next
IsRegKey = False
ret = WshShell.RegRead(sKey)
If Err.Number = 0 Then
IsRegKey = True
ElseIf CBool(Instr(Err.Description,"Unable")) Then
IsRegKey = True
End If
Err.Clear: On Error Goto 0
End Function


Thanks

John Fuhrman
Titan Global Services
 
Another strange thing is that this runs with no delays.

Code:
WScript.Echo IsRegKey("HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\")


Function IsRegKey(sKey)
Dim WshShell
set WshShell = CreateObject("wscript.Shell")
On Error Resume Next
IsRegKey = False
ret = WshShell.RegRead(sKey)
If Err.Number = 0 Then
IsRegKey = True
ElseIf CBool(Instr(Err.Description,"Unable")) Then
IsRegKey = True
End If
Err.Clear: On Error Goto 0
End Function


Thanks

John Fuhrman
Titan Global Services
 
Do you not get any error even when commenting out your initial "On Error Resume Next"??

Also, the RegKey check function doesn't work too well for me.

See if this one works for you.

Code:
Option Explicit

WScript.Echo CStr(RegKeyPresent("Software\Microsoft\Windows NT\CurrentVersion\Windows"))
WScript.Echo CStr(RegKeyPresent("Software\Microsoft\Windows NT\CurrentVersion\Windws"))

Function RegKeyPresent(strKeyPath)
	Const HKCU = &H80000001
	Dim objReg : Set objReg = GetObject("winmgmts:\\.\root\default:StdRegProv")
	Dim arrValues
	RegKeyPresent = False
	Dim errRtn : errRtn = objReg.EnumValues(HKCU, strKeyPath, arrValues)
	If errRtn = 0 Then RegKeyPresent = True
End Function



--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Still seems to be taking forever.

Can someone run this and tell me if it seems to take 3 to 5 minutes to run? This needs to run as part of our AD logon process so this long delay won't work.

Oh, forgot to mention, all this does is check for the logged on username, default printer, default printer Port Name, and the existance of the registry key where the defualt printer info is stored and write that to a CSV file.

TIA..

Code:
'*******************************************************************
'	Author:	John Fuhrman
'			Lenexa Outlink Data Center
'			10910 W. 87th
'			Lenexa, Ks 66215
'
'	Date:	03/15/2007
'
'	Purpose:
'	Log users default printer to a comma delimited file.
'
'	Update:
'	Date: 07/09/2007
'	Purpose:
'	Added check for registry key per request from MS on problem ticket
'	conserning Printing issues and roaming profiles.
'
'*******************************************************************
Option Explicit 
'On Error Resume Next

'Grab the user name
Dim WSHNetwork,strUser,strDomain,strComputer,strBank  
Set WSHNetwork = CreateObject("WScript.Network")
	strUser = WSHNetwork.UserName
	strDomain = WSHNetwork.UserDomain
	strComputer = wshnetwork.ComputerName
	strBank = (Left(strUser,3)) ' Grab the first 3 characters of the username
Set WSHNetwork = Nothing 
	
Call LogDefaultPrinter()

Sub LogDefaultPrinter()
Dim objWMIService: Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
' Only Get printers that are set to Default.
Dim colItems: Set colItems = objWMIService.ExecQuery("Select * from Win32_Printer where default=true")    'not semi-synchronous to use .count
If colItems.count=0 Then 
'	    wscript.echo "No default printer set."
			If IsNumeric(strBank) = True Then 
				Add2Log  Chr(34) & strBank &  Chr(34) & "," &  Chr(34) &_
				strUser & Chr(34) & "," & Chr(34) & strComputer & Chr(34) & "," &_
				Chr(34) & "No Default Printer Set" & Chr(34) & "," & Chr(34) & Chr(34) & "," &_ 
				Chr(34) & CStr(RegKeyPresent("Software\Microsoft\Windows NT\CurrentVersion\Windows")) & Chr(34)

			Else
				' If not Bank user ID then Bank 001
				Add2Log Chr(34) & "001" & Chr(34) &  "," & Chr(34) &_
				strUser & Chr(34) & "," & Chr(34) & strComputer & Chr(34) & "," &_
				Chr(34) & "No Default Printer Set" & Chr(34) & "," & Chr(34) & Chr(34) & "," &_ 
				Chr(34) & CStr(RegKeyPresent("Software\Microsoft\Windows NT\CurrentVersion\Windows")) & Chr(34)
			End If
Else 
	Dim objItem
	For Each objItem in colItems
		If objItem.Default = True Then 
			' Write to Log when the user has a Bank ID.
			If IsNumeric(strBank) = True Then 
				Add2Log  Chr(34) & strBank &  Chr(34) & "," &  Chr(34) &_
				strUser & Chr(34) & "," & Chr(34) & strComputer & Chr(34) & "," &_
				Chr(34) & objItem.DeviceID & Chr(34) & "," & Chr(34) &_
				Chr(34) & (Mid(objItem.PortName,4)) & Chr(34) & "," &_
				Chr(34) & CStr(RegKeyPresent("Software\Microsoft\Windows NT\CurrentVersion\Windows")) & Chr(34)
			Else
				' If not Bank user ID then Bank 001
				Add2Log Chr(34) & "001" & Chr(34) &  "," & Chr(34) &_
				strUser & Chr(34) & "," & Chr(34) & strComputer &_
				Chr(34) & "," & Chr(34) & objItem.DeviceID & Chr(34) & "," &_
				Chr(34) & (Mid(objItem.PortName,4)) & Chr(34) & "," &_
				Chr(34) & CStr(RegKeyPresent("Software\Microsoft\Windows NT\CurrentVersion\Windows")) & Chr(34)
			End If
		Else 
		End If 	
	Next 
End If

' Clean up memory by removing objects created 
Set objWMIService = Nothing 
Set colItems = Nothing 

End Sub 

Sub Add2Log(txt) ' txt is the text we deliver into the sub  
Dim fso
Set fso = CreateObject("scripting.filesystemobject")

' Declare the log file name  
'Dim Myfile: Myfile = "\\corebanks\SYSVOL\corebanks.jackhenry.com\LogFiles\" &_
'		 "DefaultPrinters.csv"
Dim Myfile: Myfile = "DefaultPrinters.csv"

' Open it for Append  
Const ForAppending = 8 ' Append mode  
' Declare the FileSystemObject and File variables 
  	
' Create a new FileSystemObject object  
Set fso = CreateObject("Scripting.FileSystemObject")
' Open the file and force creation, if it doesn't exist already  
Dim file: Set file = fso.OpenTextFile(MyFile, ForAppending, TRUE)
file.WriteLine (txt) ' append log  
' Clean up  
Set file = Nothing 
Set fso = Nothing 
End Sub

'WScript.Echo CStr(RegKeyPresent("Software\Microsoft\Windows NT\CurrentVersion\Windows"))
'WScript.Echo CStr(RegKeyPresent("Software\Microsoft\Windows NT\CurrentVersion\Windws"))

Function RegKeyPresent(strKeyPath)
    Const HKCU = &H80000001
    Dim objReg : Set objReg = GetObject("winmgmts:\\.\root\default:StdRegProv")
    Dim arrValues
    RegKeyPresent = False
    Dim errRtn : errRtn = objReg.EnumValues(HKCU, strKeyPath, arrValues)
    If errRtn = 0 Then RegKeyPresent = True
End Function

Thanks

John Fuhrman
Titan Global Services
 
OK, sorry, looks like I am exagerating. But this is taking too long to be able to put into the logon process.

Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

Script Start Time: 11:36:46 AM
Get Default Printer Start: 11:36:46 AM
Get Default Printer End: 11:36:46 AM
Function Start Time: 11:37:35 AM
Function Stop Time: 11:37:35 AM
Add2Log Start: 11:37:35 AM
Add2Log End: 11:37:35 AM
Script Stop Time: 11:37:35 AM

***** script completed *****

[!]CODE with Time Echoes.[/!]
Code:
'*******************************************************************
'	Author:	John Fuhrman
'			Lenexa Outlink Data Center
'			10910 W. 87th
'			Lenexa, Ks 66215
'
'	Date:	03/15/2007
'
'	Purpose:
'	Log users default printer to a comma delimited file.
'
'	Update:
'	Date: 07/09/2007
'	Purpose:
'	Added check for registry key per request from MS on problem ticket
'	conserning Printing issues and roaming profiles.
'
'*******************************************************************
Option Explicit 
'On Error Resume Next
WScript.Echo "Script Start Time:  " & Time()
'Grab the user name
Dim WSHNetwork,strUser,strDomain,strComputer,strBank  
Set WSHNetwork = CreateObject("WScript.Network")
	strUser = WSHNetwork.UserName
	strDomain = WSHNetwork.UserDomain
	strComputer = wshnetwork.ComputerName
	strBank = (Left(strUser,3)) ' Grab the first 3 characters of the username
Set WSHNetwork = Nothing 
	
Call LogDefaultPrinter()

Sub LogDefaultPrinter()
Dim objWMIService: Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
' Only Get printers that are set to Default.
WScript.Echo "Get Default Printer Start:  " & Time()
Dim colItems: Set colItems = objWMIService.ExecQuery("Select * from Win32_Printer where default=true")    'not semi-synchronous to use .count
WScript.Echo "Get Default Printer End:  " & Time()
If colItems.count=0 Then 
'	    wscript.echo "No default printer set."
			If IsNumeric(strBank) = True Then 
				Add2Log  Chr(34) & strBank &  Chr(34) & "," &  Chr(34) &_
				strUser & Chr(34) & "," & Chr(34) & strComputer & Chr(34) & "," &_
				Chr(34) & "No Default Printer Set" & Chr(34) & "," & Chr(34) & Chr(34) & "," &_ 
				Chr(34) & CStr(RegKeyPresent("Software\Microsoft\Windows NT\CurrentVersion\Windows")) & Chr(34)

			Else
				' If not Bank user ID then Bank 001
				Add2Log Chr(34) & "001" & Chr(34) &  "," & Chr(34) &_
				strUser & Chr(34) & "," & Chr(34) & strComputer & Chr(34) & "," &_
				Chr(34) & "No Default Printer Set" & Chr(34) & "," & Chr(34) & Chr(34) & "," &_ 
				Chr(34) & CStr(RegKeyPresent("Software\Microsoft\Windows NT\CurrentVersion\Windows")) & Chr(34)
			End If
Else 
	Dim objItem
	For Each objItem in colItems
		If objItem.Default = True Then 
			' Write to Log when the user has a Bank ID.
			If IsNumeric(strBank) = True Then 
				Add2Log  Chr(34) & strBank &  Chr(34) & "," &  Chr(34) &_
				strUser & Chr(34) & "," & Chr(34) & strComputer & Chr(34) & "," &_
				Chr(34) & objItem.DeviceID & Chr(34) & "," & Chr(34) &_
				Chr(34) & (Mid(objItem.PortName,4)) & Chr(34) & "," &_
				Chr(34) & CStr(RegKeyPresent("Software\Microsoft\Windows NT\CurrentVersion\Windows")) & Chr(34)
			Else
				' If not Bank user ID then Bank 001
				Add2Log Chr(34) & "001" & Chr(34) &  "," & Chr(34) &_
				strUser & Chr(34) & "," & Chr(34) & strComputer &_
				Chr(34) & "," & Chr(34) & objItem.DeviceID & Chr(34) & "," &_
				Chr(34) & (Mid(objItem.PortName,4)) & Chr(34) & "," &_
				Chr(34) & CStr(RegKeyPresent("Software\Microsoft\Windows NT\CurrentVersion\Windows")) & Chr(34)
			End If
		Else 
		End If 	
	Next 
End If
WScript.Echo "Script Stop Time:  " & Time()

' Clean up memory by removing objects created 
Set objWMIService = Nothing 
Set colItems = Nothing 

End Sub 

Sub Add2Log(txt) ' txt is the text we deliver into the sub  
WScript.Echo "Add2Log Start:  " & Time()

Dim fso
Set fso = CreateObject("scripting.filesystemobject")

' Declare the log file name  
'Dim Myfile: Myfile = "\\corebanks\SYSVOL\corebanks.jackhenry.com\LogFiles\" &_
'		 "DefaultPrinters.csv"
Dim Myfile: Myfile = "DefaultPrinters.csv"

' Open it for Append  
Const ForAppending = 8 ' Append mode  
' Declare the FileSystemObject and File variables 
  	
' Create a new FileSystemObject object  
Set fso = CreateObject("Scripting.FileSystemObject")
' Open the file and force creation, if it doesn't exist already  
Dim file: Set file = fso.OpenTextFile(MyFile, ForAppending, TRUE)
file.WriteLine (txt) ' append log  
' Clean up  
Set file = Nothing 
Set fso = Nothing 
WScript.Echo "Add2Log End:  " & Time()

End Sub

'WScript.Echo CStr(RegKeyPresent("Software\Microsoft\Windows NT\CurrentVersion\Windows"))
'WScript.Echo CStr(RegKeyPresent("Software\Microsoft\Windows NT\CurrentVersion\Windws"))

Function RegKeyPresent(strKeyPath)
WScript.Echo "Function Start Time:  " & Time()
    Const HKCU = &H80000001
    Dim objReg : Set objReg = GetObject("winmgmts:\\.\root\default:StdRegProv")
    Dim arrValues
    RegKeyPresent = False
    Dim errRtn : errRtn = objReg.EnumValues(HKCU, strKeyPath, arrValues)
    If errRtn = 0 Then RegKeyPresent = True
WScript.Echo "Function Stop Time:  " & Time()
End Function


Thanks

John Fuhrman
Titan Global Services
 
OK, this is strange. Only seems to have the extended run times from my admin server.

If ran from one of the normal client TS servers it seems to be fine.



Thanks

John Fuhrman
Titan Global Services
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top