title: How can I call the FailoverClusters PowerShell module when working from C#?
body: <p>I’m trying to call a PowerShell script from C#, which is usually very straightforward, except that for some reason the commands in the FailoverClusters module can never be found when calling from C# (not in scope). Here’s what I’ve discovered:</p>
  
  <p>The commands are always found when using an interactive PowerShell session:</p>
  
  <pre><code>PS C:\&gt; Get-Cluster -Name DummyCluster
  
  Name
  ----
  DummyCluster
  </code></pre>
  
  <p>The commands are never found when using a local PowerShell session from C#:</p>
  
  <pre><code>var ps = PowerShell.Create();
  ps.AddCommand("Get-Cluster");
  ps.AddParameter("Name", "DummyCluster");
  var r = ps.Invoke();
  //Exception: command not found
  </code></pre>
  
  <p>I’ve tried about 6-7 different ways to import the module, and none of them worked. Here is one of them, taken from <a href="http://www.codeproject.com/Tips/374052/Import-PowerShell-modules-and-excute-CmdLets-using" rel="nofollow">this</a> tutorial on the subject:</p>
  
  <pre><code>var ps = PowerShell.Create();
  var ss = InitialSessionState.CreateDefault();
  var modules = new string[1]{"FailoverClusters"};
  ss.ImportPSModule(modules);
  var rs = RunspaceFactory.CreateRunspace(ss);
  rs.Open();
  var iv = new RunspaceInvoke(rs);
  var r = iv.Invoke("Get-Cluster -Name DummyCluster");
  //Exception: command not found
  </code></pre>
  
  <p>Interestingly, the commands are found when using a remote PowerShell session. This is a plausible work-around for certain use cases.</p>
  
  <pre><code>var ci = new WSManConnectionInfo(); //localhost remote connection
  var rs = RunspaceFactory.CreateRunspace(ci);
  rs.Open();
  var iv = new RunspaceInvoke(rs);
  var r = iv.Invoke("Get-Cluster -Name DummyCluster");
  //Exception: access is denied
  </code></pre>
  
  <p>The FailoverClusters module is not visible from C#:</p>
  
  <pre><code>var ps = PowerShell.Create();
  ps.AddCommand("Get-Module");
  ps.AddParameter("ListAvailable");
  ps.AddArgument("FailoverClusters");
  var results = ps.Invoke();
  Console.WriteLine(results.Count.ToString()); //prints 0
  </code></pre>
  
  <p>It is, however, visible from an interactive PowerShell session:</p>
  
  <pre><code>PS C:\&gt; Get-Module -ListAvailable FailoverClusters
  
  
      Directory: C:\windows\system32\WindowsPowerShell\v1.0\Modules
  
  
  ModuleType Version    Name                                ExportedCommands
  ---------- -------    ----                                ----------------
  Manifest   2.0.0.0    FailoverClusters                    {Add-ClusterCheckpoint, Add-ClusterDisk, Add-ClusterFileSe...
  </code></pre>