Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Code Implementation Report: Managing

Remote Desktop Users


Farhad Piri

Supervisor:
Dr. Rashidian
Introduction
This report outlines the implementation of two versions of a script designed to manage Remote
Desktop Users on a system. The primary goal of these scripts is to control user access to
Remote Desktop Services by adding and removing users from the "Remote Desktop Users"
group based on predefined allocation rules. The scripts achieve this functionality through the
use of text files and Windows batch scripting.
Script Version 1: Monthly User Allocation
Objective
The first script version aims to read a text file named allocation.txt containing user allocation
data and add users to the "Remote Desktop Users" group if they match the allocation for the
current day.
Implementation Details
1. Getting Current Date: The script starts by retrieving the current date using the wmic
command and parsing the output to extract the year, month, and day. The date is stored
in the today variable.
2. Loading User Allocations: The script reads the allocation.txt file line by line, extracting
the username and their allocated days using a loop and tokenization. For each user, the
script checks if the current day matches any of their allocated days.
3. Adding Users: If the current day matches any of the allocated days for a user, that user's
username is added to the "Remote Desktop Users" group using the net localgroup
command.
Difficulty and Challenges
 Parsing Date: The script uses complex string manipulation to parse the date obtained
from wmic output. This approach can be error-prone and difficult to understand.
 Complex Nested Loops: The script contains nested loops that can be challenging to
follow, making maintenance and debugging potentially difficult.
Script Version 2: User Rotation Prototype
Objective
The second script version focuses on rotating users in and out of the "Remote Desktop Users"
group in a cyclical manner, allowing one user at a time.
Implementation Details
1. Loading User List: The script defines a list of usernames. It reads the state.txt file to
determine which user should be added next to the "Remote Desktop Users" group.
2. Rotating Users: The script uses a cyclical rotation mechanism to determine the next
user to be added. It removes all users from the "Remote Desktop Users" group and adds
the next user based on the current rotation index.
3. Updating Rotation Index: After adding a user, the script updates the rotation index and
saves it in the state.txt file for the next run.
Difficulty and Challenges
 Rotation Logic: The script implements a rotation mechanism that might be difficult to
grasp at first glance, making it more challenging to modify and extend.
Conclusion
Both script versions demonstrate the ability to manage user access to Remote Desktop Services
through batch scripting. However, both versions have certain complexities and challenges that
could potentially impact maintainability, readability, and future modifications. The first version
employs complex date parsing and nested loops, while the second version relies on intricate
rotation logic.
As the scripts are intended to be executed using Task Scheduler on a monthly basis, it is
recommended to consider enhancing code readability, modularity, and documentation. This
will facilitate easier maintenance and troubleshooting, making the scripts more robust and
adaptable to future changes.
To provide visual confirmation of the users being added to the "Remote Desktop Users" group,
you can include screenshots or figures showing the group membership before and after script
execution. This will help illustrate the effectiveness of the scripts in achieving the intended user
management goals.
Script Version 2: User Rotation Prototype
Introduction
This section provides a detailed breakdown of the code for the second version of the script. The
objective of this script is to rotate users in and out of the "Remote Desktop Users" group,
granting access to one user at a time. The script achieves this by maintaining a rotation index
and updating it in a cyclical manner.
Code Breakdown

@echo off
setlocal enabledelayedexpansion

The script starts by turning off command echoing and enabling delayed variable expansion. This
allows variables to be accessed within loops and blocks.

REM List of usernames


set "usernames=test1 test2 test3"

The list of usernames is defined using space-separated values. You can extend this list with
additional usernames as needed.

REM Load the last added user index from the state file
set "stateFile=state.txt"
if exist %stateFile% (
set /p "current=" < %stateFile%
) else (
echo 0 > state.txt
set "current=0"
)

The script checks if the state.txt file exists. If it does, the script reads the last added user index
from the file. Otherwise, it initializes the file with a default value of 0 and sets the current index
accordingly.

REM Remove all users from "Remote Desktop Users" group


for %%u in (%usernames%) do (
net localgroup "Remote Desktop Users" %%u /delete
)

This loop iterates through each username in the list and removes them from the "Remote
Desktop Users" group using the net localgroup command.
REM Add the next user to the "Remote Desktop Users" group
set "currentIndex=0"
for %%u in (%usernames%) do (
set /a "compIndex=currentIndex"
set /a "compCurrent=current"

if !compIndex! equ !compCurrent! (


net localgroup "Remote Desktop Users" %%u /add
goto :end
)
set /a "currentIndex+=1"
)
:end
set /a "current=(current+1) %% 3"

 The script starts by initializing currentIndex to 0. It then iterates through the usernames
and compares compIndex (current index) with compCurrent (last added user index). If
they match, the current user is added to the "Remote Desktop Users" group, and the
script jumps to the :end label.
 After the loop, the script increments the current index and ensures it wraps around to 0
when it reaches the end of the list. This prepares the index for the next run.

 REM Save the next user index to the state file


 echo %current% > %stateFile%

Finally, the script saves the updated current index to the state.txt file, ensuring that the
rotation continues correctly in subsequent runs.
Conclusion
This version of the script demonstrates a cyclical user rotation mechanism for managing access
to Remote Desktop Services. By maintaining a current index and updating it in a cyclical
manner, the script provides controlled access to one user at a time.
While the script achieves its goal, it does involve some intricate logic and variable manipulation.
To improve code maintainability, you might consider breaking down the logic into functions,
adding comments to clarify complex sections, and structuring the script for better readability.

You might also like