Running a command as Administrator using PowerShell?

后端 未结 26 2933
粉色の甜心
粉色の甜心 2020-11-22 09:41

You know how if you\'re the administrative user of a system and you can just right click say, a batch script and run it as Administrator without entering the administrator p

相关标签:
26条回答
  • 2020-11-22 09:57

    This is a clarification ...

    The powershell RUNAS / SAVECRED credential "is not safe", tried it and it adds the admin identity and password into the credential cache and can be used elsewhere OOPS!. If you have done this I suggest you check and remove the entry.

    Review your program or code because the Microsoft policy is you cannot have mixed user and admin code in the same code blob without the UAC (the entry point) to execute the program as admin. This would be sudo (same thing) on Linux.

    The UAC has 3 types, dont'see, a prompt or an entry point generated in the manifest of the program. It does not elevate the program so if there is no UAC and it needs admin it will fail. The UAC though as an administrator requirement is good, it prevents code execution without authentication and prevents the mixed codes scenario executing at user level.

    0 讨论(0)
  • 2020-11-22 09:58

    Here is an addition to Shay Levi's suggestion (just add these lines at the beginning of a script):

    if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))  
    {  
      $arguments = "& '" +$myinvocation.mycommand.definition + "'"
      Start-Process powershell -Verb runAs -ArgumentList $arguments
      Break
    }
    

    This results in the current script being passed to a new powershell process in Administrator mode (if current User has access to Administrator mode and the script is not launched as Administrator).

    0 讨论(0)
  • 2020-11-22 09:59

    Another simpler solution is that you may also right click on "C:\Windows\System32\cmd.exe" and choose "Run as Administrator" then you can run any app as administrator without providing any password.

    0 讨论(0)
  • 2020-11-22 10:00

    I haven't seen my own way of doing it before, so, try this out. It is way easier to follow and has a much smaller footprint:

    if([bool]([Security.Principal.WindowsIdentity]::GetCurrent()).Groups -notcontains "S-1-5-32-544") {
        Start Powershell -ArgumentList "& '$MyInvocation.MyCommand.Path'" -Verb runas
        }
    

    Very simply, if the current Powershell session was called with administrator privileges, the Administrator Group well-known SID will show up in the Groups when you grab the current identity. Even if the account is a member of that group, the SID won't show up unless the process was invoked with elevated credentials.

    Nearly all of these answers are a variation on Microsoft's Ben Armstrong's immensely popular method of how to accomplish it while not really grasping what it is actually doing and how else to emulate the same routine.

    0 讨论(0)
  • 2020-11-22 10:01

    If the current console is not elevated and the operation you're trying to do requires elevated privileges then you can start powershell with the Run as Administrator option :

    PS> Start-Process powershell -Verb runAs
    

    https://docs.microsoft.com/powershell/module/Microsoft.PowerShell.Management/Start-Process

    0 讨论(0)
  • 2020-11-22 10:01

    You can also force the application to open as administrator, if you have an administrator account, of course.

    Locate the file, right click > properties > Shortcut > Advanced and check Run as Administrator

    Then Click OK.

    0 讨论(0)
提交回复
热议问题