Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Help with VBScripts

Reply
 
Old 10-21-2009   #1 (permalink)
Matthew


 
 

Help with VBScripts

Run from the command line CheckREGKeys.vbs. The script seems to work
and least for not finding a key or a value but no matter what valid
key I use the script all ways tells me its invalid

cscript CheckREGKeys.vbs HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft
\Windows\WindowsUpdate

Any ideas?

All I am trying to do is to see if a key/value exists and then see if
that value is equal to some specified value.

[code= Save as CheckREGKeys.vbs]
'************************************
'* Registry Item Exists (Function)
'* Returns a value (true / false)
'************************************
'This function checks to see if a passed registry key/value exists,
and
'returns true if it does
'Original Source http://www.tek-tips.com/faqs.cfm?fid=5864
'Extended by Matthew Kruer to handel external arguments.
'
'Requirements: The registry key/value you are looking for
(RegistryItem)
'Note: RegistryItem MUST end in a backslash (\) if you are looking for
a key
' RegistryItem's without a backslash (\) will assume you are
looking for a value
option explicit
Dim objArgs, strArg
Set objArgs = WScript.Arguments
If (WScript.Arguments.Count = 1) Then
Call RegistryItemExists (objArgs(0))
WScript.Quit
Else
WScript.Echo "USAGE: CheckREGKeys.vbs keyname"
WScript.Quit
End If

'************************************
'* Registry Item Exists (Function)
'* Returns a value (true / false)
'************************************
'This function checks to see if a passed registry key/value exists,
and
'returns true if it does
'
'Requirements: The registry key/value you are looking for
(RegistryItem)
'Note: RegistryItem MUST end in a backslash (\) if you are looking for
a key
' RegistryItem's without a backslash (\) will assume you are
looking for a value
Function RegistryItemExists (RegistryItem)
'If there isnt the item when we read it, it will return an error, so
we need to resume
On Error Resume Next
WScript.Echo("Looking for Key or Value")
'Find out if we are looking for a key or a value
If (Right(RegistryItem, 1) = "\") Then
'It's a registry key we are looking for
'Try reading the key
WScript.Echo("Looking for Key")
WScript.echo RegistryItem
WshShell.RegRead RegistryItem
'Catch the error
Select Case Err
'Error Code 0 = 'success'
Case 0:
RegistryItemExists = true
WScript.Echo("1. " & RegistryItem)
'This checks for the (Default) value existing (but being blank);
as well as key's not existing at all (same error code)
Case &h80070002:
'Read the error description, removing the registry key from
that description
ErrDescription = Replace(Err.description, RegistryItem, "")
wscript.echo ErrDescription
'Clear the error
Err.clear

'Read in a registry entry we know doesn't exist (to create an
error description for something that doesnt exist)
WshShell.RegRead "HKEY_ERROR\"

'The registry key exists if the error description from the
HKEY_ERROR RegRead attempt doesn't match the error
'description from our RegistryKey RegRead attempt
If (ErrDescription <> Replace(Err.description, "HKEY_ERROR\",
"")) Then
RegistryItemExists = true
WScript.Echo("Check Found Key " & RegistryItem)
Else
RegistryItemExists = false
WScript.Echo("Check Failed for Key " & RegistryItem)
End If
'Any other error code is a failure code
Case Else:
RegistryItemExists = false
WScript.Echo("Check Failed: Unknown Failure while looking for " &
RegistryItem)
End Select
Else
'It's a registry value we are looking for
'Try reading the value
WScript.Echo("Looking for Value")
WshShell.RegRead RegistryItem
'Catch the error
Select Case Err
Case 0:
'Error Code 0 = 'success'
RegistryItemExists = true
WScript.Echo("Check Found Value for " & RegistryItem)
Case Else
'Any other error code is a failure code
RegistryItemExists = false
WScript.Echo("Check could not find a value for " & RegistryItem)
End Select
End If
'Turn error reporting back on
On Error Goto 0
End Function
[code]

My System SpecsSystem Spec
Old 10-21-2009   #2 (permalink)
Pegasus [MVP]


 
 

Re: Help with VBScripts

"Matthew" <mkruer@newsgroup> wrote in message
news:7e34de29-f141-4ccd-8d99-043f675b9b8e@newsgroup
Quote:

> Run from the command line CheckREGKeys.vbs. The script seems to work
> and least for not finding a key or a value but no matter what valid
> key I use the script all ways tells me its invalid
>
> cscript CheckREGKeys.vbs HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft
> \Windows\WindowsUpdate
>
> Any ideas?
>
> All I am trying to do is to see if a key/value exists and then see if
> that value is equal to some specified value.
>
> [code= Save as CheckREGKeys.vbs]
> '************************************
> '* Registry Item Exists (Function)
> '* Returns a value (true / false)
> '************************************
> 'This function checks to see if a passed registry key/value exists,
> and
> 'returns true if it does
> 'Original Source http://www.tek-tips.com/faqs.cfm?fid=5864
> 'Extended by Matthew Kruer to handel external arguments.
> '
> 'Requirements: The registry key/value you are looking for
> (RegistryItem)
> 'Note: RegistryItem MUST end in a backslash (\) if you are looking for
> a key
> ' RegistryItem's without a backslash (\) will assume you are
> looking for a value
> option explicit
> Dim objArgs, strArg
> Set objArgs = WScript.Arguments
> If (WScript.Arguments.Count = 1) Then
> Call RegistryItemExists (objArgs(0))
> WScript.Quit
> Else
> WScript.Echo "USAGE: CheckREGKeys.vbs keyname"
> WScript.Quit
> End If
>
> '************************************
> '* Registry Item Exists (Function)
> '* Returns a value (true / false)
> '************************************
> 'This function checks to see if a passed registry key/value exists,
> and
> 'returns true if it does
> '
> 'Requirements: The registry key/value you are looking for
> (RegistryItem)
> 'Note: RegistryItem MUST end in a backslash (\) if you are looking for
> a key
> ' RegistryItem's without a backslash (\) will assume you are
> looking for a value
> Function RegistryItemExists (RegistryItem)
> 'If there isnt the item when we read it, it will return an error, so
> we need to resume
> On Error Resume Next
> WScript.Echo("Looking for Key or Value")
> 'Find out if we are looking for a key or a value
> If (Right(RegistryItem, 1) = "\") Then
> 'It's a registry key we are looking for
> 'Try reading the key
> WScript.Echo("Looking for Key")
> WScript.echo RegistryItem
> WshShell.RegRead RegistryItem
> 'Catch the error
> Select Case Err
> 'Error Code 0 = 'success'
> Case 0:
> RegistryItemExists = true
> WScript.Echo("1. " & RegistryItem)
> 'This checks for the (Default) value existing (but being blank);
> as well as key's not existing at all (same error code)
> Case &h80070002:
> 'Read the error description, removing the registry key from
> that description
> ErrDescription = Replace(Err.description, RegistryItem, "")
> wscript.echo ErrDescription
> 'Clear the error
> Err.clear
>
> 'Read in a registry entry we know doesn't exist (to create an
> error description for something that doesnt exist)
> WshShell.RegRead "HKEY_ERROR\"
>
> 'The registry key exists if the error description from the
> HKEY_ERROR RegRead attempt doesn't match the error
> 'description from our RegistryKey RegRead attempt
> If (ErrDescription <> Replace(Err.description, "HKEY_ERROR\",
> "")) Then
> RegistryItemExists = true
> WScript.Echo("Check Found Key " & RegistryItem)
> Else
> RegistryItemExists = false
> WScript.Echo("Check Failed for Key " & RegistryItem)
> End If
> 'Any other error code is a failure code
> Case Else:
> RegistryItemExists = false
> WScript.Echo("Check Failed: Unknown Failure while looking for " &
> RegistryItem)
> End Select
> Else
> 'It's a registry value we are looking for
> 'Try reading the value
> WScript.Echo("Looking for Value")
> WshShell.RegRead RegistryItem
> 'Catch the error
> Select Case Err
> Case 0:
> 'Error Code 0 = 'success'
> RegistryItemExists = true
> WScript.Echo("Check Found Value for " & RegistryItem)
> Case Else
> 'Any other error code is a failure code
> RegistryItemExists = false
> WScript.Echo("Check could not find a value for " & RegistryItem)
> End Select
> End If
> 'Turn error reporting back on
> On Error Goto 0
> End Function
> [code]
I use the script below for this type of task. It's shorter and it works.

'Enumerate all values found in the nominated registry key.
'http://www.microsoft.com/technet/scriptcenter/guide/sas_reg_famr.mspx?mfr=True
Const HKLM=&H80000002
Const REG_SZ = 1
Const REG_EXPAND_SZ = 2
Const REG_BINARY = 3
Const REG_DWORD = 4
Const REG_MULTI_SZ = 7
Const sKeyPath = "SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU"
Set oReg=GetObject("winmgmts:\\.\root\default:StdRegProv")
oReg.EnumValues HKLM, sKeyPath, aEntryNames, aValueTypes

If VarType(aEntryNames) = 1 Then
WScript.Echo "No values found"
WScript.Quit
End If

For i = 0 To UBound(aEntryNames)
WScript.Echo "Name: " & aEntryNames(i)
Select Case aValueTypes(i)
Case REG_SZ
oReg.GetStringValue HKLM, sKeyPath, aEntryNames(i),sValue
WScript.Echo "String=" & sValue
Case REG_EXPAND_SZ
oReg.GetExpandedStringValue HKLM, sKeyPath, aEntryNames(i),
esValue
WScript.Echo "Expanded String=" & esValue
Case REG_BINARY
oReg.GetBinaryValue HKLM, sKeyPath, aEntryNames(i),aValues
Line = ""
For Each byteValue In aValues
Line = Line & byteValue
Next
WScript.Echo "Binary=" & Line
Case REG_DWORD
oReg.GetDWORDValue HKLM, sKeyPath, aEntryNames(i),dwValue
WScript.Echo "DWORD=" & dwValue
Case REG_MULTI_SZ
oReg.GetMultiStringValue HKLM, sKeyPath, aEntryNames(i), aValues
Line = ""
For Each sValue In aValues
Line = Line & sValue & vbLf
Next
WScript.Echo "Multi String=" & Line
End Select
Next


My System SpecsSystem Spec
Old 10-21-2009   #3 (permalink)
Matthew


 
 

Re: Help with VBScripts

On Oct 21, 10:13*am, "Pegasus [MVP]" <n...@newsgroup> wrote:
Quote:

> "Matthew" <mkr...@newsgroup> wrote in message
>
> news:7e34de29-f141-4ccd-8d99-043f675b9b8e@newsgroup
>
>
>
Quote:

> > Run from the command line CheckREGKeys.vbs. The script seems to work
> > and least for not finding a key or a value but no matter what valid
> > key I use the script all ways tells me its invalid
>
Quote:

> > cscript CheckREGKeys.vbs HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft
> > \Windows\WindowsUpdate
>
Quote:

> > Any ideas?
>
Quote:

> > All I am trying to do is to see if a key/value exists and then see if
> > that value is equal to some specified value.
>
Quote:

> > [code= Save as CheckREGKeys.vbs]
> > '************************************
> > '* Registry Item Exists (Function)
> > '* Returns a value (true / false)
> > '************************************
> > 'This function checks to see if a passed registry key/value exists,
> > and
> > 'returns true if it does
> > 'Original Sourcehttp://www.tek-tips.com/faqs.cfm?fid=5864
> > 'Extended by Matthew Kruer to handel external arguments.
> > '
> > 'Requirements: The registry key/value you are looking for
> > (RegistryItem)
> > 'Note: RegistryItem MUST end in a backslash (\) if you are looking for
> > a key
> > ' * * *RegistryItem's without a backslash (\) will assume you are
> > looking for a value
> > option explicit
> > Dim objArgs, strArg
> > Set objArgs = WScript.Arguments
> > If (WScript.Arguments.Count = 1) Then
> > *Call RegistryItemExists (objArgs(0))
> > WScript.Quit
> > Else
> > *WScript.Echo "USAGE: CheckREGKeys.vbs keyname"
> > *WScript.Quit
> > End If
>
Quote:

> > '************************************
> > '* Registry Item Exists (Function)
> > '* Returns a value (true / false)
> > '************************************
> > 'This function checks to see if a passed registry key/value exists,
> > and
> > 'returns true if it does
> > '
> > 'Requirements: The registry key/value you are looking for
> > (RegistryItem)
> > 'Note: RegistryItem MUST end in a backslash (\) if you are looking for
> > a key
> > ' * * *RegistryItem's without a backslash (\) will assume you are
> > looking for a value
> > Function RegistryItemExists (RegistryItem)
> > *'If there isnt the item when we read it, it will return an error, so
> > we need to resume
> > *On Error Resume Next
> > * WScript.Echo("Looking for Key or Value")
> > *'Find out if we are looking for a key or a value
> > *If (Right(RegistryItem, 1) = "\") Then
> > * *'It's a registry key we are looking for
> > * *'Try reading the key
> > WScript.Echo("Looking for Key")
> > WScript.echo RegistryItem
> > WshShell.RegRead RegistryItem
> > * *'Catch the error
> > * *Select Case Err
> > * * *'Error Code 0 = 'success'
> > * * *Case 0:
> > * * * *RegistryItemExists = true
> > WScript.Echo("1. " & RegistryItem)
> > * * *'This checks for the (Default) value existing (but being blank);
> > as well as key's not existing at all (same error code)
> > * * *Case &h80070002:
> > * * * *'Read the error description, removing the registry key from
> > that description
> > * * * *ErrDescription = Replace(Err.description, RegistryItem, "")
> > wscript.echo ErrDescription
> > * * * *'Clear the error
> > * * * *Err.clear
>
Quote:

> > * * * *'Read in a registry entry we know doesn't exist (to create an
> > error description for something that doesnt exist)
> > * * * *WshShell.RegRead "HKEY_ERROR\"
>
Quote:

> > * * * *'The registry key exists if the error description from the
> > HKEY_ERROR RegRead attempt doesn't match the error
> > * * * *'description from our RegistryKey RegRead attempt
> > * * * *If (ErrDescription <> Replace(Err.description, "HKEY_ERROR\",
> > "")) Then
> > * * * * *RegistryItemExists = true
> > * WScript.Echo("Check Found Key " & RegistryItem)
> > * * * *Else
> > * * * * *RegistryItemExists = false
> > * WScript.Echo("Check Failed for Key " & RegistryItem)
> > * * * *End If
> > * * *'Any other error code is a failure code
> > * * *Case Else:
> > * * * *RegistryItemExists = false
> > WScript.Echo("Check Failed: Unknown Failure while looking for " &
> > RegistryItem)
> > * *End Select
> > *Else
> > * *'It's a registry value we are looking for
> > * *'Try reading the value
> > WScript.Echo("Looking for Value")
> > * *WshShell.RegRead RegistryItem
> > * *'Catch the error
> > * *Select Case Err
> > * * *Case 0:
> > * * * *'Error Code 0 = 'success'
> > * * * *RegistryItemExists = true
> > WScript.Echo("Check Found Value for " & RegistryItem)
> > * * *Case Else
> > * * * *'Any other error code is a failure code
> > * * * *RegistryItemExists = false
> > WScript.Echo("Check could not find a value for " & RegistryItem)
> > * *End Select
> > *End If
> > *'Turn error reporting back on
> > *On Error Goto 0
> > End Function
> > [code]
>
> I use the script below for this type of task. It's shorter and it works.
>
> 'Enumerate all values found in the nominated registry key.
> 'http://www.microsoft.com/technet/scriptcenter/guide/sas_reg_famr.mspx...
> Const HKLM=&H80000002
> Const REG_SZ = 1
> Const REG_EXPAND_SZ = 2
> Const REG_BINARY = 3
> Const REG_DWORD = 4
> Const REG_MULTI_SZ = 7
> Const sKeyPath = "SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU"
> Set oReg=GetObject("winmgmts:\\.\root\default:StdRegProv")
> oReg.EnumValues HKLM, sKeyPath, aEntryNames, aValueTypes
>
> If VarType(aEntryNames) = 1 Then
> * *WScript.Echo "No values found"
> * *WScript.Quit
> End If
>
> For i = 0 To UBound(aEntryNames)
> * *WScript.Echo "Name: " & aEntryNames(i)
> * * Select Case aValueTypes(i)
> * * * *Case REG_SZ
> * * * * * oReg.GetStringValue HKLM, sKeyPath, aEntryNames(i),sValue
> * * * * * WScript.Echo "String=" & sValue
> * * * *Case REG_EXPAND_SZ
> * * * * * oReg.GetExpandedStringValue HKLM, sKeyPath, aEntryNames(i),
> esValue
> * * * * * WScript.Echo "Expanded String=" & esValue
> * * * *Case REG_BINARY
> * * * * * oReg.GetBinaryValue HKLM, sKeyPath, aEntryNames(i),aValues
> * * * * *Line = ""
> * * * * *For Each byteValue In aValues
> * * * * * * *Line = Line & byteValue
> * * * * * Next
> * * * * * WScript.Echo "Binary=" & Line
> * * * *Case REG_DWORD
> * * * * * oReg.GetDWORDValue HKLM, sKeyPath, aEntryNames(i),dwValue
> * * * * * WScript.Echo "DWORD=" & dwValue
> * * * *Case REG_MULTI_SZ
> * * * * * oReg.GetMultiStringValue HKLM, sKeyPath, aEntryNames(i), aValues
> * * * * *Line = ""
> * * * * * For Each sValue In aValues
> * * * * * * *Line = Line & sValue & vbLf
> * * * * * Next
> * * * * * WScript.Echo "Multi String=" & Line
> *End Select
> Next
Thanks for the follow up. I have come across that particular script
before. I should be able to modify it. My requirements are a bit more
convoluted. In a nutshell all the systems were cloned and deployed
with the incorrect WSUS information if any. I need to verify the
server in the registry if the key exists, and if it doesn't then i
need to delete another key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft
\Windows\CurrentVersion\WindowsUpdate. This will force the system to
register with the WSUS box. I was hoping to make it modular enough
that i could use it to check other things in the registry. All this
would be run during the initial logon for the box.
My System SpecsSystem Spec
Old 10-21-2009   #4 (permalink)
Pegasus [MVP]


 
 

Re: Help with VBScripts


"Matthew" <mkruer@newsgroup> wrote in message
news:eae77240-031e-4454-bd95-507ab390b926@newsgroup
<snip>
Thanks for the follow up. I have come across that particular script
before. I should be able to modify it. My requirements are a bit more
convoluted. In a nutshell all the systems were cloned and deployed
with the incorrect WSUS information if any. I need to verify the
server in the registry if the key exists, and if it doesn't then i
need to delete another key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft
\Windows\CurrentVersion\WindowsUpdate. This will force the system to
register with the WSUS box. I was hoping to make it modular enough
that i could use it to check other things in the registry. All this
would be run during the initial logon for the box.

=============

The script I posted can easily be modified to suit your requirement. You can
use it to read any value of any type in any hive.


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Create mutiple PCs in the Domain using vbScripts VB Script
What is the executable associated with running VBScripts? VB Script
VBScripts in Vista Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46