tekartik / process_run.dart

Process run helper

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

how to run as Administrator ?

abdullah230695 opened this issue · comments

I tried like this ..

await shell.run('''

          # Display some text
          echo Running Scripts ... 
          
          # For admin rights
          powershell -c "Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process -Force" //it doesn't work
          
          powershell -c ".\\Sophia.ps1" //this is script file
          
          echo Scripts Running Completed ... 
  ''');

Since I did not have much idea (I'm more a linux guy), I tried multiple solutions using "runas".

Let's consider the following script test_admin.ps1

function Test-Administrator
{
    $user = [Security.Principal.WindowsIdentity]::GetCurrent();
    return (New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
Write-Output "IsAdmin: $( Test-Administrator )"
Start-Sleep -s 2

that should print either IsAdmin: False or IsAdmin: False depending on whether you are in a elevated position.

The following (dart code):

// Running in normal mode
await shell.run('powershell -c .\\test_admin.ps1');

should print IsAdmin: False

while the following ugly method (sorry I guess it can be simpler but I don't know powershell enough)

// Running as admin
// dir should point to the full absolute path
await shell.run(
      'powershell -c ${shellArgument("Start-Process PowerShell -Verb RunAs ${shellArgument('-Command ${absolute(join(dir, 'test_admin.ps1'))}')}")}');

should ask for admin rights, spawn a new shell and print IsAdmin: True

Note that the full path is required for the script here.

Good luck in finding a better solution...