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

2
  • 1
    After you execute the Add-PSSnapin command, check the Pipeline.Error property to see if the add is failing and why. Commented Aug 14, 2010 at 0:06
  • Ok, the error collection contains "No snap-ins have been registered for Windows PowerShell version 2." After doing some searching, I realized that my Custom Action is running in a 32-bit process, which probably can't load the SP management Cmdlets. Thanks for the tip. Commented Aug 16, 2010 at 17:03

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.