Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.Collections.ObjectModel;
- using System.Management.Automation;
- using System.Management.Automation.Runspaces;
- using System.IO;
- namespace ClientCheck
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- }
- private string RunScript(string scriptText)
- {
- // create Powershell runspace
- InitialSessionState initial = InitialSessionState.CreateDefault();
- Runspace runspace = RunspaceFactory.CreateRunspace(initial);
- PowerShell ps = PowerShell.Create();
- ps.Runspace = runspace;
- //Runspace runspace = RunspaceFactory.CreateRunspace();
- // open it
- ps.Runspace.Open();
- ps.Runspace.SessionStateProxy.SetVariable("ComputerName", CompnameInput.Text);
- // create a pipeline and feed it the script text
- Pipeline pipeline = runspace.CreatePipeline();
- pipeline.Commands.AddScript(scriptText);
- // add an extra command to transform the script output objects into nicely formatted strings
- // remove this line to get the actual objects that the script returns. For example, the script
- // "Get-Process" returns a collection of System.Diagnostics.Process instances.
- pipeline.Commands.Add("Out-String");
- // execute the script
- Collection<PSObject> results = pipeline.Invoke();
- // close the runspace
- ps.Runspace.Close();
- // convert the script result into a single string
- StringBuilder stringBuilder = new StringBuilder();
- foreach (PSObject obj in results)
- {
- stringBuilder.AppendLine(obj.ToString());
- }
- // return the results of the script that has
- // now been converted to text
- return stringBuilder.ToString();
- }
- // helper method that takes your script path, loads up the script
- // into a variable, and passes the variable to the RunScript method
- // that will then execute the contents
- private string LoadScript(string filename)
- {
- try
- {
- // Create an instance of StreamReader to read from our file.
- // The using statement also closes the StreamReader.
- using (StreamReader sr = new StreamReader(filename))
- {
- // use a string builder to get all our lines from the file
- StringBuilder fileContents = new StringBuilder();
- // string to hold the current line
- string curLine;
- // loop through our file and read each line into our
- // stringbuilder as we go along
- while ((curLine = sr.ReadLine()) != null)
- {
- // read each line and MAKE SURE YOU ADD BACK THE
- // LINEFEED THAT IT THE ReadLine() METHOD STRIPS OFF
- fileContents.Append(curLine + "\n");
- }
- // call RunScript and pass in our file contents
- // converted to a string
- return fileContents.ToString();
- }
- }
- catch (Exception e)
- {
- // Let the user know what went wrong.
- string errorText = "The file could not be read:";
- errorText += e.Message + "\n";
- return errorText;
- }
- }
- private void Exitbutton_Click(object sender, RoutedEventArgs e)
- {
- this.Close();
- }
- private void DeviceStatusButton_Click(object sender, RoutedEventArgs e)
- {
- // run our script and put the result into our textbox
- // NOTE: make sure to change the path to the correct location of your script
- Testlabel_Out.Content = RunScript(LoadScript(@"C:\VSProjects\ClientChecksNew\ClientChecksNew\Script\Get_Devicestatus.ps1"));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement