自定义Windows服务调用Powershell执行命令交换32位与64位问题?

发布于 2024-11-29 01:17:12 字数 1288 浏览 3 评论 0原文

我开发了一个在 Visual Studio 2010 中开发的 Windows 服务,该服务执行 Exchange 2007 管理任务,并且我正在将其从 32 位开发服务器迁移到 64 位生产服务器。该服务接受参数,然后调用 PowerShell 在 Exchange 2007 管理单元中执行命令。

安装后,它可以在调试器和 Windows 服务本身的开发环境中正常工作;不幸的是,当我将项目复制到新的 Windows 2008 R2 64 位服务器并构建它时,我只能让该服务在 VS2010 调试器中工作。当作为 Windows 服务安装在 64 位服务器上时,该服务始终会失败,并显示来自代码的 PowerShell 组件的消息:

“没有为 Windows PowerShell 版本 2 注册管理单元”。

C# 代码在特定的服务器上失败调用 PowerShell 实例时的行:

RunspaceConfiguration rc; PSSnapInInfo info = rc.AddPSSSnapIn(“Microsoft.Exchange.Management.PowerShell.Admin”, 警告);

正如我提到的,只要我在 VS2010 调试器中运行该代码,它就可以工作。

为了尝试解决这个问题,我尝试用谷歌搜索该错误,并发现许多其他人也遇到此问题,但没有人对此有明确的答案。按照其中一些帖子中留下的一些线索,我尝试了以下操作:

  • 我删除并重新安装了 Exchange 2007 管理工具
  • 我从 Exchange Management Shell PowerShell 提示符中执行了“Get-PSSnapin”(并返回Exchange 管理工具的列表如下所示 - 该版本表示它适用于 PSVersion 1.0 而不是 2.0,但是,由于在我的 VS2010 调试器中工作正常,我认为 psversion 是不是导致问题的原因): o 名称:Microsoft.Exchange.Management.PowerShell.Admin o PS版本:1.0 o 描述:Exchange Server 的管理任务
  • 我尝试使用 C:\Windows\Microsoft.NET\Framework64\v2.0.50727 下的 INSTALLUTIL 来注册管理单元 o Installutil c:\program files\microsoft\exchange server\bin\microsoft.exchange.management.dll

但我仍然坚持在调试器中运行代码,直到我可以让它作为服务工作。

从我读到的消息的主旨来看,这是一个很多人都遇到过的问题,但迄今为止还没有人能够完全解决。有人可以提供任何帮助吗?

I’ve developed a windows service developed in Visual Studio 2010 which performs Exchange 2007 administrative tasks and I’m moving it from a 32-bit development server to a 64-bit production server. The service accepts arguments and then invokes PowerShell to execute commands in the Exchange 2007 snap in.

It works correctly in the development environment both in the debugger and in the windows service itself after it’s been installed; unfortunately, when I copied the project to the new Windows 2008 R2 64-bit server and built it I can only get the service to work in the VS2010 debugger. When installed as a windows service on the 64-bit server the service consistently fails with a message from the PowerShell component of the code which reads:

“No snap-ins have been registered for Windows PowerShell version 2”

The C# code fails on a specific line when the PowerShell instance is invoked:

RunspaceConfiguration rc;
PSSnapInInfo info = rc.AddPSSSnapIn(“Microsoft.Exchange.Management.PowerShell.Admin”, out warning);

As I mentioned, the code will work so long as I run it in the VS2010 debugger.

To try and fix this I’ve tried Googling the error and come up with many other people having this problem but no-one having a definitive answer to it. Following some of the leads left in some of these postings I’ve tried these things:

  • I have removed and re-installed the Exchange 2007 management tools
  • I have done a “Get-PSSnapin” from the Exchange Management Shell PowerShell prompt (and got back a listing for the Exchange admin tools that looks like this – the version says that it’s for PSVersion 1.0 rather than 2.0 but, since things work in my VS2010 debugger, I thought that the psversion was not what was causing the problem):
    o Name: Microsoft.Exchange.Management.PowerShell.Admin
    o PSVersion: 1.0
    o Description: Admin Tasks for the Exchange Server
  • I’ve tried registering the snap-in using INSTALLUTIL under the C:\Windows\Microsoft.NET\Framework64\v2.0.50727 as
    o Installutil c:\program files\microsoft\exchange server\bin\microsoft.exchange.management.dll

But I’m still stuck running the code in the debugger until I can get it working as a service.

From the tenor of the messages I’ve read about this this is a problem that a lot of people have had but no-one’s been able to completely fix as yet. Can anyone offer any help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泼猴你往哪里跑 2024-12-06 01:17:12

在使用 C# 开发的服务管理 Exchange 2007 时,我遇到了非常类似的问题。

不仅要确保您的代码已编译为针对 x86 平台,还要确保您有对 C:\Program Files 的特定引用调用 PowerShell 运行空间的类中的 (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0\System.Management.Automation.dll 文件。

I had a very similar problem when managing Exchange 2007 using a service developed in C#

Make sure not only that your code is compiled to target the x86 platform but also make sure that you have a specific reference to the C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0\System.Management.Automation.dll file in the class which invokes the PowerShell runspace.

‘画卷フ 2024-12-06 01:17:12

该管理单元可能只是 32 位的。重新编译您的服务以针对 x86 平台(如果您托管 PowerShell)或直接从 C:\Windows\SysWOW64\WindowsPowerShell\v1.0 运行 32 位版本的 PowerShell.exe 应该可以解决问题。

That Snap-In is probably 32 bit only. Recompiling your service to target the x86 platform (if you are hosting PowerShell) or running the 32 bit version of PowerShell.exe directly from C:\Windows\SysWOW64\WindowsPowerShell\v1.0 should clear things up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文