Created
June 30, 2024 13:07
-
-
Save V6ser/bb741dc075b744bdbfcc3c6ba27a4c13 to your computer and use it in GitHub Desktop.
Vapour Server Util
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add-Type -AssemblyName System.Windows.Forms | |
Add-Type -AssemblyName System.Drawing | |
$exePath = "path\to\Vapour.Server.Host.exe" # Replace with your actual path | |
# Create and configure the notify icon | |
$notifyIcon = New-Object System.Windows.Forms.NotifyIcon | |
$notifyIcon.Text = "Vapour Server Host" | |
$notifyIcon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($exePath) | |
$notifyIcon.Visible = $true | |
# Create a context menu | |
$contextMenu = New-Object System.Windows.Forms.ContextMenuStrip | |
$toggleItem = $contextMenu.Items.Add("Show/Hide Console") | |
$restartItem = $contextMenu.Items.Add("Restart Server") | |
$exitItem = $contextMenu.Items.Add("Exit") | |
$notifyIcon.ContextMenuStrip = $contextMenu | |
# Function to start the process hidden | |
function Start-HiddenProcess { | |
$pinfo = New-Object System.Diagnostics.ProcessStartInfo | |
$pinfo.FileName = $exePath | |
$pinfo.CreateNoWindow = $true | |
$pinfo.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Hidden | |
$pinfo.UseShellExecute = $false | |
$process = [System.Diagnostics.Process]::Start($pinfo) | |
return $process | |
} | |
# Start the process hidden | |
$process = Start-HiddenProcess | |
# Event handlers | |
$toggleItem.Add_Click({ | |
if ($process -and !$process.HasExited) { | |
$hwnd = $process.MainWindowHandle | |
if ($hwnd -eq [IntPtr]::Zero) { | |
# Window is hidden, so show it | |
[Win32]::ShowWindow($hwnd, 5) # SW_SHOW | |
} else { | |
# Window is visible, so hide it | |
[Win32]::ShowWindow($hwnd, 0) # SW_HIDE | |
} | |
} | |
}) | |
$restartItem.Add_Click({ | |
if ($process -and !$process.HasExited) { | |
$process.Kill() | |
} | |
$process = Start-HiddenProcess | |
}) | |
$exitItem.Add_Click({ | |
if ($process -and !$process.HasExited) { | |
$process.Kill() | |
} | |
$notifyIcon.Visible = $false | |
[System.Windows.Forms.Application]::Exit() | |
}) | |
# Add Win32 API functions | |
Add-Type @" | |
using System; | |
using System.Runtime.InteropServices; | |
public class Win32 { | |
[DllImport("user32.dll")] | |
[return: MarshalAs(UnmanagedType.Bool)] | |
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); | |
} | |
"@ | |
# Message loop to keep the script running | |
[System.Windows.Forms.Application]::Run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment