krutoindie.blogg.se

Unable to run as administrator
Unable to run as administrator





unable to run as administrator

You can create a batch file (*.bat) that runs your powershell script with administrative privileges when double-clicked. $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") Write-Host -NoNewLine "Press any key to continue." # Run your code that needs to be elevated here. # Exit from the current, unelevated, process # Indicate that the process should be elevated $newProcess.Arguments = "& '" + $script:MyInvocation.M圜ommand.Path + "'" # Specify the current script path and name as a parameter with added scope and support for scripts with spaces in it's path # Create a new process object that starts PowerShell # We are not running as an administrator, so relaunch as administrator $Host.UI.RawUI.BackgroundColor = "DarkBlue" $Host.UI.RawUI.WindowTitle = $myInvocation.M圜ommand.Definition + "(Elevated)" # We are running as an administrator, so change the title and background colour to indicate this If ($myWindowsPrincipal.IsInRole($adminRole)) # Check to see if we are currently running as an administrator # Get the security principal for the administrator role $myWindowsPrincipal = New-Object ($myWindowsID) # Get the ID and security principal of the current user account There a few minor issue with his code a modified version based on fixes suggested in the comment is below.īasically it gets the identity associated with the current process, checks whether it is an administrator, and if it isn't, creates a new PowerShell process with administrator privileges and terminates the old process. Benjamin Armstrong posted an excellent article about self-elevating PowerShell scripts.







Unable to run as administrator