<h2 style="text-align: left;">How do I grant temporary access to company printers?</h2>
&nbsp;

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 allowed 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.

&nbsp;

This is how we helped the reseller, and hopefully, you'll find it useful too!

&nbsp;
<h3 style="text-align: left;">Where did we start?</h3>
<p style="text-align: left;">We started by looking at the elements that needed to address. Once we'd broken the problem down, we were able to see what elements were required for us to put a solution into motion, in this case, there were three parts.</p>

<ol>
 	<li>Issuing temporary cards</li>
 	<li>Automatically removing the card number from an account</li>
 	<li>Allowing the admin or card issuer to set an expiration date</li>
</ol>
&nbsp;
<h4 style="text-align: left;">Issuing temporary cards</h4>
The first part is fairly simple to handle. PaperCut can be set to self-associate new cards with the <a href="https://www.papercut.com/kb/Main/SelfAssociateSecondaryIDField">secondary ID field</a>, or as the cards are temporary the admin can put this value into the field manually.

&nbsp;
<h4 style="text-align: left;">Automatically removing card numbers</h4>
Next up is removing the card number automatically from an account and setting an expiration date. This can be done using the API or a server command using the scheduling abilities of your OS. In our example we've used Powershell, this would look something like the example below which removes the secondary ID for all users.

&nbsp;
<pre class="lang:ps"># 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 = &amp;$serverCommand list-user-accounts

# Loop over every user
foreach ($user in $users) {
  # Set the secondary-card-number user property to nothing
  &amp;$serverCommand set-user-property $user "secondary-card-number" "`" `""
}
</pre>
&nbsp;
<h4 style="text-align: left;">Set an expiration date</h4>
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 26th November 2018 they would input 23/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.

&nbsp;
<h5 style="text-align: left;">Powershell</h5>
<pre class="lang:ps">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 = &amp;$serverCommand list-user-accounts

# Loop users
foreach ($user in $users) {
  # Get the note field
  $noteField = &amp;$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

      &amp;$serverCommand set-user-property $user "secondary-card-number" "`" `""
      # Clear the date from the notes field
      &amp;$serverCommand set-user-property $user "notes" "`" `""
    }
  }
}

Write-Host "Cleanup Completed"
</pre>
<h5 style="text-align: left;">Bash</h5>
<pre class="lang:sh">#!/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"
</pre>
&nbsp;

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, and 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 your guest users and the dd/mm/yyyy format for the expiration dates.

&nbsp;
<h5 style="text-align: left;">Powershell</h5>
<pre class="lang:ps">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 = &amp;$serverCommand list-user-accounts

# Loop users
foreach ($user in $users) {
  # If username starts with guest-
  if ($user -like 'guest-*') {
    # Get the note field
    $noteField = &amp;$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
        &amp;$serverCommand delete-existing-user $user
      }
    }
  }
}
Write-Host "Cleanup Completed"
</pre>
<h5 style="text-align: left;">Bash</h5>
<pre class="lang:sh">#!/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"
</pre>
&nbsp;

As you can see, with a bit of planning, you can provide temporary access to ID cards for your users.
<h3 style="text-align: left;"></h3>
<h3 style="text-align: left;">If you have a task that you need a little help with, <a href="support@selectec.com">get in touch</a> with our team who will be happy to help out.</h3>