I’m basically new to PowerShell, and I wanted a script to write a script to do something which happens to use a command which requires administrator privilege.
With some research I found a rather old script here which allows a script to automatically elevate itself to admin mode (with user permission).
The script on its own does seem to work, but I tried fiddling around with the given script and found some weird results.
Here’s the script I have now (modified from the one above):
# Get the ID and security principal of the current user account $myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent() $myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID) # Get the security principal for the Administrator role $adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator # Check to see if we are currently running "as Administrator" if ($myWindowsPrincipal.IsInRole($adminRole)) { # We are running "as Administrator" - so change the title and background color to indicate this $Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)" $Host.UI.RawUI.BackgroundColor = "DarkBlue" clear-host } else { # We are not running "as Administrator" - so relaunch as administrator # Create a new process object that starts PowerShell $newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell"; # Specify the current script path and name as a parameter $newProcess.Arguments = $myInvocation.MyCommand.Definition; ############################################ echo "Yoo" #echo $newProcess #$newProcess.UseShellExecute = $FALSE ############################################ # Indicate that the process should be elevated $newProcess.Verb = "runas"; # Start the new process [System.Diagnostics.Process]::Start($newProcess); # Exit from the current, unelevated, process exit } # Run your code that needs to be elevated here $userDirectory = (Get-Item -Path "." -Verbose).FullName echo $userDirectory echo $newProcess.Arguments echo "heyy ;)" Read-Host "Press enter to finish"
The important lines are bound by #’s. If I un-comment the first line echo $newProcess
, then the script fails with the following error:
Exception calling "Start" with "1" argument(s): "The Process object must have the UseShellExecute property set to false in order to use environment variables." At C:UsersDanielDocumentsaddSymLinkToXAMPP.ps1:35 char:4 + [System.Diagnostics.Process]::Start($newProcess); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : InvalidOperationException
It strikes me that simply adding an echo
statement would cause anything to break, as I see it as equivalent to a print statement, which is something I frequently use for debugging in other languages. What’s going on here?
Of course, though, if I listen to the error then I arrive at the next line: $newProcess.UseShellExecute = $FALSE
. Unfortunately, if I uncomment this line (regardless of whether the previous line is uncommented), then the script gets stuck in an infinite loop and just keeps running any uncommented echo statements over-and-over. There are no loop statements, so I honestly have no idea why this happens.
Also, apparently, if you let this go on long enough it causes a memory leak. Still not sure the cause…
Any help would be very appreciated. For the record, I am a somewhat experienced programmer (C++, Java, JS), I’m just new to PowerShell.
submitted by /u/Auride
[link] [comments]
The post Issues with Privilege Elevation (Administrator) Script appeared first on How to Code .NET.