Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Credentials
- {
- private static SecureString MakeSecureString(string text)
- {
- SecureString secure = new SecureString();
- foreach (char c in text)
- {
- secure.AppendChar(c);
- }
- return secure;
- }
- public static void RunAs(string path, string username, string password)
- {
- try
- {
- Process process = new Process();
- process.StartInfo.FileName = "powershell.exe";
- process.StartInfo.UserName = "adminaccount@account.com";
- process.StartInfo.Password = MakeSecureString(password);
- process.StartInfo.CreateNoWindow = false;
- process.StartInfo.RedirectStandardInput = true;
- process.StartInfo.RedirectStandardOutput = true;
- process.StartInfo.RedirectStandardError = true;
- process.StartInfo.UseShellExecute = false;
- process.Start();
- process.StandardInput.WriteLine(" Some Power Shell Script");
- process.StandardInput.Flush();
- process.StandardInput.Close();
- process.WaitForExit();
- Console.WriteLine(process.StandardOutput.ReadToEnd());
- Console.WriteLine(process.StandardError.ReadToEnd());
- Console.Read();
- }
- catch (Win32Exception w32E)
- {
- // The process didn't start.
- Console.WriteLine(w32E);
- }
- }
- }
- private void Button_Click(object sender, EventArgs e)
- {
- Credentials.SecureString();
- Credentials.RunAs();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement