@@ -398,9 +398,9 @@ public static string ExtractStringFromDLL(string file, int number)
398398 }
399399 }
400400
401- public static async Task < bool > RunPowershellCommandAsync ( string command , bool runAsAdmin )
401+ public static async Task < bool > RunPowershellCommandAsync ( string command , PowerShellExecutionOptions options )
402402 {
403- using Process process = CreatePowershellProcess ( command , runAsAdmin ) ;
403+ using Process process = CreatePowershellProcess ( command , options ) ;
404404 using var cts = new CancellationTokenSource ( TimeSpan . FromMilliseconds ( 30 * 1000 ) ) ;
405405
406406 try
@@ -425,11 +425,11 @@ public static async Task<bool> RunPowershellCommandAsync(string command, bool ru
425425 }
426426 }
427427
428- public static bool RunPowershellCommand ( string command , bool runAsAdmin )
428+ public static bool RunPowershellCommand ( string command , PowerShellExecutionOptions options )
429429 {
430430 try
431431 {
432- using Process process = CreatePowershellProcess ( command , runAsAdmin ) ;
432+ using Process process = CreatePowershellProcess ( command , options ) ;
433433
434434 process . Start ( ) ;
435435
@@ -544,24 +544,24 @@ public static Task OpenFormatDriveDialog(string drive)
544544 {
545545 // Format requires elevation
546546 int driveIndex = drive . ToUpperInvariant ( ) [ 0 ] - 'A' ;
547- return RunPowershellCommandAsync ( $ "-command \" $Signature = '[DllImport(\\ \" shell32.dll\\ \" , SetLastError = false)]public static extern uint SHFormatDrive(IntPtr hwnd, uint drive, uint fmtID, uint options);'; $SHFormatDrive = Add-Type -MemberDefinition $Signature -Name \" Win32SHFormatDrive\" -Namespace Win32Functions -PassThru; $SHFormatDrive::SHFormatDrive(0, { driveIndex } , 0xFFFF, 0x0001)\" ", true ) ;
547+ return RunPowershellCommandAsync ( $ "-command \" $Signature = '[DllImport(\\ \" shell32.dll\\ \" , SetLastError = false)]public static extern uint SHFormatDrive(IntPtr hwnd, uint drive, uint fmtID, uint options);'; $SHFormatDrive = Add-Type -MemberDefinition $Signature -Name \" Win32SHFormatDrive\" -Namespace Win32Functions -PassThru; $SHFormatDrive::SHFormatDrive(0, { driveIndex } , 0xFFFF, 0x0001)\" ", PowerShellExecutionOptions . Elevated | PowerShellExecutionOptions . Hidden ) ;
548548 }
549549
550550 public static void SetVolumeLabel ( string drivePath , string newLabel )
551551 {
552552 // Rename requires elevation
553- RunPowershellCommand ( $ "-command \" $Signature = '[DllImport(\\ \" kernel32.dll\\ \" , SetLastError = false)]public static extern bool SetVolumeLabel(string lpRootPathName, string lpVolumeName);'; $SetVolumeLabel = Add-Type -MemberDefinition $Signature -Name \" Win32SetVolumeLabel\" -Namespace Win32Functions -PassThru; $SetVolumeLabel::SetVolumeLabel('{ drivePath } ', '{ newLabel } ')\" ", true ) ;
553+ RunPowershellCommand ( $ "-command \" $Signature = '[DllImport(\\ \" kernel32.dll\\ \" , SetLastError = false)]public static extern bool SetVolumeLabel(string lpRootPathName, string lpVolumeName);'; $SetVolumeLabel = Add-Type -MemberDefinition $Signature -Name \" Win32SetVolumeLabel\" -Namespace Win32Functions -PassThru; $SetVolumeLabel::SetVolumeLabel('{ drivePath } ', '{ newLabel } ')\" ", PowerShellExecutionOptions . Elevated | PowerShellExecutionOptions . Hidden ) ;
554554 }
555555
556556 public static void SetNetworkDriveLabel ( string driveName , string newLabel )
557557 {
558- RunPowershellCommand ( $ "-command \" (New-Object -ComObject Shell.Application).NameSpace('{ driveName } ').Self.Name='{ newLabel } '\" ", false ) ;
558+ RunPowershellCommand ( $ "-command \" (New-Object -ComObject Shell.Application).NameSpace('{ driveName } ').Self.Name='{ newLabel } '\" ", PowerShellExecutionOptions . Hidden ) ;
559559 }
560560
561561 public static Task < bool > MountVhdDisk ( string vhdPath )
562562 {
563563 // Mounting requires elevation
564- return RunPowershellCommandAsync ( $ "-command \" Mount-DiskImage -ImagePath '{ vhdPath } '\" ", true ) ;
564+ return RunPowershellCommandAsync ( $ "-command \" Mount-DiskImage -ImagePath '{ vhdPath } '\" ", PowerShellExecutionOptions . Elevated | PowerShellExecutionOptions . Hidden ) ;
565565 }
566566
567567 public static Bitmap ? GetBitmapFromHBitmap ( HBITMAP hBitmap )
@@ -850,29 +850,32 @@ public static async Task InstallFontsAsync(string[] fontFilePaths, bool forAllUs
850850 if ( psCommand . Length + appendCommand . Length > 32766 )
851851 {
852852 // The command is too long to run at once, so run the command once up to this point.
853- await RunPowershellCommandAsync ( psCommand . Append ( "\" " ) . ToString ( ) , true ) ;
853+ await RunPowershellCommandAsync ( psCommand . Append ( "\" " ) . ToString ( ) , PowerShellExecutionOptions . Elevated | PowerShellExecutionOptions . Hidden ) ;
854854 psCommand . Clear ( ) . Append ( "-command \" " ) ;
855855 }
856856
857857 psCommand . Append ( appendCommand ) ;
858858 }
859859
860- await RunPowershellCommandAsync ( psCommand . Append ( "\" " ) . ToString ( ) , true ) ;
860+ await RunPowershellCommandAsync ( psCommand . Append ( "\" " ) . ToString ( ) , PowerShellExecutionOptions . Elevated | PowerShellExecutionOptions . Hidden ) ;
861861 }
862862
863- private static Process CreatePowershellProcess ( string command , bool runAsAdmin )
863+ private static Process CreatePowershellProcess ( string command , PowerShellExecutionOptions options )
864864 {
865865 Process process = new ( ) ;
866866
867- if ( runAsAdmin )
867+ process . StartInfo . FileName = "powershell.exe" ;
868+ if ( options . HasFlag ( PowerShellExecutionOptions . Elevated ) )
868869 {
869870 process . StartInfo . UseShellExecute = true ;
870871 process . StartInfo . Verb = "runas" ;
871872 }
872873
873- process . StartInfo . FileName = "powershell.exe" ;
874- process . StartInfo . CreateNoWindow = true ;
875- process . StartInfo . WindowStyle = ProcessWindowStyle . Hidden ;
874+ if ( options . HasFlag ( PowerShellExecutionOptions . Hidden ) )
875+ {
876+ process . StartInfo . CreateNoWindow = true ;
877+ process . StartInfo . WindowStyle = ProcessWindowStyle . Hidden ;
878+ }
876879 process . StartInfo . Arguments = command ;
877880
878881 return process ;
0 commit comments