Solved

Running Nutanix Powershell CmdLets from C# application


Badge
Hello all,

I have a situation where I need to inter-operate with Powershell scripts from my C# code. The powershell works fine on its own but I am getting an error when running the scripts from my C# code. I realize that this might be a Microsoft problem but the error is referencing the Nutanix NutanixCMDletsPSSnapin. The snapin is installed and works just fine from Powershell ISE and PS command prompt.

C#:
code:

RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
PSSnapInException psex = new PSSnapInException();
runspaceConfiguration.AddPSSnapIn("NutanixCMDletsPSSnapin",out psex);
using (Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration))
{
runspace.Open();
RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
string path = Path.Combine(Environment.CurrentDirectory, "GetClonesForVolumeGroups.ps1");
Pipeline pipeline = runspace.CreatePipeline();
Command scriptCommand = new Command(path);
scriptCommand.Parameters.Add("volumeGroupName", "testmtvolumegroup");
pipeline.Commands.Add(scriptCommand);
Collection psObjects = pipeline.Invoke();
}



code:

Powershell: ("GetClonesForVolumeGroups.ps1")

param(
[Parameter(Mandatory=$true)]
[string]$volumeGroupName)

Add-PSSnapin NutanixCMDletsPSSnapin
. "$PSScriptRoot\ConnectToNutanixCluster.ps1"
if(Connect-NutanixCluster)
{
$clones = Get-NTNXVolumeGroups | Where-Object{$_.name -like $volumeGroupName + '*' -and $_.name -ne $volumeGroupName}
return $clones
}





I get the following error:
System.Management.Automation.PSArgumentException: 'The Windows PowerShell snap-in 'NutanixCMDletsPSSnapin' is not installed on this computer.'
icon

Best answer by Jeremy Rammalaere 2 October 2018, 18:03

I found the solution. Despite the fact that the Nutanix Powershell Cmdlets are installed in Program Files (x86), it is actaully a 64-bit assembly. Once I set my application to 64-bit explicitly, everything worked as expected.

View original

1 reply

Badge
I found the solution. Despite the fact that the Nutanix Powershell Cmdlets are installed in Program Files (x86), it is actaully a 64-bit assembly. Once I set my application to 64-bit explicitly, everything worked as expected.

Reply