Skip to content

Instantly share code, notes, and snippets.

@ringe
Created July 28, 2016 10:00
Show Gist options
  • Save ringe/b0422bc9fac11a42101f7623db10d8ae to your computer and use it in GitHub Desktop.
Save ringe/b0422bc9fac11a42101f7623db10d8ae to your computer and use it in GitHub Desktop.
Maximum users of an application, limit license usage on Windows Terminal Server
# name of procsess we are tracking
$limited_process = "notepad.exe"
# limit of instances we allow
$user_limit = 3
# Limit reached message and title
$message = "Too many users! They are:"
$title = "No more notepad"
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$msgbox = [System.Windows.Forms.MessageBox]
$processes = (Get-WmiObject win32_process -Filter "name='$($limited_process)'")
$users = Foreach ($proc in $processes) { $proc.getowner().User }
$names = ($users -join ', ').ToUpper()
if ($users.count -eq $user_limit) {
$msgbox::Show("$($message) $($names)", $title)
} else {
start $limited_process
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment