Forms Newbie

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
phurious
Posts: 15
Joined: Sun Mar 03, 2013 12:42 pm

Forms Newbie

Post by phurious » Thu Jun 02, 2016 10:19 am

I am attempting to export some registry settings in one of the routines in my form. The routine gets the content of a text file, which is a list of registry keys, and then uses REG.EXE /EXPORT to complete the export. When I run the packaged script most keys seem to export correctly, but the few which have a space in them fail. What is more strange is that if I copy and paste the code directly into a console all keys export successfully, even those with spaces. In an attempt to work around the issue, I had the script output the REG commands to a BAT file which I then called from the script. The exact same issue occurred; any registry key with a space in the path failed to export. However, if I run the BAT file created by the script manually all registry key export successfully. What am I missing?

User avatar
jvierra
Posts: 13623
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Forms Newbie

Post by jvierra » Thu Jun 02, 2016 10:46 am

Without you code it is not possible to guess at what you are doing.

User avatar
phurious
Posts: 15
Joined: Sun Mar 03, 2013 12:42 pm

Re: Forms Newbie

Post by phurious » Thu Jun 02, 2016 11:32 am

I have put together an example that demonstrates what I am seeing:

Code: Select all

$aRegistry = @("HKCU\printers","HKLM\SOFTWARE\Logitech\Logitech Gaming Software")
$regPath = "D:\"
$i = 0
$j = 0
ForEach ($r in $aRegistry)
{
If ($r.Substring(0, 4) -eq "HKCU")
{
[String]$reg = ($r -replace 'HKCU','HKCU:')
If (Test-Path $reg)
{
Write-Host "True"
$argument = "Export $([char]34)$r$([char]34) $([char]34)$regPath\HKCU$i.reg$([char]34)"
Start-Process -FilePath C:\Windows\System32\reg.exe -ArgumentList $argument
$i++
}
}
Else
{
[String]$reg = ($r -replace 'HKLM', 'HKLM:')
If (Test-Path $reg)
{
Write-Host "True"
$argument = "Export $([char]34)$r$([char]34) $([char]34)$regPath\HKLM$j.reg$([char]34)"
Start-Process -FilePath C:\Windows\System32\reg.exe -ArgumentList $argument
$j++
}
}
}
If the code is "Run" from within Powershell Studio only the first registry key, the one without a space in the path, is exported. If it is packaged into an EXE and run, same thing. However, if you choose to "Run in console" within Powershell Studio both registry keys export successfully.

User avatar
jvierra
Posts: 13623
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Forms Newbie

Post by jvierra » Thu Jun 02, 2016 11:55 am

Like this:

Code: Select all

#ArgumentList is an array if you want Windows to manage the strings.
$filePath='d:\pathfile.reg'
$argList = @(
'Export',
$r,
$filePath
)
Start-Process -FilePath C:\Windows\System32\reg.exe -ArgumentList $argsList

User avatar
phurious
Posts: 15
Joined: Sun Mar 03, 2013 12:42 pm

Re: Forms Newbie

Post by phurious » Thu Jun 02, 2016 12:19 pm

I changed the code as you have described and it is still not working.

I assumed the abbreviated line in your code read:

Code: Select all

Start-Process -FilePath C:\Windows\System32\reg.exe -ArgumentList $argsList

User avatar
jvierra
Posts: 13623
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Forms Newbie

Post by jvierra » Thu Jun 02, 2016 12:40 pm

Sorry. I didn't notice the truncation.
You have to retrieve the error message. Without knowing what the error is we cannot determine what is happening.

User avatar
phurious
Posts: 15
Joined: Sun Mar 03, 2013 12:42 pm

Re: Forms Newbie

Post by phurious » Thu Jun 02, 2016 12:47 pm

Do you have any recommendations on how to capture the error being generated?

User avatar
jvierra
Posts: 13623
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Forms Newbie

Post by jvierra » Thu Jun 02, 2016 12:52 pm

You may need to do it like this:

Code: Select all

$argList = @(
'Export',
('"{0}"' -f $regPath),
$regfile,
'/Y'
)
Start-Process -FilePath C:\Windows\System32\reg.exe -ArgumentList $argList -NoNewWindow

User avatar
phurious
Posts: 15
Joined: Sun Mar 03, 2013 12:42 pm

Re: Forms Newbie

Post by phurious » Thu Jun 02, 2016 1:16 pm

I ran the following command:

Code: Select all

$RegError = cmd /c reg.exe export "$r" "$regPath\HKLM$j.reg" 2`>`1
The error I am receiving is:
ERROR: The system was unable to find the specified registry key or value.

Because I am 100% certain the registry key exists it would seem to indicate there is something wrong with the quotations on the command line. Not sure what I can change . . .

User avatar
jvierra
Posts: 13623
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Forms Newbie

Post by jvierra » Thu Jun 02, 2016 1:43 pm

I am sorry but I cannot help you if you do not follow instructions.
Please try the code I just posted. YU cannot just make up strings t test when you are trying to understand what the code is doing. You will just end up chasing new and unrelated errors.

Locked