Advertisement
Guest User

Untitled

a guest
Sep 5th, 2016
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. using System.Collections.ObjectModel;
  16. using System.Management.Automation;
  17. using System.Management.Automation.Runspaces;
  18. using System.IO;
  19.  
  20. namespace ClientCheck
  21. {
  22. /// <summary>
  23. /// Interaction logic for MainWindow.xaml
  24. /// </summary>
  25. public partial class MainWindow : Window
  26. {
  27. public MainWindow()
  28. {
  29. InitializeComponent();
  30. }
  31.  
  32. private string RunScript(string scriptText)
  33. {
  34. // create Powershell runspace
  35. InitialSessionState initial = InitialSessionState.CreateDefault();
  36. Runspace runspace = RunspaceFactory.CreateRunspace(initial);
  37. PowerShell ps = PowerShell.Create();
  38. ps.Runspace = runspace;
  39. //Runspace runspace = RunspaceFactory.CreateRunspace();
  40.  
  41. // open it
  42. ps.Runspace.Open();
  43. ps.Runspace.SessionStateProxy.SetVariable("ComputerName", CompnameInput.Text);
  44.  
  45. // create a pipeline and feed it the script text
  46. Pipeline pipeline = runspace.CreatePipeline();
  47. pipeline.Commands.AddScript(scriptText);
  48.  
  49. // add an extra command to transform the script output objects into nicely formatted strings
  50. // remove this line to get the actual objects that the script returns. For example, the script
  51. // "Get-Process" returns a collection of System.Diagnostics.Process instances.
  52. pipeline.Commands.Add("Out-String");
  53.  
  54. // execute the script
  55. Collection<PSObject> results = pipeline.Invoke();
  56.  
  57. // close the runspace
  58. ps.Runspace.Close();
  59.  
  60. // convert the script result into a single string
  61. StringBuilder stringBuilder = new StringBuilder();
  62. foreach (PSObject obj in results)
  63. {
  64. stringBuilder.AppendLine(obj.ToString());
  65. }
  66.  
  67. // return the results of the script that has
  68. // now been converted to text
  69. return stringBuilder.ToString();
  70. }
  71. // helper method that takes your script path, loads up the script
  72. // into a variable, and passes the variable to the RunScript method
  73. // that will then execute the contents
  74. private string LoadScript(string filename)
  75. {
  76. try
  77. {
  78. // Create an instance of StreamReader to read from our file.
  79. // The using statement also closes the StreamReader.
  80. using (StreamReader sr = new StreamReader(filename))
  81. {
  82.  
  83. // use a string builder to get all our lines from the file
  84. StringBuilder fileContents = new StringBuilder();
  85.  
  86. // string to hold the current line
  87. string curLine;
  88.  
  89. // loop through our file and read each line into our
  90. // stringbuilder as we go along
  91. while ((curLine = sr.ReadLine()) != null)
  92. {
  93. // read each line and MAKE SURE YOU ADD BACK THE
  94. // LINEFEED THAT IT THE ReadLine() METHOD STRIPS OFF
  95. fileContents.Append(curLine + "\n");
  96. }
  97.  
  98. // call RunScript and pass in our file contents
  99. // converted to a string
  100. return fileContents.ToString();
  101. }
  102. }
  103. catch (Exception e)
  104. {
  105. // Let the user know what went wrong.
  106. string errorText = "The file could not be read:";
  107. errorText += e.Message + "\n";
  108. return errorText;
  109. }
  110.  
  111. }
  112.  
  113. private void Exitbutton_Click(object sender, RoutedEventArgs e)
  114. {
  115. this.Close();
  116. }
  117.  
  118. private void DeviceStatusButton_Click(object sender, RoutedEventArgs e)
  119. {
  120. // run our script and put the result into our textbox
  121. // NOTE: make sure to change the path to the correct location of your script
  122. Testlabel_Out.Content = RunScript(LoadScript(@"C:\VSProjects\ClientChecksNew\ClientChecksNew\Script\Get_Devicestatus.ps1"));
  123. }
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement