在 WiX 安装中从 C# 自定义操作调用 PowerShell 命令
背景:我正在编写 SharePoint 组件的安装程序。除了将软件安装到目标机器上之外,我还想正确配置它。 SharePoint(尤其是 2010 年)通过 PowerShell 公开其管理功能。因此,我编写了一个 C# 自定义操作来调用一系列命令,如下所示:
Runspace runSpace = RunspaceFactory.CreateRunspace();
runSpace.Open();
Pipeline pipeline = runSpace.CreatePipeline();
Command addSnapin = new Command("Add-PSSnapin");
addSnapin.Parameters.Add("Name", "Microsoft.SharePoint.Powershell");
pipeline.Commands.Add(addSnapin);
pipeline.Invoke();
Command getSearchApp = new Command("Get-SPEnterpriseSearchServiceApplication");
pipeline = runSpace.CreatePipeline();
pipeline.Commands.Add(getSearchApp);
Object searchApp = pipeline.Invoke().First().BaseObject;
/* pass searchApp to other PS cmdlets */
问题: 当我从测试可执行文件运行此代码时,它工作正常。但是,从自定义操作运行时它不起作用。它失败,并且日志包含消息“术语‘Get-SPEnterpriseSearchServiceApplication’未被识别为 cmdlet 的名称...”。该 cmdlet 应该由 Add-PSSnapin 命令导入(据我所知,该命令成功了)。
问题: 当相同的命令序列在 PS 控制台中工作并在我的测试程序中运行时,为什么 PS 找不到该函数? 有没有更简单的方法来完成这个过程? WiX 似乎对复杂的 PS 自定义操作没有太多支持(并不是说我的很复杂,但它不是一句简单的话)。
Background: I'm writing an installation for a SharePoint component. In addition to getting the software onto the target machine, I want to properly configure it. SharePoint (esp. 2010) exposes its management functionality via PowerShell. So I wrote a C# custom action to invoke a series of commands, like this:
Runspace runSpace = RunspaceFactory.CreateRunspace();
runSpace.Open();
Pipeline pipeline = runSpace.CreatePipeline();
Command addSnapin = new Command("Add-PSSnapin");
addSnapin.Parameters.Add("Name", "Microsoft.SharePoint.Powershell");
pipeline.Commands.Add(addSnapin);
pipeline.Invoke();
Command getSearchApp = new Command("Get-SPEnterpriseSearchServiceApplication");
pipeline = runSpace.CreatePipeline();
pipeline.Commands.Add(getSearchApp);
Object searchApp = pipeline.Invoke().First().BaseObject;
/* pass searchApp to other PS cmdlets */
Problem:
When I run this code from a test executable, it works fine. However, it does not work when run from a custom action. It fails, and the log contains the message "The term 'Get-SPEnterpriseSearchServiceApplication' is not recognized as the name of a cmdlet...". This cmdlet is supposed to be imported by the Add-PSSnapin
command (which succeeded, as far as I can tell).
Questions:
Why can PS not find the function, when the same command sequence works in the PS console and when run in my test program?
Is there any easier way to go about this process? WiX didn't seem to have much support for complex PS custom actions (not that mine is very complicated, but it's not a one-liner).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种特殊情况下,管理单元无法加载,因为自定义操作作为 32 位进程运行。我更改了构建设置以将其编译为 x64,然后它开始工作。 Keith 的建议很有帮助(+1) - 我认为失败会导致抛出异常,但事实并非如此。
In this particular case, the Snapin couldn't load because the custom action was running as a 32-bit process. I changed the build settings to compile it as x64 and it started working. Keith's suggestion was helpful (+1) - I assumed that a failure would result in an exception being thrown, but this is not the case.