|
3 | 3 | Untitled |
4 | 4 | #> |
5 | 5 |
|
| 6 | +# Get the ID and security principal of the current user account |
| 7 | +$myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent() |
| 8 | +$myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID) |
| 9 | + |
| 10 | +# Get the security principal for the Administrator role |
| 11 | +$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator |
| 12 | + |
| 13 | +# Check to see if we are currently running "as Administrator" |
| 14 | +if ($myWindowsPrincipal.IsInRole($adminRole)) |
| 15 | + { |
| 16 | + # We are running "as Administrator" - so change the title and background color to indicate this |
| 17 | + $Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)" |
| 18 | + $Host.UI.RawUI.BackgroundColor = "DarkBlue" |
| 19 | + clear-host |
| 20 | + } |
| 21 | +else |
| 22 | + { |
| 23 | + # We are not running "as Administrator" - so relaunch as administrator |
| 24 | + |
| 25 | + # Create a new process object that starts PowerShell |
| 26 | + $newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell"; |
| 27 | + |
| 28 | + # Specify the current script path and name as a parameter |
| 29 | + $newProcess.Arguments = $myInvocation.MyCommand.Definition; |
| 30 | + |
| 31 | + # Indicate that the process should be elevated |
| 32 | + $newProcess.Verb = "runas"; |
| 33 | + |
| 34 | + # Start the new process |
| 35 | + [System.Diagnostics.Process]::Start($newProcess); |
| 36 | + |
| 37 | + # Exit from the current, unelevated, process |
| 38 | + exit |
| 39 | + } |
| 40 | + |
6 | 41 | $DLL = '[DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);' |
7 | 42 | Add-Type -MemberDefinition $DLL -name NativeMethods -namespace Win32 |
8 | 43 | $Process = (Get-Process PowerShell | Where-Object MainWindowTitle -like '*RemoveVMwUI*').MainWindowHandle |
|
0 commit comments