• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 35664
  • Last Modified:

Check to see if a registry key exist

I would like to see if a registry key exist and if it does exit and if it doesn't install a executable.  Here is a sample of my code:

If strRegKey = oshell.RegRead ("HKLM\Software\Network Associates\ePolicy Orchestrator\Agent") = True Then
WScript.Echo oshell.RegRead(strRegKey)
WScript.Quit
Else oshell.Run "runas /user:domain\username ""\\nindsfs1.ninds.nih.gov\common\security_agent\epo\framepkg.exe /silent /forceinstall /install=agent""", 2
WScript.Sleep 1500
oshell.SendKeys "password~"
End If
0
jcredle
Asked:
jcredle
  • 9
  • 6
  • 2
  • +1
1 Solution
 
T3LogicCommented:
Try This,

If strRegKey = (oshell.RegRead ("HKLM\Software\Network Associates\ePolicy Orchestrator\Agent") Then
WScript.Echo oshell.RegRead(strRegKey)
WScript.Quit
Else oshell.Run "runas /user:domain\username ""\\nindsfs1.ninds.nih.gov\common\security_agent\epo\framepkg.exe /silent /forceinstall /install=agent""", 2
WScript.Sleep 1500
oshell.SendKeys "password~"
End If
0
 
jcredleAuthor Commented:
It gives me an unable to open registry key for reading message with the code number 80070002.
0
 
bdetcheveryCommented:
Just error trap it and test for an error

Err.Clear
On Error Resume Next
strRegKey = oshell.RegRead ("HKLM\Software\Network Associates\ePolicy Orchestrator\Agent") = True Then
If Err.Number <> 0 Then
WScript.Echo oshell.RegRead(strRegKey)
WScript.Quit
Else
oshell.Run "runas /user:domain\username ""\\nindsfs1.ninds.nih.gov\common\security_agent\epo\framepkg.exe /silent /forceinstall /install=agent""", 2
WScript.Sleep 1500
oshell.SendKeys "password~"
End If
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
jcredleAuthor Commented:
This doesn't work either.  I get a unexpected end of statement at line 23 and char 99.  The error code is 800a0401
0
 
T3LogicCommented:
installKey = "HKLM\Software\Network Associates\ePolicy Orchestrator\Agent"
checkKey = oShell.RegRead(installKey)

If Err.number <> 0 Then
      WScript.Echo oshell.RegRead(strRegKey)
                WScript.Quit
Else
      oshell.Run "runas /user:domain\username ""\\nindsfs1.ninds.nih.gov\common\security_agent\epo\framepkg.exe /silent /forceinst                 all /install=agent""", 2
                WScript.Sleep 1500
                oshell.SendKeys "password~"
End If
0
 
bdetcheveryCommented:
Sorry, I didn't see the = True part when I copied and pasted, I did it quickly as I was running out the door:

Should have said:

Err.Clear
On Error Resume Next
strRegKey = oshell.RegRead ("HKLM\Software\Network Associates\ePolicy Orchestrator\Agent")
If Err.Number <> 0 Then
WScript.Echo oshell.RegRead(strRegKey)
WScript.Quit
Else
oshell.Run "runas /user:domain\username ""\\nindsfs1.ninds.nih.gov\common\security_agent\epo\framepkg.exe /silent /forceinstall /install=agent""", 2
WScript.Sleep 1500
oshell.SendKeys "password~"
End If
0
 
cschipperCommented:
We use this script to check if the sms client is installed:

Option Explicit
on error resume next
Dim WSHShell
Dim RegKey
Dim waarde
Dim wshEnvironment
Dim strRunProgram
Set WSHShell = CreateObject("WScript.Shell")
Set wshEnvironment = wshShell.Environment("System")
RegKey = "HKLM\SOFTWARE\Microsoft\SMS\Mobile Client\"
waarde = WSHShell.RegRead(RegKey & "AssignedSiteCode")
if not waarde = "HQ1" Then

Call installsms
Else
WScript.echo "SMS Client is geinstalleerd"
End If

Function installsms()
strRunProgram = "\\prosmshq-02\SMSClient\i386\client.msi"
WshShell.Run strRunProgram, 1, True

End Function
0
 
jcredleAuthor Commented:
Thanks cschipper but your response works for values and not keys.  I need to know if the agent key exists.
0
 
jcredleAuthor Commented:
I think i have it working now cschipper.  I made some adjustments and now my true or false statements work.  I think the reason why it wasn't working before was because i didn't include wshenviroment in my code.
0
 
jcredleAuthor Commented:
I take that back.  It is working regardless if the key is there or not.  Can someone please help.
0
 
bdetcheveryCommented:
Did my revised code not work either ?


Err.Clear
On Error Resume Next
strRegKey = oshell.RegRead ("HKLM\Software\Network Associates\ePolicy Orchestrator\Agent")
If Err.Number <> 0 Then
WScript.Echo oshell.RegRead(strRegKey)
WScript.Quit
Else
oshell.Run "runas /user:domain\username ""\\nindsfs1.ninds.nih.gov\common\security_agent\epo\framepkg.exe /silent /forceinstall /install=agent""", 2
WScript.Sleep 1500
oshell.SendKeys "password~"
End If
0
 
jcredleAuthor Commented:
bdetchevery,

Your suggestion will not run at all.  On the debug screen I get an If without Then on line 4
0
 
bdetcheveryCommented:
Oh, How odd,

hmm...I wonder why it works for me.

The line:

On Error Resume Next

should stop any erros from displaying

Just curious, what environment are you debugging in?
0
 
jcredleAuthor Commented:
I am using vbscript but now it not giving me that message after I brought up the wscript.echo statement and then put it back like the way you had it.
0
 
bdetcheveryCommented:
Okay Back to square 1

I would suggest just trying the following: Copy and paste this in a new .VBS file say we call it keycheck.vbs

Set oshell = CreateObject("WScript.Shell")
Err.Clear
On Error Resume Next
strRegKey = oshell.RegRead ("HKLM\Software\Network Associates\ePolicy Orchestrator\Agent")

If Err.Number <> 0 Then
      'This is where you put code if the key doesn't exist
      WScript.Echo "The Key DOES NOT Exist"
Else
      'This is where you put code if the key DOES exist
      WScript.Echo "The Key Exists"
End If

Then run keycheck.vbs on a system that has the key and a system that doesn't. It should show "The Key DOES NOT Exist" for systems without the key and "The Key Exists with system with the key.

HOWEVER, it is better to look for an actual 'value', string or DWORD if possible. oShell.RegRead with return an error if the KEY exists but has NO DEFAULT value. Many programs may set a key such as "Agent", write other values into the agent key but not set a value for [DEFAULT]. In this situation the code will return "NO KEY EXISTS" even though a key does exist but no DEFAULT value is set.

So if you know that every ePolicy\Orachstra\Agent key will have something other string value in the registry this is what i would check for. Unless you are sure that the Agent Key's default value will always contain something in 'DEFAULT' if it exists.

Hope this helps.
0
 
jcredleAuthor Commented:
It seems that is not reading the registry because it says that it does not exist on both machines.
0
 
bdetcheveryCommented:
If you open up the machine with the key and look in the registry editor under the key do you see a value for default?

If not, this may be your problem, no default value for the key.  If there is another string/value pair this would be the best thing to check for.

You should also make sure the script has correct permissions to read the registry.
0
 
jcredleAuthor Commented:
bdetchevery,

I think that I have it working now with the script you included.  For some reason it would not run correctly with me using the Option Explicit function so I removed it and now it works both ways.  I will now add in the installation path and do more test.  It looks like you will get the points for this.

Thank!
0

Featured Post

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.

  • 9
  • 6
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now