Link to home
Start Free TrialLog in
Avatar of sqlagent007
sqlagent007Flag for United States of America

asked on

unable to use the FailOverClusters module in a .ps1 file when calling the file from c#

Error when trying to call a .ps1 file from c# the "FailoverClusters" module is not available. I am trying to provide some self service administration to people via a web page and the first step in that is getting c# to call a .ps1 file using the Get-Cluster command. It appears that when I call the .ps1 from c# most of the modules are available in that run space but the "FailoverClusters" is not. When I create just a test file to run this command from the c# call, it returns nothing.
get-module -ListAvailable | Where-Object {$_.Name -like "*clust*"}

Open in new window


However when I run the above code in the powershell console as me, I see the module name "FailoverClusters".

I don't know what c# is running under, but whatever user it is running under (assumed me since I am clicking the debug button), does not appear to have access to the FailOverClusters module or commands.

See code....

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using Microsoft.FailoverClusters.PowerShell;

PowerShell posh = PowerShell.Create();
            posh.AddCommand(@"C:\test\Test.ps1");
           
            var results = posh.Invoke();
            Console.Out.WriteLine("Done");
            foreach (var result in results)
            {
                Console.Out.WriteLine(result.BaseObject.ToString());
            }
            Console.Out.WriteLine("Done");
            Console.In.ReadLine();

Open in new window

Avatar of sqlagent007
sqlagent007
Flag of United States of America image

ASKER

It appears that I need to configure the project for a 64 bit platform. https://docs.microsoft.com/en-us/visualstudio/ide/how-to-configure-projects-to-target-platforms?view=vs-2019

however when I do that I get the error: The process has been terminated. Refresh the process list before attempting another attach.

I can only debug in "any cpu" and according to the internet the FailOverClusters module only works in 64 bit mode. I am stuck here and I don't know what to do. Has anybody out the successfully called .ps1 files for system administration from C#?
Ok, so I am not convinced the issue is with the fact that c# is calling 32 bit PoSh and console is using 64 bit PoSh. Can anybody help me get a c# project that can call a .ps1 file using code like listed below?

    PowerShell posh = PowerShell.Create();
            posh.AddCommand(@"C:\Scripts\myWindowsAdminStuff.ps1");

Open in new window


The code above will use the "FailOverClusters" module and that only works in 64 bit Shell...ugg.....
ASKER CERTIFIED SOLUTION
Avatar of sqlagent007
sqlagent007
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial