0
votes

I have just started writing my first Groovy script, in which I want to read the value of a string from the Windows registry and print the last part of what is returned, to the screen.

This is what I have:

group = Runtime.getRuntime().exec('reg query HKEY_LOCAL_MACHINE\\SOFTWARE\\App\\UDF1 -v PatchRebootGroup');
System.out.println "patchRebootGroup=" + group;

The output of the reg command looks like this:

HKEY_LOCAL_MACHINE\SOFTWARE\App\UDF1

PatchRebootGroup REG_SZ Group1

The question is, "How do I print out just 'patchRebootGroup=Group1'?

Thanks.

2

2 Answers

1
votes

Assuming process you are trying to execute generates following output:

HKEY_LOCAL_MACHINE\SOFTWARE\App\UDF1

PatchRebootGroup REG_SZ Group1

you can firstly split input String by \n, take the last line, split it by single space and read first and the last part. Something like this:

def process = Runtime.runtime.exec('reg query HKEY_LOCAL_MACHINE\\SOFTWARE\\App\\UDF1 -v PatchRebootGroup')

def parts = process.text.split('\n')[1].split(" ")

def output = "${parts[0]}=${parts[2]}"

println output

Output

PatchRebootGroup=Group1

You can also try following way to initialize Process object:

def process = ['reg', 'query', 'HKEY_LOCAL_MACHINE\\SOFTWARE\\App\\UDF1', '-v', 'PatchRebootGroup'].execute()
0
votes

Here is what I ended up using:

String output = 'reg query HKEY_LOCAL_MACHINE\\SOFTWARE\\App\\UDF1 -v PatchRebootGroup'.execute().text
String group = output.tokenize(' ')[-1]
println 'DesiredPatchRebootGroup=' + group
return 0