views:

84

answers:

1

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).

A: 

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.

Brian