diff --git a/lib/platform/process.dart b/lib/platform/process.dart index 1199f16..a11a4e1 100644 --- a/lib/platform/process.dart +++ b/lib/platform/process.dart @@ -127,12 +127,20 @@ class ProcessData { } class ProcessExecutor { - static bool _powershellGranted = false; - static Future getPowershellGrant() async { - if (!_powershellGranted) { - _powershellGranted = true; - var result = await Process.run( - 'powershell.exe', + static String _powershellPath = ''; + static Future getPowershell() async { + if (_powershellPath.isEmpty) { + var result = await Process.run('cmd', ['/c', 'where powershell']); + if (result.exitCode == 0) { + var path = result.stdout.toString().split('\n')[0]; + _powershellPath = File(path).absolute.path; + } + if (_powershellPath.isEmpty) { + throw UnsupportedError('Not found powershell'); + } + + result = await Process.run( + _powershellPath, [ '-Command', 'Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned' @@ -144,7 +152,7 @@ class ProcessExecutor { print('[Powershell Grant] Error: ${result.stderr}'); } } - return simpleFuture; + return dataFutrue(_powershellPath); } static Future startExe(String exe, List args, @@ -226,11 +234,9 @@ class ProcessExecutor { file.createSync(); file.writeAsStringSync(shell.toString(), flush: true); - await getPowershellGrant(); - try { - processData.process = await Process.start( - 'powershell.exe', ['-NoProfile', '-File', file.absolute.path]); + processData.process = await Process.start(await getPowershell(), + ['-NoProfile', '-File', file.absolute.path]); } on Exception catch (e) { logHandler ?? (e, LogType.error); } @@ -288,10 +294,10 @@ class ProcessExecutor { print(file.absolute.path); - await getPowershellGrant(); + await getPowershell(); processResult = await Process.run( - 'powershell.exe', // PowerShell 실행 + await getPowershell(), // PowerShell 실행 ['-NoProfile', '-File', file.absolute.path], runInShell: true); diff --git a/lib/utils/os_startup.dart b/lib/utils/os_startup.dart index 65281a2..4ae3ece 100644 --- a/lib/utils/os_startup.dart +++ b/lib/utils/os_startup.dart @@ -324,7 +324,8 @@ Exit @override Future isRegistedStartup(String label) async { var shell = 'Get-ScheduledTask | Where-Object {\$_.TaskName -eq "$label"}'; - var process = await Process.run('powershell.exe', [shell]); + var process = + await Process.run(await ProcessExecutor.getPowershell(), [shell]); var exist = false; if (process.exitCode == 0) { var existLine = []; @@ -387,8 +388,8 @@ Exit var powershellCommand = 'Start-Process powershell.exe -ArgumentList "-NoProfile -ExecutionPolicy Bypass -Command `& \'${tempFile.path}\'; exit" -Verb RunAs -WindowStyle Hidden'; - var process = await Process.run( - 'powershell', ['-Command', powershellCommand, '-Wait']); + var process = await Process.run(await ProcessExecutor.getPowershell(), + ['-Command', powershellCommand, '-Wait']); print(process.stdout.toString()); print(process.stderr.toString()); @@ -403,7 +404,7 @@ Exit Future unregistStartup(String label) async { var shell = 'Unregister-ScheduledTask -TaskName "$label" -Confirm:\$false'; var process = await Process.run( - 'powershell.exe', + await ProcessExecutor.getPowershell(), [ '-Command', 'Start-Process powershell -ArgumentList \'-NoProfile -ExecutionPolicy Bypass -Command "$shell"\' -Verb RunAs -WindowStyle Hidden'