Skip to content

Instantly share code, notes, and snippets.

@ringe
Last active June 16, 2017 13:37
Show Gist options
  • Save ringe/1ed9a5f1db0eccdb2c77c32b9df39699 to your computer and use it in GitHub Desktop.
Save ringe/1ed9a5f1db0eccdb2c77c32b9df39699 to your computer and use it in GitHub Desktop.
Limit the number of processes
# We only have a limited number of user licenses available for a given application
# So we execute the application through a script informing the user of the limit
# To reduce support requests, we tell the user who to talk to to release a license
# See also https://stackoverflow.com/q/1802127/1062276
# The executable we want to limit
$limited_app = "rg_cli.exe"
# Information about the limit
$app_name = "Visma Global"
$info_msg = "Sjekker antall lisenser"
$limit_msg = "For mange brukere. Snakk med: "
$after_msg = " og spar kr 30.000"
# The users not counted
$eternal_users = "RUNE|eksp|ADMINISTRATOR"
# Maximum number of processes allowed
$max_procs = 6
# Some users are always allowed, start the app
if ($env:UserName | select-string $eternal_users) {
start $limited_app
}
else {
# prepare a message popup
$a = new-object -comobject wscript.shell
# inform the user we are counting licenses, close after one second
$a.popup($info_msg, 1, $app_name, 0x40)
# get a list of all processses of the limited app, and the session ids
$procs = Get-WmiObject -class win32_process -filter "Name='$($limited_app)'"
$ids = $procs | %{$_.SessionId}
# if the number of processes have reached the limit, inform the user
if($ids -and $procs -and $procs.count -ge $max_procs) {
# look up the users currently running the app
$sessions = query session | select-string "tcp#" | %{$_ -replace "^.*#\d+ +"} | select-string ($ids -join " | ")
$people = $sessions | select-string $eternal_users -notMatch | %{$_ -replace " {2,27}"," "} | %{$_.ToString().split(" ")[0]}
$message = $limit_msg + ($people -join ", ").ToUpper() + $after_msg
$a.popup($message, 0, $app_name, 0x10)
}
# else, start the app
else {
start $limited_app
}
}
@ringe
Copy link
Author

ringe commented Jun 14, 2017

This is what we use at one of our customers to limit the number of users executing a specific application on a terminal server.
The customer only have a few licenses, because this app's usage is sporadic and limited.

The motivation for the script was a recurring complaint from users who got a "too many users" message from the app.
It sent them running the corridors to ask who was using the app and if they could close it.

The owner said, rather nag on people than spend excess money on licensing.

The shortcut they use to reach the app looks like this:

Target: c:\tools\PSRun.exe C:\tools\limited_visma_global.ps1

I found the PSRun.exe through https://stackoverflow.com/questions/1802127/how-to-run-a-powershell-script-without-displaying-a-window and downloaded it from http://www.leporelo.eu/blog.aspx?id=run-scheduled-tasks-with-winform-gui-in-powershell

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment