none
Elevated instance RRS feed

  • Question

  • Hello,

    Im using below code in scripts so it asks for elevation itself.

    $bAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
    If (!$bAdmin){
      $argumentsList = @(
        '-File',
        $myinvocation.mycommand.definition,
        $SID
            
      )
      Write-Host $argumentsList
      Start-Process powershell -Verb RunAs -ArgumentList $argumentsList
      exit #must exit first script 
    }

    That all works fine, until there is something wrong in the script (typo in command) then the elevated window exits, and thus the error message is gone. 

    Is there a way to prevent that?

    thanks


    --------------------- Leos

    Friday, January 12, 2018 10:17 AM

Answers

  • $argumentsList = @(
        '-noexit',
    '-File', $myinvocation.mycommand.definition )
    I remind to always read the help carefully until it is clear to you.  Don't just skim it while you are brushing your teeth or arguing with your girlfriend.

    \_(ツ)_/


    • Edited by jrvModerator Monday, January 15, 2018 4:06 AM
    • Marked as answer by Leoš Marek Wednesday, January 17, 2018 1:26 PM
    Monday, January 15, 2018 4:03 AM
    Moderator

All replies

  • Debug the script by running from an elevated prompt.  Implement error management.

    You can try adding "-noexit" to the arguments.


    \_(ツ)_/


    Friday, January 12, 2018 11:44 AM
    Moderator
  • In other words its not possible that the elevation window will stay open on error? .)

    --------------------- Leos

    Friday, January 12, 2018 11:46 AM
  • You can try adding "-noexit" to the arguments.

    \_(ツ)_/

    Friday, January 12, 2018 11:49 AM
    Moderator
  • doesnt work, exits anyway

    $bAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
    If (!$bAdmin){
      $argumentsList = @(
        '-File',
        '-noexit',
        $myinvocation.mycommand.definition
        
            
      )
      Write-Host $argumentsList
      Start-Process powershell -Verb RunAs -ArgumentList $argumentsList
      exit #must exit first script 
    }
    
    ads


    --------------------- Leos

    Friday, January 12, 2018 11:52 AM
  • Hi Leos,

    Based on the tests in my lab, the same behavior is re-produced. I suppose this issue is probably caused by the position of -NoExit is in the middle of -File and $myinvocation.mycommand.definition, please have a try to place it in the first position and see if it works.

    If you need further help, please feel free to let us know.

    Best Regards,
    Albert

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Monday, January 15, 2018 2:47 AM
  • Good call.  "File" must be the last argument on the command line.

    -File
        Runs the specified script in the local scope ("dot-sourced"), so that the
        functions and variables that the script creates are available in the
        current session. Enter the script file path and any parameters.
        File must be the last parameter in the command, because all characters
        typed after the File parameter name are interpreted
        as the script file path followed by the script parameters.
    

    Once again - referring to "help" has the answer.


    \_(ツ)_/

    Monday, January 15, 2018 2:51 AM
    Moderator
  • $argumentsList = @(
        '-noexit',
    '-File', $myinvocation.mycommand.definition )
    I remind to always read the help carefully until it is clear to you.  Don't just skim it while you are brushing your teeth or arguing with your girlfriend.

    \_(ツ)_/


    • Edited by jrvModerator Monday, January 15, 2018 4:06 AM
    • Marked as answer by Leoš Marek Wednesday, January 17, 2018 1:26 PM
    Monday, January 15, 2018 4:03 AM
    Moderator
  • $bAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
    If (!$bAdmin){
        $argumentsList = @(
          '-noExit',    
          '-File',
            $myinvocation.mycommand.definition
           #params here IF ANY
           
        )
        Write-Host $argumentsList
        Start-Process powershell -Verb RunAs -ArgumentList $argumentsList
        exit #must exit first script 
    }
    pause
    This doesnt work, both non-elevated and elevated window close, the pause is ignored totally.

    --------------------- Leos

    Tuesday, January 16, 2018 6:08 PM
  • Works fine for me.  Are you really an administrator?


    \_(ツ)_/

    Tuesday, January 16, 2018 7:03 PM
    Moderator
  • Really?:) Yes I am. Same behavior on Win 10 and 2012r2 here

    --------------------- Leos

    Tuesday, January 16, 2018 7:28 PM
  • Hi Leos,

    According to the current situation, I recommend using return if do not want to close the non-elevated console. Also, please have a try for testing using an actual PS1 file in the argument  and see if it works, the following demo for your reference:
    $bAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
    If (!$bAdmin){
        $argumentsList = @(
          '-NoExit',    
          '-File',
          'D:\test.ps1'
        )
        Start-Process powershell -Verb RunAs -ArgumentList $argumentsList
        return
    }
    

    If you need further help, please feel free to let us know.

    Best Regards,
    Albert

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Wednesday, January 17, 2018 9:32 AM
  • Sorry guys,

    it was my mistake. It works fine as per answer marked.

    Thanks a lot


    --------------------- Leos

    Wednesday, January 17, 2018 1:26 PM