reg import returns (The operation completed successfully.:String) [], RemoteExce

Welcome Forums General PowerShell Q&A reg import returns (The operation completed successfully.:String) [], RemoteExce

This topic contains 5 replies, has 3 voices, and was last updated by

 
Participant
2 weeks, 5 days ago.

  • Author
    Posts
  • #125546

    Participant
    Points: -4
    Rank: Member

    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.

    Any ideas?

    PS V5.1 but requires compatibility to V4

  • #125646

    Participant
    Points: 259
    Helping Hand
    Rank: Contributor

    Hmmm .... that's actually not Powershell at all. reg.exe is a windows command line tool.
    Anyway without seeing your actual code and the exact and complete error message we can barely guess what's wrong.

  • #126194

    Participant
    Points: -4
    Rank: Member

    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

  • #126200

    Participant
    Points: 259
    Helping Hand
    Rank: 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
    • #126203

      Participant
      Points: -4
      Rank: Member

      That works

      Thanks!

  • #126212

    Participant
    Points: 832
    Helping Hand
    Rank: 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.