title: Running Powershell from .Net Core - Could not load file or assembly Microsoft.Management.Infrastructure
body: <p>I have been trying to run a powershell script from a .Net Core Web app (not discussing best practices here ;) ) with the following code:</p>
  
  <pre><code>    string command = @"&amp; """c:\\my Folder\\myScript.ps1""";
  
      using (var ps = PowerShell.Create())
      {
          var results = ps.AddScript(command).Invoke();
      }
  </code></pre>
  
  <p>It works well on my dev machine, but in production it fails when trying to execute this function:</p>
  
  <pre><code>ps.AddScript(command).Invoke()
  </code></pre>
  
  <p>I get the following exception:</p>
  
  <blockquote>
    <p>System.IO.FileNotFoundException: Could not load file or assembly
    'Microsoft.Management.Infrastructure, Version=1.0.0.0,
    Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot
    find the file specified. File name:
    'Microsoft.Management.Infrastructure, Version=1.0.0.0,
    Culture=neutral, PublicKeyToken=31bf3856ad364e35'    at
    System.Reflection.RuntimeAssembly.GetExportedTypes(RuntimeAssembly
    assembly, ObjectHandleOnStack retTypes)    at
    System.Reflection.RuntimeAssembly.GetExportedTypes()    at
    System.Management.Automation.Runspaces.PSSnapInHelpers.GetAssemblyTypes(Assembly
    assembly, String name)    at
    System.Management.Automation.Runspaces.PSSnapInHelpers.AnalyzeModuleAssemblyWithReflection(Assembly
    assembly, String name, PSSnapInInfo psSnapInInfo, PSModuleInfo
    moduleInfo, Boolean isModuleLoad, Dictionary<code>2&amp; cmdlets, Dictionary</code>2&amp;
    aliases, Dictionary<code>2&amp; providers, String helpFile, Type&amp;
    randomCmdletToCheckLinkDemand, Type&amp; randomProviderToCheckLinkDemand) 
    at
    System.Management.Automation.Runspaces.PSSnapInHelpers.AnalyzePSSnapInAssembly(Assembly
    assembly, String name, PSSnapInInfo psSnapInInfo, PSModuleInfo
    moduleInfo, Boolean isModuleLoad, Dictionary</code>2&amp; cmdlets, Dictionary<code>2&amp;
    aliases, Dictionary</code>2&amp; providers, String&amp; helpFile)    at
    System.Management.Automation.Runspaces.InitialSessionState.ImportPSSnapIn(PSSnapInInfo
    psSnapInInfo, PSSnapInException&amp; warning)    at
    System.Management.Automation.Runspaces.InitialSessionState.CreateDefault()
    at
    System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(PSHost
    host)    at
    System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace()
    at
    System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace
    rsToUse, Boolean isSync)    at
    System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCollection<code>1
    input, PSDataCollection</code>1 output, PSInvocationSettings settings)    at
    System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection<code>1
    input, PSDataCollection</code>1 output, PSInvocationSettings settings)    at
    System.Management.Automation.PowerShell.CoreInvoke[TOutput](IEnumerable
    input, PSDataCollection`1 output, PSInvocationSettings settings)    at
    System.Management.Automation.PowerShell.Invoke(IEnumerable input,
    PSInvocationSettings settings)</p>
  </blockquote>
  
  <p>I don't know which framework/package I am suppose to install to make it run.
  Target Framework is .Net Core 2.1 which is installed, hence the application running fine except line mentionned above.</p>
  
  <p>The deps.json file contains this:</p>
  
  <pre><code>    "Microsoft.Management.Infrastructure/1.0.0": {
          "dependencies": {
            "NETStandard.Library": "2.0.3",
            "System.Runtime.CompilerServices.VisualC": "4.3.0",
            "System.Runtime.Serialization.Xml": "4.3.0",
            "System.Security.SecureString": "4.3.0",
            "System.Threading.ThreadPool": "4.3.0"
          },
          "compile": {
            "ref/netstandard1.6/Microsoft.Management.Infrastructure.Native.dll": {},
            "ref/netstandard1.6/Microsoft.Management.Infrastructure.dll": {}
          }
        }
  </code></pre>
  
  <p>Nuget Packages installed:</p>
  
  <pre><code>Microsoft.AspNetCore.App (2.1.1)
  Microsoft.PowerShell.Commands.Diagnostics (6.0.5)
  Microsoft.PowerShell.SDK (6.0.5)
  Microsoft.PowerShell.WSMan.Management (6.0.5)
  </code></pre>
  
  <p><strong>Edit</strong>
      I also added Microsoft.Management.Infrastructure (1.0.0)
  but it didn't fix the issue</p>
  
  <p><strong>Edit2</strong>
      Dev is Windows 10 Pro and Prod is Windows Server 2016 Standard</p>
  
  <p><strong>Edit3</strong>
      The script runs fine on prod when launched through PowerShell directly. The error shown is when the Web app tries to run PowerShell.</p>
  
  <p><strong>Edit4</strong>
      App pool account has admin rights and is the same account that I used to run the script manually (not good practice but for now I'm just trying to make this run).</p>
  
  <p><strong>Edit5</strong>
      I tried to put the dll in the folder of the app on the server and restart the site + recycle the application pool. I tried with every version I could find on the dev machine and nothing worked :/</p>
  
  <p><strong>Edit6</strong>
      Updated nuget packages  from 6.0.5 to latest 6.2.1 , same issue</p>