Sign in to follow this  
Followers 0
AutoMT

Search registry for key

9 posts in this topic

Hi guys, this is an annoying one for me because I know what needs to be done but I don't know how to use the code that does it...

Basically what I want to do is go to the "Volatile Environment" folder in HKEY_USERS on a remote machine, so I can read from the key "HOMEPATH". However the folder the contains this key varies depending on the machine and/or session. For example, on my machine it's located at

HKEY_USERS\S-1-5-21-614741574-1238569373-1831341646-6937021\Volatile Environment

However there are a lot of subkeys(?) under HKEY_USERS...

Posted Image

...and their names vary depending on machine. I need to search through those to find which one contains "Volatile Environment".

I'm pretty sure I need to use StringSplit and/or StringRegExp, but I'm not completely sure if that's the case and how exactly to go about using them. All I've done so far is used RegEnumKey to list everything under HKEY_USERS but I'll be damned if I can get any further than that I'm afraid...

Run("notepad")
Sleep(500)

For $i= 1 to 10
    $key = RegEnumKey("HKEY_USERS", $i)
    If @error <> 0 then ExitLoop
    Send($key & @CRLF)
Next

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

This works and has been tested. I used ConsoleWrite instead of notepad, so the output should should up at the bottom of SciTe if you use it. But what you needed shows up in the MsgBox at the end anyway:

Global $rootKey = "HKEY_USERS"
Global $targetString = "Volatile Environment"
Global $keyFound = ""
Global $i = 0
Global $n = 0

ConsoleWrite(@CR)

While 1
    $i += 1
    $subkey = $rootKey & "\" & RegEnumKey($rootKey, $i)
    If @error Or StringInStr($subkey, "No more data") Then ExitLoop
    ConsoleWrite($subkey & @CR)
    While 1
        $n += 1
        $subSubKey = $subkey & "\" & RegEnumKey($subkey, $n)
        If StringInStr($subSubKey, $targetString) Then
            $keyFound = $subSubKey
            ExitLoop 2
        EndIf
        If @error Or StringInStr($subSubKey, "No more data") Then
            $n = 0
            ExitLoop 1
        EndIf
        ConsoleWrite($subSubKey & @CR)
    WEnd
WEnd
$homedrive = RegRead($keyFound, "HOMEDRIVE"); such as "C:"
$homepath = RegRead($keyFound, "HOMEPATH"); such as "\Documents and Settings\USER"
$fullpath = $homedrive & $homepath
MsgBox(0, "", _
            "Volatile Environment Key: " & $keyFound _
    & @CR & "HOMEPATH: " & $homepath _
    & @CR & "HOMEDRIVE: " & $homedrive _
    & @CR & "Fullpath: " & $fullpath)

If you are not using SciTe, the ConsoleWrite functions are not necessary in this code.

Edited by Squirrely1

Das Häschen benutzt Radar

Share this post


Link to post
Share on other sites

Thanks for that, that was awesome and exactly what I wanted! Using that code I've now written a script that retrieves PC names from simple TXT files and goes through each one and finds its volatile environment so it can tell me who's logged on on, it's great! Thanks for your help.

Hey I might just ask another question in this topic save making a new one. You know how when you run a script in SciTE, and when it completes, it displays the time it took to run the script (">Exit code: 0 Time: 116.603" for example). Is there anyway I can get that data so I can maybe output it to a file?

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

ConsoleRead won't work except in a compiled script, I read in the help file.

But if you expect the script to be running for several seconds and you don't need a time that is completely precise, you could use TimerInit near the front of the script and TimerDiff near the end, writing its output to the text file.

This code worked for me:

$file = "C:\My File.txt"

$Tstamp = TimerInit()

Sleep(3000);rest of script goes here in place of this Sleep...

$result = String(Round(TimerDiff($Tstamp) / 1000))
FileWriteLine($file, "Execution duration: " & $result & " seconds")
Edited by Squirrely1

Das Häschen benutzt Radar

Share this post


Link to post
Share on other sites

That worked nicely! I have another question though... Is there anyway to have a timeout on a command? For example, a RegRead? It'd be handy if I could have it timeout if it takes too long to connect to the registry of a remote computer.

Share this post


Link to post
Share on other sites

This is the only thing to come to my somewhat experienced coding-mind:

If Ping($remoteAddress, 5000) Then
    RegRead(...)
EndIf

Das Häschen benutzt Radar

Share this post


Link to post
Share on other sites

Okay I'll try that. My last question (hopefully), is it possible to open multiple instances of SciTE at the same time? I have three versions of this script, one for each set of remote PCs (some are located further away than others), and I'd like to be running all three at once with the SciTE console open so I know where each one is up to (I used a lot of ConsoleWrites).

Share this post


Link to post
Share on other sites

Okay I'll try that. My last question (hopefully), is it possible to open multiple instances of SciTE at the same time? I have three versions of this script, one for each set of remote PCs (some are located further away than others), and I'd like to be running all three at once with the SciTE console open so I know where each one is up to (I used a lot of ConsoleWrites).

It is probably possible to have other instances of SciTe open - you would need to adjust either the Global settings .properties file or the User .properties file:

SciTe > Options menu > Open User Options file

or

SciTe > Options menu > Open Global Options file

If you do this, you should first study the SciTe Help file:

SciTe > Help menu > SciTe Documentation > Scroll down to "Defined variables in properties files" section of the page

But aside from many - almost unlimited number of them - undo buffers, SciTe has file buffers - you can have many .au3 files open at the same time, of course, all in just one instance of SciTe.


Das Häschen benutzt Radar

Share this post


Link to post
Share on other sites

Nice! I was just trying to do something similar and didn't have the looping logic quite right. Thanks for that.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0