Sign in to follow this  
Followers 0
SaintedRogue

Issues with Sending Variable Command

4 posts in this topic

Ok, so I am working on a script to backup up the HKU Registry settings live on a system. I have most of it working properly. I am able to get the necessary user SID and put them into a variable. To back them up, I want to run the following command:

REG SAVE HKU\<Insert SID Here> <Save Location>\#.dat

Basically, I have that command working with this code here:

Local $s_Out = ""
$h_Proc = Run(@ComSpec & " /c " & "Reg query hku", "", @SW_HIDE, 0x08)
While 1
    $s_Out &= StdoutRead($h_Proc)
    If @Error Then ExitLoop
WEnd
$aLines = StringRegExp($s_Out, "(?m:^)\h*\S.+(?:\v|$)+", 3)
If NOT @Error Then
For $i = 0 To Ubound($aLines) -1
  $s_Val = $aLines[$i]
;  MsgBox(0, "Result " & $i+1, $s_Val)
Next
EndIf
;Use Array to Backup HKU
;Send("REG{space}SAVE{space}HKEY_USERS\" & $aLines+1 & "{space}" & @ScriptDir & "\Evidence\" &.dat")
;Open Command Prompt
Run("cmd.exe")
;Match Windows by SubString
Opt("WinTitleMatchMode", 2)
;Wait until the program opens.
WinWaitActive("cmd")
If NOT @Error Then
For $i = 0 To Ubound($aLines) -1
  $s_Val = $aLines[$i]
  ;MsgBox(0, "Result " & $i+1, $s_Val)
  ;Send("REG{space}SAVE{space}HKEY_USERS\" & $s_Val & "{space}" & @ScriptDir & "\Evidence\" & $i+1 & ".dat")
  Run("cmd /c REG SAVE " & $s_Val & " " & @ScriptDir & "\Evidence\" & $i+1 & ".dat")
Next
EndIf

But my issue comes to this. When the Command Prompt opens it enters properly, however with the variable it hits enter early and breaks the command. Is there anyway I can send the whole command? I have tried sending RAW as well and the variable does the same thing.

All thoughts and help are welcomed, and appreciated.

Share this post


Link to post
Share on other sites



Your main issue is that @ScriptDir likely contains spaces so is not treated as a single parameter.

Wrapping it in quotes (which can be done a half dozen ways ) ought to fix you up:

#include <Array.au3> ; test
DirCreate(@ScriptDir & "Evidence") ; test
Local $aLines, $x

While 1
  $x += 1
  $aLines &= RegEnumKey("HKU", $x) & "|"
  If @error Then ExitLoop
WEnd
$aLines = StringSplit(StringTrimRight($aLines, 2), "|")
_ArrayDisplay($aLines) ; test

For $i = 1 To $aLines[0]
  ToolTip("Processing: " & $aLines[$i] & "..."); test
  RunWait("cmd /c REG SAVE HKU" & $aLines[$i] & ' "' & @ScriptDir & "Evidence" & $i & '.dat"', "", @SW_HIDE)
Next

Share this post


Link to post
Share on other sites

The problem is that the folder @ScriptDir & "Evidence" probably doesn't exist on the machine you're running the script on. I ran your script on my computer and it appeared that it worked, but nothing was created. As soon as I created the Evidence folder it worked. Try this code:

If Not FileExists(@ScriptDir & "Evidence") Then DirCreate(@ScriptDir & "Evidence")
Local $s_Out = ""
$h_Proc = Run(@ComSpec & " /c " & "Reg query hku", "", @SW_HIDE, 0x08)
While 1
    $sTemp = StdoutRead($h_Proc)
    $s_Out &= $sTemp
    If @error Then ExitLoop
WEnd
$aLines = StringRegExp($s_Out, "(?m:^)h*S.+(?:v|$)+", 3)
Run("cmd.exe")
;Match Windows by SubString
Opt("WinTitleMatchMode", 2)
;Wait until the program opens.
WinWaitActive("cmd")
If Not @error Then
    For $i = 0 To UBound($aLines) - 1
        $s_Val = $aLines[$i]
        $s_Val = StringStripWS($s_Val, 2)
        ;MsgBox(0, "Result " & $i+1, $s_Val)
        ;Send("REG{space}SAVE{space}HKEY_USERS" & $s_Val & "{space}" & @ScriptDir & "Evidence" & $i+1 & ".dat")
        Run("cmd /c REG SAVE " & $s_Val & " " & @ScriptDir & "Evidence" & $i + 1 & ".dat /y") ; <<< The /y is in case it's run on the same machine more than once, it overwrites the existing file.
    Next
EndIf

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Sir, thank you so much for simplifying a problem I've spent 2.5 weeks on. HAHAH! You're amazing!

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