Run PSCmdLets in C# code (Citrix XenDesktop)

前端 未结 1 1350
一整个雨季
一整个雨季 2020-12-21 12:17

I\'m new to PowerShell and running PowerShell cmd-lets in C#. Specifically, I\'m trying to use Citrix\'s XenDesktop SDK to write a web app to manage our XenDesktop environm

1条回答
  •  醉梦人生
    2020-12-21 13:12

    You need to host the PowerShell engine in order to execute a PSCmdlet e.g. (from the MSDN docs):

      // Call the PowerShell.Create() method to create an 
      // empty pipeline.
      PowerShell ps = PowerShell.Create();
    
      // Call the PowerShell.AddCommand(string) method to add 
      // the Get-Process cmdlet to the pipeline. Do 
      // not include spaces before or after the cmdlet name 
      // because that will cause the command to fail.
      ps.AddCommand("Get-Process");
    
      Console.WriteLine("Process                 Id");
      Console.WriteLine("----------------------------");
    
      // Call the PowerShell.Invoke() method to run the 
      // commands of the pipeline.
      foreach (PSObject result in ps.Invoke())
      {
        Console.WriteLine(
                "{0,-24}{1}",
                result.Members["ProcessName"].Value,
                result.Members["Id"].Value);
      } 
    } 
    

    0 讨论(0)
提交回复
热议问题