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.
This topic is 7 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
phurious
Posts: 15
Last visit: Tue Oct 15, 2019 12:06 pm

Forms Newbie

Post by phurious »

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?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Forms Newbie

Post by jvierra »

Without you code it is not possible to guess at what you are doing.
User avatar
phurious
Posts: 15
Last visit: Tue Oct 15, 2019 12:06 pm

Re: Forms Newbie

Post by phurious »

I have put together an example that demonstrates what I am seeing:
  1. $aRegistry = @("HKCU\printers","HKLM\SOFTWARE\Logitech\Logitech Gaming Software")
  2. $regPath = "D:\"
  3. $i = 0
  4. $j = 0
  5. ForEach ($r in $aRegistry)
  6. {
  7.     If ($r.Substring(0, 4) -eq "HKCU")
  8.     {
  9.         [String]$reg = ($r -replace 'HKCU','HKCU:')
  10.         If (Test-Path $reg)
  11.         {
  12.             Write-Host "True"
  13.             $argument = "Export $([char]34)$r$([char]34) $([char]34)$regPath\HKCU$i.reg$([char]34)"
  14.             Start-Process -FilePath C:\Windows\System32\reg.exe -ArgumentList $argument
  15.             $i++
  16.         }
  17.     }
  18.     Else
  19.     {
  20.         [String]$reg = ($r -replace 'HKLM', 'HKLM:')       
  21.         If (Test-Path $reg)
  22.         {
  23.             Write-Host "True"
  24.             $argument = "Export $([char]34)$r$([char]34) $([char]34)$regPath\HKLM$j.reg$([char]34)"
  25.             Start-Process -FilePath C:\Windows\System32\reg.exe -ArgumentList $argument
  26.             $j++
  27.         }
  28.     }
  29. }
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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Forms Newbie

Post by jvierra »

Like this:
  1. #ArgumentList is an array if you want Windows to manage the strings.
  2. $filePath='d:\pathfile.reg'
  3. $argList = @(
  4.     'Export',
  5.     $r,
  6.     $filePath
  7. )
  8. Start-Process -FilePath C:\Windows\System32\reg.exe -ArgumentList $argsList
User avatar
phurious
Posts: 15
Last visit: Tue Oct 15, 2019 12:06 pm

Re: Forms Newbie

Post by phurious »

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

I assumed the abbreviated line in your code read:
  1. Start-Process -FilePath C:\Windows\System32\reg.exe -ArgumentList $argsList
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Forms Newbie

Post by jvierra »

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
Last visit: Tue Oct 15, 2019 12:06 pm

Re: Forms Newbie

Post by phurious »

Do you have any recommendations on how to capture the error being generated?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Forms Newbie

Post by jvierra »

You may need to do it like this:
  1. $argList = @(
  2.     'Export',
  3.     ('"{0}"' -f $regPath),
  4.     $regfile,
  5.     '/Y'
  6. )
  7. Start-Process -FilePath C:\Windows\System32\reg.exe -ArgumentList $argList -NoNewWindow
User avatar
phurious
Posts: 15
Last visit: Tue Oct 15, 2019 12:06 pm

Re: Forms Newbie

Post by phurious »

I ran the following command:
  1. $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 . . .
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Forms Newbie

Post by jvierra »

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.
This topic is 7 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked