This topic contains 5 replies, has 3 voices, and was last updated by
November 19, 2018 at 10:45 am #125546
Working with registry in PowerShell. I export a key using reg export for backup and that works fine and as expected, but when I run reg import to restore from the backup, it returns as an error although it is successful.
I saw this post: https://powershell.org/forums/topic/reg-exe-throws-an-error-on-success-in-remote-session/
But in my case, this is not on a remote machine and I do not want to suppress errors, but capture and handle them.
The key I export and import is in the HKEY_CLASSES_ROOT hive.
The import does work(!), just returns an exception as a result for some reason.
I can do as suggested in the post above and then check $lastexitcode but prefer to do something more elegant.
PS V5.1 but requires compatibility to V4
November 19, 2018 at 6:30 pm #125646
November 22, 2018 at 10:52 am #126194
Yes, I am aware that reg.exe is a windows cmd tool, but it is PowerShell that, for some reason, interprets success as an error
All I do is something like this:
reg export HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\AssemblyFolders\v3.5 c:\test11.reg reg import c:\test.reg
When I run it in cmd, it works fine, the output is "The operation completed successfully." on both commands
However, in PowerShell, the first commands yields the same result: "The operation completed successfully."
But the second results in this:
reg : The operation completed successfully. At line:1 char:1 + reg import c:\test.reg + ~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (The operation completed successfully.:String) , RemoteException + FullyQualifiedErrorId : NativeCommandError
Now, when I try to assign these to a variable, like:
$regexport = reg export HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\AssemblyFolders\v3.5 c:\test11.reg $regimport = reg import c:\test.reg
The value of $regexport will be "The operation completed successfully."
But the value of $regimport will be "", although it is successful. I need to know the import was successful.
So yes, I can check $lastexitcode, but the behavior seems very strange
The "exception" is only happening in ISE, but I don't get the value also in the console
November 22, 2018 at 11:20 am #126200ParticipantPoints: 259Rank: Contributor
Obviously reg import has a bug. The success message appears on the error channel. You can catch this when you use Start-Process with the parameter -RedirectStandardError.
Start-Process -FilePath reg -ArgumentList 'import c:\test.reg' -NoNewWindow -Wait -RedirectStandardError C:\reg-error.log Get-Content -Path C:\reg-error.log Remove-Item -Path C:\reg-error.log
November 22, 2018 at 11:31 am #126203
November 22, 2018 at 2:20 pm #126212ParticipantPoints: 832Rank: Major Contributor
Yu can do like below with native executables.
$regimport = reg import c:\test.reg 2>&1 $regimport
You must be logged in to reply to this topic.