Yes, I admit I'm an engineer. I'm new to this but I'm thinking
logically here. I'm looking for if..than statement examples. Yes,
I've found tons but it really doesn't explain anything to me. For
example, I'm looking for a basic script that would say If you find a
particular registry key with a particular string value, than delete
it. If you you don't find a particular registry key with it's string
value, than move on to the rest of the script. I can't find anything
like that. I can't even find if you find the key, rename it, modify
it, smile at it......

Am I too much of an engineer and this is working my left brain, the
side I've probably never touched, probably rotted out???? I've
created a delreg key and it works fine - see:

******************************************************
Const HKLM = &H80000002
sKey = "SOFTWARE\Lotus\Notes"

Set oReg = GetObject _
("WinMgmts:{impersonationLevel=impersonate}!//./root/default:StdRegProv")

DeleteRegistryKey HKLM, sKey

Sub DeleteRegistryKey(ByVal sHive, ByVal sKey)
Dim aSubKeys, sSubKey, iRC
On Error Resume Next
iRC = oReg.EnumKey(sHive, sKey, aSubKeys)
If iRC = 0 And IsArray(aSubKeys) Then
For Each sSubKey In aSubKeys
If Err.Number <> 0 Then
Err.Clear
Exit Sub
End If
DeleteRegistryKey sHive, sKey & "\" & sSubKey
Next
End If
oReg.DeleteKey sHive, sKey
End Sub
*****************************************************************

Not bad for an engineer - whatever.... Anyway, is this something that
can be done? Thanks to anyone that can help me out, point me in the
right direction.

Re: Help w/ the Basics (if...then) - Okay, you can tell I'm an Engineer!! by Torgeir

Torgeir
Wed Jun 23 04:51:50 CDT 2004

Robin wrote:

> Yes, I admit I'm an engineer. I'm new to this but I'm thinking
> logically here. I'm looking for if..than statement examples. Yes,
> I've found tons but it really doesn't explain anything to me. For
> example, I'm looking for a basic script that would say If you find a
> particular registry key with a particular string value, than delete
> it. If you you don't find a particular registry key with it's string
> value, than move on to the rest of the script. I can't find anything
> like that.
Hi

To check if a registry key or value exists, you can use the
RegKeyExists and RegValueExists functions in the scripts in
the links below.

With WMI:
http://groups.google.com/groups?selm=4027FB99.5DFF5864%40hydro.com

With the WshShell's RegRead method ("pure" WSH code):
http://groups.google.com/groups?selm=%23t94ENtLEHA.2532%40TK2MSFTNGP10.phx.gbl


An example:

Const HKLM = &H80000002

Set oReg = GetObject _
("WinMgmts:{impersonationLevel=impersonate}!//./root/default:StdRegProv")

sRegKey = "HKLM\Software\MyKey"
sRegValue = sRegKey & "\MyValue"

If RegValueExists(sRegValue) Then

' delete the registry key
DeleteRegistryKey HKLM, sRegKey

Else

' if the value didn't exist, script contines here

End If


Function RegValueExists(sRegValue)
' Returns True or False based of the existence of a registry value.
Dim oShell, RegReadReturn
Set oShell = CreateObject("WScript.Shell")
RegValueExists = True ' init value
On Error Resume Next
RegReadReturn = oShell.RegRead(sRegValue)
If Err.Number <> 0 Then
RegValueExists = False
End if
On Error Goto 0
End Function


Sub DeleteRegistryKey(ByVal sHive, ByVal sKey)
Dim aSubKeys, sSubKey, iRC
On Error Resume Next
iRC = oReg.EnumKey(sHive, sKey, aSubKeys)
If iRC = 0 And IsArray(aSubKeys) Then
For Each sSubKey In aSubKeys
If Err.Number <> 0 Then
Err.Clear
Exit Sub
End If
DeleteRegistryKey sHive, sKey & "\" & sSubKey
Next
End If
oReg.DeleteKey sHive, sKey
End Sub



--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/community/scriptcenter/default.mspx

Re: Help w/ the Basics (if...then) - Okay, you can tell I'm an Engineer!! by jerzdevs

jerzdevs
Wed Jun 23 09:51:01 CDT 2004

Thanks, I'm sort of getting this now. Let me ask you something else.

Layout:
- Within my registry I have a Key named Software (everyone does)
- a SubKey named Lotus
- with another SubKey named Notes
- within Notes, there's a StringValue named KeyPath.

Goal:
- I'm trying to see if a string value meets c:\notesclient.
- If it does, than delete the registry.
- If it's anything else, than continue the script.

Question:
The code that you showed me, does that search StringValues or just
SubKeys? I tried running your code but nothing happened.

Thanks again.

"Torgeir Bakken \(MVP\)" <Torgeir.Bakken-spam@hydro.com> wrote in message news:<#KmiOgQWEHA.2908@TK2MSFTNGP10.phx.gbl>...
> Robin wrote:
>
> > Yes, I admit I'm an engineer. I'm new to this but I'm thinking
> > logically here. I'm looking for if..than statement examples. Yes,
> > I've found tons but it really doesn't explain anything to me. For
> > example, I'm looking for a basic script that would say If you find a
> > particular registry key with a particular string value, than delete
> > it. If you you don't find a particular registry key with it's string
> > value, than move on to the rest of the script. I can't find anything
> > like that.
> Hi
>
> To check if a registry key or value exists, you can use the
> RegKeyExists and RegValueExists functions in the scripts in
> the links below.
>
> With WMI:
> http://groups.google.com/groups?selm=4027FB99.5DFF5864%40hydro.com
>
> With the WshShell's RegRead method ("pure" WSH code):
> http://groups.google.com/groups?selm=%23t94ENtLEHA.2532%40TK2MSFTNGP10.phx.gbl
>
>
> An example:
>
> Const HKLM = &H80000002
>
> Set oReg = GetObject _
> ("WinMgmts:{impersonationLevel=impersonate}!//./root/default:StdRegProv")
>
> sRegKey = "HKLM\Software\MyKey"
> sRegValue = sRegKey & "\MyValue"
>
> If RegValueExists(sRegValue) Then
>
> ' delete the registry key
> DeleteRegistryKey HKLM, sRegKey
>
> Else
>
> ' if the value didn't exist, script contines here
>
> End If
>
>
> Function RegValueExists(sRegValue)
> ' Returns True or False based of the existence of a registry value.
> Dim oShell, RegReadReturn
> Set oShell = CreateObject("WScript.Shell")
> RegValueExists = True ' init value
> On Error Resume Next
> RegReadReturn = oShell.RegRead(sRegValue)
> If Err.Number <> 0 Then
> RegValueExists = False
> End if
> On Error Goto 0
> End Function
>
>
> Sub DeleteRegistryKey(ByVal sHive, ByVal sKey)
> Dim aSubKeys, sSubKey, iRC
> On Error Resume Next
> iRC = oReg.EnumKey(sHive, sKey, aSubKeys)
> If iRC = 0 And IsArray(aSubKeys) Then
> For Each sSubKey In aSubKeys
> If Err.Number <> 0 Then
> Err.Clear
> Exit Sub
> End If
> DeleteRegistryKey sHive, sKey & "\" & sSubKey
> Next
> End If
> oReg.DeleteKey sHive, sKey
> End Sub

Re: Help w/ the Basics (if...then) - Okay, you can tell I'm an Engineer!! by Torgeir

Torgeir
Wed Jun 23 10:05:44 CDT 2004

Torgeir Bakken (MVP) wrote:

> Robin wrote:
>
>> Yes, I admit I'm an engineer. I'm new to this but I'm thinking
>> logically here. I'm looking for if..than statement examples. Yes,
>> I've found tons but it really doesn't explain anything to me. For
>> example, I'm looking for a basic script that would say If you find a
>> particular registry key with a particular string value, than delete
>> it. If you you don't find a particular registry key with it's string
>> value, than move on to the rest of the script. I can't find anything
>> like that.
>
> Hi
>
> To check if a registry key or value exists, you can use the
> RegKeyExists and RegValueExists functions in the scripts in
> the links below.
>
> With WMI:
> http://groups.google.com/groups?selm=4027FB99.5DFF5864%40hydro.com
>
> With the WshShell's RegRead method ("pure" WSH code):
> http://groups.google.com/groups?selm=%23t94ENtLEHA.2532%40TK2MSFTNGP10.phx.gbl
>
>
>
> An example:
>
> Const HKLM = &H80000002
>
> Set oReg = GetObject _
>
> ("WinMgmts:{impersonationLevel=impersonate}!//./root/default:StdRegProv")
>
> sRegKey = "HKLM\Software\MyKey"
> sRegValue = sRegKey & "\MyValue"
>
> If RegValueExists(sRegValue) Then
>
> ' delete the registry key
> DeleteRegistryKey HKLM, sRegKey
>
> Else
>
> ' if the value didn't exist, script contines here
>
> End If
Hi

My example was untested, and I see now that the DeleteRegistryKey
callout didn't use the proper format for how WMI needs it to be,
here is an updated version (the sRegKeyWMI variable is added):

Const HKLM = &H80000002

Set oReg = GetObject _
("WinMgmts:{impersonationLevel=impersonate}!//./root/default:StdRegProv")

sRegKeyWMI = "Software\MyKey"
sRegKey = "HKLM\Software\MyKey"
sRegValue = sRegKey & "\MyValue"

If RegValueExists(sRegValue) Then

' delete the registry key
DeleteRegistryKey HKLM, sRegKeyWMI

Else

' if the value didn't exist, script contines here

End If


--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/community/scriptcenter/default.mspx

Re: Help w/ the Basics (if...then) - Okay, you can tell I'm an Engineer!! by Torgeir

Torgeir
Wed Jun 23 10:09:26 CDT 2004

Robin wrote:

> Thanks, I'm sort of getting this now. Let me ask you something else.
>
> Layout:
> - Within my registry I have a Key named Software (everyone does)
> - a SubKey named Lotus
> - with another SubKey named Notes
> - within Notes, there's a StringValue named KeyPath.
>
> Goal:
> - I'm trying to see if a string value meets c:\notesclient.
> - If it does, than delete the registry.
> - If it's anything else, than continue the script.
>
> Question:
> The code that you showed me, does that search StringValues or just
> SubKeys? I tried running your code but nothing happened.
Hi

No searching is done, you need to give the correct and exact
registry path/names to make it work. Also note the other post
I just made that made a correction to one of the input parameters
to the DeleteRegistryKey callout.


Here is a script that should fulfill your goal listed above:


Const HKLM = &H80000002

Set oReg = GetObject _
("WinMgmts:{impersonationLevel=impersonate}!//./root/default:StdRegProv")

sRegKeyWMI = "Software\Lotus\Notes"
sRegKey = "HKLM\" & sRegKeyWMI
sRegValue = sRegKey & "\KeyPath"

If LCase(RegRead(sRegValue)) = "c:\notesclient" Then
' delete the registry key HKLM\Software\Lotus\Notes
DeleteRegistryKey HKLM, sRegKeyWMI

Else

' if the value didn't meet the criteria, script contines here

End If


Function RegRead(sRegValue)
Set oShell = CreateObject("WScript.Shell")
On Error Resume Next
RegRead = oShell.RegRead(sRegValue)
' If the value does not exist, error is raised
If Err Then
RegRead = ""
Err.clear
End If
' If a value is present but uninitialized the RegRead method
' returns the input value in Win2k.
If VarType(RegRead) < vbArray Then
If RegRead = sRegValue Then
RegRead = ""
End If
End If
On Error Goto 0
End Function


Sub DeleteRegistryKey(ByVal sHive, ByVal sKey)
Dim aSubKeys, sSubKey, iRC
On Error Resume Next
iRC = oReg.EnumKey(sHive, sKey, aSubKeys)
If iRC = 0 And IsArray(aSubKeys) Then
For Each sSubKey In aSubKeys
If Err.Number <> 0 Then
Err.Clear
Exit Sub
End If
DeleteRegistryKey sHive, sKey & "\" & sSubKey
Next
End If
oReg.DeleteKey sHive, sKey
End Sub


--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/community/scriptcenter/default.mspx