Guest User

Untitled

a guest
Jul 24th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. var options = new ConnectionOptions();
  2. servicePath = "\\Testserver\root\cimv2";
  3. options.EnablePrivileges = true;
  4. options.Username = username;
  5. options.Password = pwd;
  6. serviceScope = new ManagementScope(servicePath, options);
  7. serviceScope.Connect();
  8.  
  9. Process process = new Process();
  10. process.StartInfo.FileName = "diskpart.exe";
  11. process.StartInfo.UseShellExecute = false;
  12. process.StartInfo.CreateNoWindow = true;
  13. process.StartInfo.RedirectStandardInput = true;
  14. process.StartInfo.RedirectStandardOutput = true;
  15. process.Start();
  16. process.StandardInput.WriteLine("list volume");
  17. process.StandardInput.WriteLine("exit");
  18. string output = process.StandardOutput.ReadToEnd();
  19. process.WaitForExit();
  20.  
  21. // extract information from output
  22. string table = output.Split(new string[] { "DISKPART>" }, StringSplitOptions.None)[1];
  23. var rows = table.Split(new string[] { "n" }, StringSplitOptions.None);
  24. for (int i = 3; i < rows.Length; i++)
  25. {
  26. if (rows[i].Contains("Volume"))
  27. {
  28. int index = Int32.Parse(rows[i].Split(new string[] { " " }, StringSplitOptions.None)[3]);
  29. string label = rows[i].Split(new string[] { " " }, StringSplitOptions.None)[8];
  30. Console.WriteLine($@"Volume {index} {label}:");
  31. }
  32. }
  33.  
  34. object[] theProcessToRun = { "diskpart" };
  35. using (var managementClass = new ManagementClass(serviceScope, new ManagementPath("Win32_Process"), new ObjectGetOptions()))
  36. {
  37. managementClass.InvokeMethod("Create", theProcessToRun);
  38. }
Add Comment
Please, Sign In to add comment