0

I'm trying to access PowerShell from within C# and it seems as though I need to reference the namespace System.Management.Automation and I can't find it anywhere I've searched my C: drive, google and my MSDN subscription (searching for "powershell SDK") and I can't find anything.

So my questions are:

  1. Is this the namespace where I'll find the PowerShell object referenced in the codeblock below (found here)?

  2. If not what namespace do I need and where can I find it?

    using (PowerShell PowerShellInst = PowerShell.Create())
    {
    
        PowerShell ps = PowerShell.Create();
    
        string param1= "my param";
        string param2= "another param";
        string scriptPath = <path to script>;
    
        ps.AddScript(File.ReadAllText(scriptPath));
    
        ps.AddArgument(param1);
        ps.AddArgument(param2);
    
        ps.Invoke();
    }
    
Ben_G
  • 770
  • 2
  • 8
  • 30
  • 1
    Yes, [System.Management.Automation](https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.powershell?view=powershellsdk-7.0.0) is the correct namespace. – Mathias R. Jessen Jan 19 '22 at 00:02
  • So where can I find that? – Ben_G Jan 19 '22 at 00:13
  • I'm not sure I understand the question. Are you asking which assembly/package you need to reference to be able to resolve `System.Management.Automation.PowerShell` in your own project? Or are you asking about where to find documentation? – Mathias R. Jessen Jan 19 '22 at 00:20
  • I'm asking which assembly/package you need to reference to be able to resolve System.Management.Automation.PowerShell in your own project. And where can I find this assembly/package. I've seen the docs. Can't find the assembly. Thought I just found it in NuGet, but that turned into a nightmare of dependencies that isn't working. I actually installed the NuGet package but I don't see it in my References and I can't instantiate any code based on it. – Ben_G Jan 19 '22 at 00:38
  • 1
    The following may be helpful: https://stackoverflow.com/questions/58211358/how-to-automate-either-powershell-or-powershell-core-for-same-machine/58211901#58211901 – Tu deschizi eu inchid Jan 19 '22 at 00:38

1 Answers1

1
    using System.Collections.ObjectModel;
    using System.Management.Automation;
    using System.Management.Automation.Runspaces;
n0rd
  • 11,850
  • 5
  • 35
  • 56
levels
  • 11
  • 1