Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. class Credentials
  2. {
  3. private static SecureString MakeSecureString(string text)
  4. {
  5. SecureString secure = new SecureString();
  6. foreach (char c in text)
  7. {
  8. secure.AppendChar(c);
  9. }
  10.  
  11. return secure;
  12. }
  13. public static void RunAs(string path, string username, string password)
  14. {
  15. try
  16. {
  17.  
  18. Process process = new Process();
  19. process.StartInfo.FileName = "powershell.exe";
  20. process.StartInfo.UserName = "adminaccount@account.com";
  21. process.StartInfo.Password = MakeSecureString(password);
  22. process.StartInfo.CreateNoWindow = false;
  23. process.StartInfo.RedirectStandardInput = true;
  24. process.StartInfo.RedirectStandardOutput = true;
  25. process.StartInfo.RedirectStandardError = true;
  26. process.StartInfo.UseShellExecute = false;
  27. process.Start();
  28. process.StandardInput.WriteLine(" Some Power Shell Script");
  29. process.StandardInput.Flush();
  30. process.StandardInput.Close();
  31. process.WaitForExit();
  32. Console.WriteLine(process.StandardOutput.ReadToEnd());
  33. Console.WriteLine(process.StandardError.ReadToEnd());
  34. Console.Read();
  35. }
  36. catch (Win32Exception w32E)
  37. {
  38. // The process didn't start.
  39. Console.WriteLine(w32E);
  40. }
  41.  
  42.  
  43. }
  44.  
  45. }
  46.  
  47. private void Button_Click(object sender, EventArgs e)
  48. {
  49. Credentials.SecureString();
  50. Credentials.RunAs();
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement