Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to run as Administrator ? #78

Open
abdullah230695 opened this issue Sep 30, 2022 · 1 comment
Open

how to run as Administrator ? #78

abdullah230695 opened this issue Sep 30, 2022 · 1 comment

Comments

@abdullah230695
Copy link

abdullah230695 commented Sep 30, 2022

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 ... 
  ''');
@alextekartik
Copy link
Contributor

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants