Grant temporary access to printers using PaperCut
How do I grant temporary access to company printers?
One of our resellers recently came to us with an interesting problem. They wanted to provide temporary access to ID cards for users that had forgotten or lost their own, and automatically remove the card number from PaperCut on a date the admins can pick when issuing the cards. This enables staff to carry on using the office’s printing facilities until they were able to find their existing card, or until they had replaced it with a new one.
This is how we helped the reseller, and hopefully, you’ll find it useful too!
Where did we start?
Let’s start by breaking it down into digestible chunks, so that we can see what the task entails. There were three parts in this case.
- Issuing temporary cards
- Automatically removing the card number from an account
- Allowing the admin or card issuer to set an expiration date
Issuing temporary cards
The first part is fairly simple to handle. PaperCut can be set to self-associate new cards with the secondary ID field, or as the cards are temporary the admin can put this value into the field manually.
Automatically removing card numbers
Removing the card number automatically from an account and setting an expiration date is the next task that we need to tackle. You can use the scheduling abilities of your OS API or a server command to do this. In our example we’ve used Powershell, this would look something like the example below which removes the secondary ID for all users.
# Set the path to the server command binary $serverCommand = 'C:\Program Files\PaperCut MF\server\bin\win\server-command.exe' # Get a list of all the users $users = &$serverCommand list-user-accounts # Loop over every user foreach ($user in $users) { # Set the secondary-card-number user property to nothing &$serverCommand set-user-property $user "secondary-card-number" "`" `"" }
Set an expiration date
This now leaves us with the last part. How do we add an expiration date to this? We need somewhere where an admin can input a date and if needed, change it. To do this we settled on using the notes field under the users and a format of dd/mm/yyyy so if they wanted the card to expire on 30th November 2018 they would input 30/11/2018.
Now, all we need to do is update the script to look at the notes field and compare it to the date the script is running and if it is that day or before remove the ID number.
Powershell
CLS # Location of Server Command exe $serverCommand = 'C:\Program Files\PaperCut MF\server\bin\win\server-command.exe' $todaysDate = Get-Date Write-Host "Starting Cleanup" # Get user list $users = &$serverCommand list-user-accounts # Loop users foreach ($user in $users) { # Get the note field $noteField = &$serverCommand get-user-property $user "notes" # If note field contents matches a date pattern (dd/mm/yyyy) if ($noteField -match '^([0-2][0-9]|(3)[0-1])(\/)(((0)[0-9])|((1)[0-2]))(\/)\d{4}$') { # Convert Note field to date object $noteFieldAsDate = Get-Date -Date $notefield # Check if the date is in the past if ($noteFieldAsDate -lt $todaysDate) { Write-Host "The card for '" $user "' has expired" # Remove the card number &$serverCommand set-user-property $user "secondary-card-number" "`" `"" # Clear the date from the notes field &$serverCommand set-user-property $user "notes" "`" `"" } } } Write-Host "Cleanup Completed"
Bash
#!/bin/bash # Location of Server Command binary serverCommand=/home/papercut/server/bin/linux-x64/server-command todaysDate=$(date +%s) echo "Starting Cleanup" # Get user list and loop over them "${serverCommand}" list-user-accounts | while read user ; do # Get the note field noteField=$("${serverCommand}" get-user-property $user "notes") # If note field contents match a date pattern (dd/mm/yyyy) if [[ $noteField =~ ^([0-2][0-9]|(3)[0-1])(\/)(((0)[0-9])|((1)[0-2]))(\/)[0-9]{4}$ ]] ; then # Convert Note field to unix timestamp, We do something ugly with the string to put it into y/m/d... sorry! noteFieldAsDate=$(date +'%s' -d "${noteField: -4}/${noteField: 3: 2}/${noteField: 0: 2}") # Check if the date is in the past if [[ $noteFieldAsDate -lt $todaysDate ]] ; then echo "The card for '$user' has expired" # Remove the card number "${serverCommand}" set-user-property $user "secondary-card-number" " " # Clear the date from the notes field "${serverCommand}" set-user-property $user "notes" " " fi fi done echo "Cleanup Finished"
This same theory could be used for guest users. You could create an internal user in PaperCut for guests so they could use Web Print or Mobility Print; put an expiration date into the notes field, and delete the user on the day. The examples below assume you are using a prefix of guest- for all of your guest users and the dd/mm/yyyy format for the expiration dates.
Powershell
CLS # Location of Server Command exe $serverCommand = 'C:\Program Files\PaperCut MF\server\bin\win\server-command.exe' $todaysDate = Get-Date Write-Host "Starting Cleanup" # Get user list $users = &$serverCommand list-user-accounts # Loop users foreach ($user in $users) { # If username starts with guest- if ($user -like 'guest-*') { # Get the note field $noteField = &$serverCommand get-user-property $user "notes" # If note field contents matches a date pattern (dd/mm/yyyy) if ($noteField -match '^([0-2][0-9]|(3)[0-1])(\/)(((0)[0-9])|((1)[0-2]))(\/)\d{4}$') { # Convert Note field to date object $noteFieldAsDate = Get-Date -Date $notefield # Check if the date is in the past if ($noteFieldAsDate -lt $todaysDate) { Write-Host "'" $user "' has expired" # Remove the user &$serverCommand delete-existing-user $user } } } } Write-Host "Cleanup Completed"
Bash
#!/bin/bash # Location of Server Command binary serverCommand=/home/papercut/server/bin/linux-x64/server-command todaysDate=$(date +%s) echo "Starting Cleanup" # Get user list and loop over them "${serverCommand}" list-user-accounts | while read user ; do # If username starts with guest- if [[ $user == "guest-"* ]] ; then # Get the note field noteField=$("${serverCommand}" get-user-property $user "notes") # If note field contents match a date pattern (dd/mm/yyyy) if [[ $noteField =~ ^([0-2][0-9]|(3)[0-1])(\/)(((0)[0-9])|((1)[0-2]))(\/)[0-9]{4}$ ]] ; then # Convert Note field to unix timestamp, We do something ugly with the string to put it into y/m/d... sorry! noteFieldAsDate=$(date +'%s' -d "${noteField: -4}/${noteField: 3: 2}/${noteField: 0: 2}") # Check if the date is in the past if [[ $noteFieldAsDate -lt $todaysDate ]] ; then echo "'$user' has expired" # Remove the user "${serverCommand}" delete-existing-user $user fi fi fi done echo "Cleanup Finished"
As you can see, with a bit of planning, you can provide temporary access to ID cards for your users.