Spawn PowerShell Admin consoles from Windows tray

460 Views Asked by At

I have a need to open various Local Admin PowerShell consoles many times a day. If I keep one console open all day, that's fine, but if that closes, my policy-controlled Admin password has usually expired, so if I have to later open another console, I have to go into the request process to get an Admin password, and re-request that every hour for my work environment, which wastes a lot of time.

I would really like something that sits in the tray and never closes, but which can spawn multiple processes, all with the Local Admin rights that I started it with. This way, I can request a Local Admin password in the morning, then open this system tray process or tool with that password, then I can spawn multiple Admin PowerShell consoles (or maybe other processes that I need, like Disk Management or whatever) from there without having to re-request Admin passwords every hour wasting lots of time and productivity.

Maybe this could be a PowerShell script, or some tool that someone knows that can act as an App Launcher from the system tray / notification area that I can reuse for PowerShell consoles or other apps etc. Maybe even this can be done by creating a PowerShell console that never closes (somehow(!) though I have no idea how to do that, where maybe the 'x' at top right or typing 'exit' in the console will not close it but will instead just minimise that console back into the system tray ready to pop up with admin rights whenever I need it).

I've not found anything to be able to achieve this, but it would be incredibly useful in my work environment (and probably for other DevOps / Sys Admin types that need to waste time on the bureaucratic hurdles that they make us jump through) and would appreciate some pointers in this respect.

1

There are 1 best solutions below

1
On

You can create a system tray icon fairly easily with Forms and you can run it as admin to have the correct rights:

# Load Assemblies
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$MyIcon = [Drawing.Icon]::ExtractAssociatedIcon((Get-Command powershell).Path)

# Create Primary form
$objForm = New-Object System.Windows.Forms.Form
$objForm.Visible = $false
$objForm.WindowState = "minimized"
$objForm.ShowInTaskbar = $false
$objForm.add_Closing({ $objForm.ShowInTaskBar = $False })
#
$objNotifyIcon = New-Object System.Windows.Forms.NotifyIcon
$objNotifyIcon.Icon = $MyIcon
$objNotifyIcon.Text = "TrayUtility"
$objNotifyIcon.Visible = $true
#
$objContextMenu = New-Object System.Windows.Forms.ContextMenu
#
# Build the context menu
# Create Menu Item
$ToggleMenuItem1 = New-Object System.Windows.Forms.MenuItem
$ToggleMenuItem1.Index = 1
$ToggleMenuItem1.Text = "Menu Item 1"
$ToggleMenuItem1.add_Click({
    # Action when selected
})

# Create an Exit Menu Item
$ExitMenuItem = New-Object System.Windows.Forms.MenuItem
$ExitMenuItem.Index = 5
$ExitMenuItem.Text = "E&xit"
$ExitMenuItem.add_Click({
    $objForm.Close()
    $objNotifyIcon.visible = $false
})
# Add the Menu Items to the Context Menu
$objContextMenu.MenuItems.Add($ToggleMenuItem1) | Out-Null
$objContextMenu.MenuItems.Add($ExitMenuItem) | Out-Null
#
# Assign the Context Menu
$objNotifyIcon.ContextMenu = $objContextMenu
$objForm.ContextMenu = $objContextMenu

# Show the Form - Keep it open
$objForm.ShowDialog() | Out-Null
$objForm.Dispose()

Pretty sure I've seen code somewhere that launches a PS admin window then uses show/hide rather than trying to launch a new one each time - you can incorporate controlling that into the icon right click menu Give it a try and post code here on SO if you get stuck.