3

I am using .NET Core to write some automation with Powershell, I installed the following nugets:

Microsoft.PowerShell.Commands.Management
Microsoft.PowerShell.SDK
Microsoft.WSMan.Management
System.Management.Automation

and is using the following code:

Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();

Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript("$service = Get-CimInstance -ClassName Win32_Service -Filter \"name = 'MyNewService\'\"");

pipeline.Commands.AddScript("$service.DisplayName");

Getting the following Error

System.Management.Automation.CommandNotFoundException: 'The term 'Get-CimInstance' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.'

Any Ideas how to fix the problem, so it will recognize the command?

Nkosi
  • 235,767
  • 35
  • 427
  • 472
Dan The Man
  • 1,835
  • 6
  • 30
  • 50
  • it is possible the module with that command is not installed or loaded. are you able to run the cmdlt manually? – Nkosi Jul 22 '18 at 09:41
  • CIM was introduced in PowerShell Version 3. Maybe you're still on version 2? – Theo Jul 22 '18 at 10:04
  • @Theo I am using version 6. – Dan The Man Jul 22 '18 at 11:16
  • what is your `System.Management.Automation` version? – Avshalom Jul 22 '18 at 13:06
  • `Import-Module CIMCmdlets`? Running from a command line, I take for granted that the standard stuff is in the psmodulepath. Perhaps that isn't something you get for free when creating your own runspace. – veefu Jul 22 '18 at 13:59
  • @Avshalom using System.Management.Automation 6.0.2 version – Dan The Man Jul 22 '18 at 14:06
  • @veefu using Import-Module CIMCmdlets, and got the same error. – Dan The Man Jul 22 '18 at 14:11
  • Another shot int he dark: how about providing a runspace configuration that includes the cim cmdlets assembly? https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.runspaces.runspacefactory.createrunspace?view=powershellsdk-1.1.0#System_Management_Automation_Runspaces_RunspaceFactory_CreateRunspace_System_Management_Automation_Host_PSHost_System_Management_Automation_Runspaces_RunspaceConfiguration_ – veefu Jul 22 '18 at 14:23
  • @veefu does not work. – Dan The Man Jul 23 '18 at 11:11
  • @danieltheman If you're still working on this, I think the answer may lie in InitialSessionState, which turned up while researching an answer to [this question](https://stackoverflow.com/questions/51818599/how-to-call-outside-defined-function-in-runspace-scriptblock) – veefu Aug 13 '18 at 14:07

1 Answers1

0

The problem is actually a compatibility issue with the edition being passed. You can view this by getting the Runspace's SessionStateProxy's error value:

var err=run.SessionStateProxy.PSVariable.GetValue("error");

It will reveal that there is an issue with the edition:

Module 'C:\windows\system32\WindowsPowerShell\v1.0\Modules\CIMCmdlets\CIMCmdlets.psd1' does not support current PowerShell edition 'Core'. Its supported editions are 'Desktop'. Use 'Import-Module -SkipEditionCheck' to ignore the compatibility of this module.

This provides the solution. There is not a way to do this with the InitialSessionState's ImportPSModule so try the following:

using System.Management.Automation;
using System.Management.Automation.Runspaces;
InitialSessionState _initialSessionState = InitialSessionState.CreateDefault2();
_initialSessionState.ExecutionPolicy = Microsoft.PowerShell.ExecutionPolicy.Unrestricted;
var script="$service = Get-CimInstance -ClassName Win32_Service -Filter \"name = 'MyNewService\'\";$service.DisplayName";
using (var run = RunspaceFactory.CreateRunspace(_initialSessionState))
{
       run.Open();
       var ps = PowerShell.Create(run);
       ps.AddCommand("Import-Module");
       ps.AddParameter("SkipEditionCheck");
       ps.AddArgument("CIMcmdlets");
       ps.Invoke();
       var err = run.SessionStateProxy.PSVariable.GetValue("error");
       System.Diagnostics.Debug.WriteLine(err);//This will reveal any error loading
       ps.Commands.AddScript(script);
       var results = ps.Invoke();
       run.Close();
       return results;
}
D. Lockett
  • 119
  • 1
  • 3