【发布时间】:2020-04-28 15:00:02
【问题描述】:
this stack post 和 this one 看起来都很相似,但我在那里找不到我的解决方案。我在理解 reg 导出与 reg 导入中的行为之间的区别时遇到了问题。
在 powershell 中运行 reg export 时,它返回“操作已成功完成”,但 reg import 会抛出一个终止错误,提示“操作已成功完成”。注册表文件被正确导入,即使它被抛出为错误。一个例子:
PS C:\Windows\System32> reg export HKLM\Software\MySoftware C:\Scripts\MyFile.reg
The operation completed successfully.
PS C:\Windows\System32> reg import C:\Scripts\MyFile.reg
reg : The operation completed successfully.
At line:1 char:1
+ reg import C:\Scripts\MyFile.reg
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (The operation completed successfully.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Microsoft's documentation of the reg import command 表示返回码是 0 表示成功,1 表示失败。为什么 Powershell 会将成功作为 reg 导出的常规成功消息返回,但将其作为 reg 导入的终止错误抛出?
我以管理员身份运行 Powershell,登录用户是服务器上的本地管理员。感谢您提供任何帮助。
【问题讨论】:
-
$out = .{ reg import "\\$CurServer\MySharedPath\RegistryBackup.reg" } 2>$null -
这会忽略任何错误,因此当我故意指定文件名的错误路径时,它不会引发任何错误。
-
你不能在 regedit 调用之后测试
$LASTEXITCODE,然后抛出你自己的异常。 -
我更新了文章以删除 Invoke-Command 问题,只关注 reg export 和 reg import 之间的区别。 $LASTEXITCODE 返回 0,如果我们查看有关 reg 导入的 Microsoft 文章,这似乎是正确的。问题是为什么 reg export 将其作为常规消息返回,而 reg import 将其作为终止错误抛出。
-
哦,你的意思是我使用 $LASTEXITCODE 来创建我自己的异常,其中 if 0 then this if 1 then that,对不起,我没有完全阅读你的声明!是的,我认为这是一种解决方案 :) 虽然它看起来不太好,但我在 catch 中解决问题只是因为它总是抛出错误而做一个 try/catch。
标签: powershell registry remote-registry