Running a command as Administrator using PowerShell?

后端 未结 26 3229
粉色の甜心
粉色の甜心 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 10:20

    The code posted by Jonathan and Shay Levy did not work for me.

    Please find the working code below:

    If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
    {   
    #"No Administrative rights, it will display a popup window asking user for Admin rights"
    
    $arguments = "& '" + $myinvocation.mycommand.definition + "'"
    Start-Process "$psHome\powershell.exe" -Verb runAs -ArgumentList $arguments
    
    break
    }
    #"After user clicked Yes on the popup, your file will be reopened with Admin rights"
    #"Put your code here"
    

提交回复
热议问题