Skip to content

Instantly share code, notes, and snippets.

@Horihito
Last active Jun 16, 2017
Embed
What would you like to do?
Tray Icon
#######################
# Required Assembleis #
#######################
Add-Type -AssemblyName System.Windows.Forms
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Input")
[void] [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.VisualBasic")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Input")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Input.Keyboard")
###########################
# End Required Assemblies #
###########################
#############
# Variables #
#############
$timeout = 2073600000 # 24 Days is max for tray icon timeout
####################
# End of Variables #
####################
#############
# Functions #
#############
function Refresh {
# Refresh's the Tray icon to add/remove admin functions
$form1.close()
$blnNotifyIcon.Visible = $False
# option 1
#$Script:ShiftKeyDown = [System.Windows.Input.Keyboard]::IsKeyDown("Shift")
#$Script:ControlKeyDown = [System.Windows.Input.Keyboard]::IsKeyDown("Control")
# option 2
#$Script:Modifiers = [System.Windows.Input.Keyboard]::Modifiers
Start-Sleep -milliseconds 100
CreateIcon
}
function CreateIcon{
#####################
# ContextMenu Items #
#####################
$ComputerinfoMenuItem = New-Object System.Windows.Forms.MenuItem
$ComputerinfoMenuItem.Index = 1
$ComputerinfoMenuItem.Text = "My Computer Info"
$ComputerinfoMenuItem.add_Click({SDDC-info})
$HelpdeskMenuItem = New-Object System.Windows.Forms.MenuItem
$HelpdeskMenuItem.Index = 2
$HelpdeskMenuItem.Text = "Helpdesk Ticket Options"
$DD2875MenuItem = New-Object System.Windows.Forms.MenuItem
$DD2875MenuItem.Index = 3
$DD2875MenuItem.Text = "Submit Account Paperwork (Email)"
$DD2875MenuItem.add_Click({Submit-Account-Paperwork})
$AltMenuItems = New-Object System.Windows.Forms.MenuItem
$AltMenuItems.Index = 4
$AltMenuItems.Text = "Alternate Account Options"
$RefreshMenuItems = New-Object System.Windows.Forms.MenuItem
$RefreshMenuItems.Index = 5
$RefreshMenuItems.Text = "Refresh Menu"
$RefreshMenuItems.add_Click({Refresh})
$ExitMenuItem = New-Object System.Windows.Forms.MenuItem
$ExitMenuItem.Index = 6
$ExitMenuItem.Text = "Exit"
$ExitMenuItem.add_Click({$form1.close(); $blnNotifyIcon.Visible = $False})
#########################
# End ContextMenu Items #
#########################
####################
# Admin Menu Items #
####################
$AltAdminMenuItem1 = New-Object System.Windows.Forms.MenuItem
$AltAdminMenuItem1.Index = 1
$AltAdminMenuItem1.Text = "Open ADMIN Powershell as Alt User (User/Pass)"
$AltAdminMenuItem1.add_Click({AltAdminUsername})
$AltAdminMenuItem2 = New-Object System.Windows.Forms.MenuItem
$AltAdminMenuItem2.Index = 2
$AltAdminMenuItem2.Text = "Open ADMIN Powershell as Alt User (CAC)"
$AltAdminMenuItem2.add_Click({AltAdminCAC})
$AltUserMenuItem1 = New-Object System.Windows.Forms.MenuItem
$AltUserMenuItem1.Index = 3
$AltUserMenuItem1.Text = "Open Powershell as Alt User"
$AltUserMenuItem1.add_Click({AltUsername})
########################
# End Admin Menu Items #
########################
#######################################
# Build Context Menu options for Icon #
#######################################
$objContextMenu = New-Object System.Windows.Forms.ContextMenu
$objContextMenu.MenuItems.Add($ComputerinfoMenuItem) | Out-Null
# Only show helpdesk submenu options if not a service account (Email enabled accounts only)
If ($env:USERNAME -notlike 'svc.*'){ # Only show if not a service account
$objContextMenu.MenuItems.Add($HelpdeskMenuItem) | Out-Null
# Sub Menu Items
$HelpdeskMenuItem.MenuItems.Add($HelpdeskMenuItem1) | Out-Null
$HelpdeskMenuItem.MenuItems.Add($HelpdeskMenuItem2) | Out-Null
$HelpdeskMenuItem.MenuItems.Add($HelpdeskMenuItem3) | Out-Null
$HelpdeskMenuItem.MenuItems.Add($HelpdeskMenuItem4) | Out-Null
}
<#
# checking keyboard states... NOT WORKING
#$Script:ShiftKeyDown = [System.Windows.Input.Keyboard]::IsKeyDown("Shift")
#$Script:ControlKeyDown = [System.Windows.Input.Keyboard]::IsKeyDown("Control")
If (($Script:ShiftKeyDown -eq $true) -and ($Script:ControlKeyDown -eq $true)){
#If ($Script:Modifiers -like "*Control, Shift*"){
$objContextMenu.MenuItems.Add($AltMenuItems) | Out-Null
$AltMenuItems.MenuItems.Add($AltAdminMenuItem1) | Out-Null
$AltMenuItems.MenuItems.Add($AltAdminMenuItem2) | Out-Null
$AltMenuItems.MenuItems.Add($AltUserMenuItem1) | Out-Null
}
$objContextMenu.MenuItems.Add($RefreshMenuItems) | Out-Null
#>
########################################
# End of Context Menu options for Icon #
########################################
#######################################################
# Create Notification Icon in System Tray - Icon #
#######################################################
$blnNotifyIcon = New-Object System.Windows.Forms.NotifyIcon
$blnNotifyIcon.Icon = "\\pathtoicon\icon.ico"
$blnNotifyIcon.BalloonTipTitle = "Information"
$blnNotifyIcon.BalloonTipText = " "
$blnNotifyIcon.Text = "Computer Information`nRight Click"
$blnNotifyIcon.ShowBalloonTip($timeout)
$blnNotifyIcon.ContextMenu = $objContextMenu # Enables right click context menu
########################################################
# End of Notification Icon in System Tray - Icon #
########################################################
#####################################################
# Create Form and set visibility and state of items #
#####################################################
$form1 = New-Object System.Windows.Forms.Form
$form1.Icon = "pathtoicon\icon.ico"
$form1.Visible = $false
$form1.text = "Tray Icon - DO NOT CLOSE"
$form1.MinimizeBox = $False
$form1.MaximizeBox = $False
$form1.StartPosition = "Manual"
$form1.WindowState = "Minimized"
$form1.Size = New-Object System.Drawing.Size(300,300)
$form1.Location = New-Object System.Drawing.Size(65535,65535) #moved offscreen so users dont see it
$form1.ShowInTaskbar = $false
$blnNotifyIcon.Visible = $true
$form1.ShowDialog() | Out-Null # MUST BE LAST
############################################################
# End of Create Form and set visibility and state of items #
############################################################
}
########################
# Create the Tray Icon #
########################
CreateIcon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment