简体   繁体   English

从 Windows 托盘生成 PowerShell 管理控制台

[英]Spawn PowerShell Admin consoles from Windows tray

I have a need to open various Local Admin PowerShell consoles many times a day.我需要每天多次打开各种本地管理员 PowerShell 控制台。 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.如果我让一个控制台整天打开,那很好,但如果关闭,我的策略控制的管理员密码通常已经过期,所以如果我以后必须打开另一个控制台,我必须 go 进入请求过程以获取管理员密码,并为我的工作环境每隔一小时重新请求一次,这会浪费很多时间。

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).也许这可能是 PowerShell 脚本,或者有人知道可以从系统托盘/通知区域充当应用程序启动器的一些工具,我可以将其用于 PowerShell 控制台或其他应用程序等。甚至可以通过创建 Z3D265B4E003EEEF0DCCZ8B1818 来完成永远不会关闭的控制台(不知何故(,)虽然我不知道该怎么做。也许右上角的“x”或在控制台中键入“退出”不会关闭它,而是只会将该控制台最小化回系统托盘准备好在我需要时弹出管理员权限)。

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.我还没有发现任何东西可以实现这一点,但它在我的工作环境中将非常有用(可能对于其他需要浪费时间在他们让我们跳过的官僚障碍上的 DevOps / Sys Admin 类型)和在这方面将不胜感激。

You can create a system tray icon fairly easily with Forms and you can run it as admin to have the correct rights:您可以使用 Forms 相当轻松地创建系统托盘图标,您可以以管理员身份运行它以获得正确的权限:

# 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.很确定我在某处看到过启动 PS 管理员 window 的代码,然后使用显示/隐藏而不是每次都尝试启动一个新的 - 您可以将控制合并到图标右键菜单中试一试并在此处发布代码所以,如果你被卡住了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM