update powershell calll

This commit is contained in:
leedongmyung 2025-01-05 11:55:32 +09:00
parent 8ffb07a58c
commit 26111e223e
2 changed files with 18 additions and 23 deletions

View file

@ -127,20 +127,10 @@ class ProcessData {
}
class ProcessExecutor {
static String _powershellPath = '';
static Future<String> 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;
print(_powershellPath);
}
if (_powershellPath.isEmpty) {
throw UnsupportedError('Not found powershell');
}
result = await Process.run(
static bool _initedPowershell = false;
static Future initPowershell() async {
if (!_initedPowershell) {
var result = await Process.run(
'cmd',
[
'/C',
@ -151,11 +141,13 @@ class ProcessExecutor {
runInShell: true,
);
_initedPowershell = true;
if (result.stderr.toString().trim().isNotEmpty) {
print('[Powershell Grant] Error: ${result.stderr}');
}
}
return dataFutrue(_powershellPath);
return simpleFuture;
}
static Future<ProcessData> startExe(String exe, List<String> args,
@ -237,8 +229,7 @@ class ProcessExecutor {
file.createSync();
file.writeAsStringSync(shell.toString(), flush: true);
var powershell = await getPowershell();
print(powershell);
await initPowershell();
try {
processData.process = await Process.start('cmd',
['/C', 'powershell', '-NoProfile', '-File', file.absolute.path]);
@ -299,7 +290,7 @@ class ProcessExecutor {
print(file.absolute.path);
var powshell = await getPowershell();
await initPowershell();
processResult = await Process.run(
'cmd', // PowerShell

View file

@ -323,9 +323,9 @@ Exit
@override
Future<bool> isRegistedStartup(String label) async {
await ProcessExecutor.initPowershell();
var shell = 'Get-ScheduledTask | Where-Object {\$_.TaskName -eq "$label"}';
var process =
await Process.run(await ProcessExecutor.getPowershell(), [shell]);
var process = await Process.run('cmd', ['/C', 'powershell', shell]);
var exist = false;
if (process.exitCode == 0) {
var existLine = <String>[];
@ -388,8 +388,9 @@ Exit
var powershellCommand =
'Start-Process powershell.exe -ArgumentList "-NoProfile -ExecutionPolicy Bypass -Command `& \'${tempFile.path}\'; exit" -Verb RunAs -WindowStyle Hidden';
var process = await Process.run(await ProcessExecutor.getPowershell(),
['-Command', powershellCommand, '-Wait']);
await ProcessExecutor.initPowershell();
var process = await Process.run(
'cmd', ['/C', 'powershell', '-Command', powershellCommand, '-Wait']);
print(process.stdout.toString());
print(process.stderr.toString());
@ -402,10 +403,13 @@ Exit
@override
Future<bool> unregistStartup(String label) async {
await ProcessExecutor.initPowershell();
var shell = 'Unregister-ScheduledTask -TaskName "$label" -Confirm:\$false';
var process = await Process.run(
await ProcessExecutor.getPowershell(),
'cmd',
[
'/C',
'powershell',
'-Command',
'Start-Process powershell -ArgumentList \'-NoProfile -ExecutionPolicy Bypass -Command "$shell"\' -Verb RunAs -WindowStyle Hidden'
],