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

Re: Forms Newbie

Post by phurious » Thu Jun 02, 2016 2:08 pm

Sorry, the code you posted does not work unless you added more to it that you did not post.

Code: Select all

# The $regPath variable is the location where the exported REG file will be saved. Set to D:\ for testing.
# The $regFile variable exists nowhere in my code, so I am not sure what you are using it for.
# In my script this argument statement would read as:
# 'Export',
# ("'{0}'" -f D:\),
# unknown,
# "/Y'
$argList = @(
'Export',
('"{0}"' -f $regPath),
$regfile,
'/Y'
)
Start-Process -FilePath C:\Windows\System32\reg.exe -ArgumentList $argList -NoNewWindow
Running this gave me no information. I changed it to:

Code: Select all

$argList = @(
'Export',
('"{0}"' -f $r),
"$regPath\HKLM1.REG",
'/Y'
)
Start-Process -FilePath C:\Windows\System32\reg.exe -ArgumentList $argList -NoNewWindow
Even after I changed the code it still returned nothing.

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

Re: Forms Newbie

Post by jvierra » Thu Jun 02, 2016 3:17 pm

I ran that code with the exact same data and it worked fine.

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

Re: Forms Newbie

Post by jvierra » Thu Jun 02, 2016 3:23 pm

This one also works for me:

Code: Select all

$regPaths = @(
'HKCU\printers',
'HKLM\SOFTWARE\Logitech\Logitech Gaming Software'
)
$filePath = 'D:\'

ForEach ($regPath in $regPaths) {
$regKey = ($regPath -replace '^HKCU', 'HKCU:') -replace '^HKLM', 'HKLM:'
$regfile = $filePath+($regPath -replace '\\', '-') + '.reg'
If (Test-Path $regKey) {
Write-Host "$regKey found!" -fore green
$regfile = '"'+$filePath + ($regPath -replace '\\', '-') + '.reg"'
Write-Host $regfile -fore Blue
$argList = @(
'Export',
('"{0}"' -f $regPath),
$regfile,
'/Y'
)
Start-Process -FilePath C:\Windows\System32\reg.exe -ArgumentList $argList -NoNewWindow
} else {
Write-Host "$regKey NOT found!" -fore red
}
}

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

Re: Forms Newbie

Post by phurious » Thu Jun 02, 2016 5:34 pm

Cut/Paste/and Ran the code you posted and this is what I get:

Image

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

Re: Forms Newbie

Post by jvierra » Thu Jun 02, 2016 6:04 pm

The key does not exist on you machine. I cannot help you with that. Either you have the wrong name for the key or it does not exisit on your machine.

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

Re: Forms Newbie

Post by phurious » Fri Jun 03, 2016 4:14 am

You can clearly see in the screencap the code is reporting that the key does not exist, but the window overlaying the Powershell Studio window is the registry editor opened to the exact key in question.

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

Re: Forms Newbie

Post by phurious » Fri Jun 03, 2016 4:29 am

This also helps further illustrate my original question. When the code is "Run" from within Powershell Studio, the script reports the key is not found:

Image



But if the exact same code on the same machine is "Run in Console" from within Powershell Studio, the script finds the key:

Image

The credentials are the same, so why does running the script by clicking "Run" or packaging it into an EXE fail?

User avatar
monoeagle
Posts: 107
Joined: Tue Oct 13, 2015 9:10 am

Re: Forms Newbie

Post by monoeagle » Fri Jun 03, 2016 5:03 am

Possibly a problem with the plattform? =)

Your Key is from the x64 Hive, your Script on the first picture is running under v4 x86 plattform.

On your second screen the console is running as x64.

Check your Configuration.

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

Re: Forms Newbie

Post by phurious » Fri Jun 03, 2016 6:09 am

monoeagle wrote:Possibly a problem with the plattform? =)

Your Key is from the x64 Hive, your Script on the first picture is running under v4 x86 plattform.

On your second screen the console is running as x64.

Check your Configuration.
Couldn't see the forest for the trees . . . this is indeed the issue. Thank you Monoeagle!

Next question, this code has to run on both x86 and x64 systems so I am packaging it as 32 bit. How can I successfully export the registry keys on both x64 and x86? I already have a function to determine bitness built into the script but I am unfamiliar with having to address bitness dealing with the registry.
Last edited by phurious on Fri Jun 03, 2016 8:37 am, edited 1 time in total.

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

Re: Forms Newbie

Post by jvierra » Fri Jun 03, 2016 7:11 am

You must specify which hive you are querying.

The key is clearly a main hive key. If you run it as a 64 bit script on a 64 bit machine it will work correctly. The same on a 32 bit machine.

Locked