forked from BornToBeRoot/NETworkManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowerShellHelper.cs
More file actions
37 lines (31 loc) · 1.08 KB
/
Copy pathPowerShellHelper.cs
File metadata and controls
37 lines (31 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System.Diagnostics;
namespace NETworkManager.Utilities;
public static class PowerShellHelper
{
/// <summary>
/// Execute a PowerShell command.
/// </summary>
/// <param name="command">Command to execute.</param>
/// <param name="asAdmin">
/// Start PowerShell as administrator. Error code 1223 is returned when UAC dialog is canceled by
/// user.
/// </param>
/// <param name="windowStyle">Window style of the PowerShell console (Default: Hidden)</param>
public static void ExecuteCommand(string command, bool asAdmin = false,
ProcessWindowStyle windowStyle = ProcessWindowStyle.Hidden)
{
var info = new ProcessStartInfo
{
FileName = "powershell.exe",
Arguments = $"-NoProfile -NoLogo -Command {command}",
UseShellExecute = true,
WindowStyle = windowStyle
};
if (asAdmin)
info.Verb = "runas";
using var process = new Process();
process.StartInfo = info;
process.Start();
process.WaitForExit();
}
}