Created
October 15, 2018 14:34
-
-
Save jpginc/7ac37afae3d55602433b1ad4f56941ec to your computer and use it in GitHub Desktop.
simple powershell in c#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Windows; | |
using System.Windows.Controls; | |
using System.Management.Automation; | |
using System.Management.Automation.Runspaces; | |
namespace powershell | |
{ | |
public partial class Page1 : Page | |
{ | |
public Page1() | |
{ | |
InitializeComponent(); | |
} | |
private void Button_Click(object sender, RoutedEventArgs e) | |
{ | |
string command = @in.Text; | |
@out.Text += command + "\r\n"; | |
RunspaceConfiguration rspacecfg = RunspaceConfiguration.Create(); | |
Runspace rspace = RunspaceFactory.CreateRunspace(rspacecfg); | |
rspace.Open(); | |
Pipeline pipeline = rspace.CreatePipeline(); | |
pipeline.Commands.AddScript(command); | |
pipeline.InvokeAsync(); | |
while (pipeline.PipelineStateInfo.State == PipelineState.Running || pipeline.PipelineStateInfo.State == PipelineState.Stopping) | |
{ | |
System.Threading.Thread.Sleep(50); | |
} | |
foreach (object item in pipeline.Output.ReadToEnd()) | |
{ | |
if (item != null) | |
@out.Text += item.ToString() + "\r\n"; | |
} | |
foreach (object item in pipeline.Error.ReadToEnd()) | |
{ | |
if (item != null) | |
@out.Text += item.ToString() + "\r\n"; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment