Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [CmdletBinding()]
- param($user)
- # This checks if the shell is an admin shell, otherwise summons the script in an admin shell
- 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, [Security.Principal.WindowsIdentity]::GetCurrent().Name)
- Break
- }
- else
- {
- $user = [Security.Principal.WindowsIdentity]::GetCurrent().Name
- }
- # Running as an admin shell from this point bellow
- $group = [ADSI]"WinNT://$($user.Substring(0,$user.IndexOf('\')))/Administrators,group"
- $user = $user.Replace("\","/")
- # Tries to add the current user to the admin group, if an exception it thrown, it means the user is already admin, so we remove it instead.
- try{
- $group.Add("WinNT://$user,user")
- Write-Host "Added $user to administrators group"
- }
- catch{
- $group.Remove("WinNT://$user,user")
- Write-Host "Removed $user from administrators group"
- }
- sleep 5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement