Share
Explore

w24 IN2054 - G10 - Lab 1 Instructions

Learning Outcomes:
Gain practical experience in Windows DEVOPS workflows and Python-scripted user management.

You can download Oracle Virtual Box from

Overview:

This lab is designed to familiarize students with standard Windows DEVOPS workflows by installing Windows Server in VMWARE, setting up Anaconda PYTHON, Sublime Text Editor, and using Python to create 100 users from data in a text file.

Lab Instructions: Section 1: Installing Windows Server ISO in VMWARE

Download the Windows Server ISO from the official Microsoft website.
Open VMware Workstation and create a new virtual machine.
Follow the prompts to install Windows Server using the downloaded ISO.

Section 2: Installing Anaconda PYTHON and Sublime Text Editor

Download PYTHON
Download Anaconda PYTHON from the official website and follow the installation instructions.
Configure the Anaconda environment variables as per the provided documentation.
Download and install Sublime Text Editor from the official website.

Once you have installed
PYTHON VSC
Open a PowerShell Terminal:
Windows Start Menu : enter PowerShell
Once you have gotten into your Command Terminal
Make a Directory
Change to the ROOT of your File System
mkdir LabTest1
megaphone

cd \

mkdir LabTest1
chdir LabTest1

Your command prompt will now indicate that your Active Directory is c:\LabTest1

Section 3: Using Python to Create 100 Users in Windows

Create a new Python script file named create_users.py using Sublime Text Editor.
Use the following Python code to create 100 users in Windows:

import subprocess
for i in range(1, 101): username = f"User{i}" password = "StrongPassword123" command = f'New-LocalUser -Name "{username}" -Password (ConvertTo-SecureString -AsPlainText "
{password}" -Force) -AccountNeverExpires $true' subprocess.run(["powershell", "-Command", command], shell=True)
megaphone

Let's break down each line of the Python script and understand how it works:

import subprocess: This line imports the Python subprocess module, which allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes.
The Python `subprocess` module provides a way to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. It allows you to run new programs, as well as capture their output or error messages. The `subprocess` module is a powerful tool for interacting with the operating system and executing external commands or scripts.
Key functions and classes within the `subprocess` module include: 1. `subprocess.run()`: This function executes a command or a script and waits for it to finish. It is a simple way to run an external command and collect its output. You can specify the command as a list of arguments or as a single string that is parsed to locate the path and the name of the command to run.
2. `subprocess.Popen()`: This class allows you to spawn a new process and obtain handles for its input, output, and error pipes, as well as process management functions. It provides a more flexible way to control the behavior of the executed process, such as setting environment variables, working directory, and input/output handling.
3. Other functions and constants: The `subprocess` module also provides other functions and constants for handling input/output redirection, error handling, and process management.
Overall, the `subprocess` module is commonly used for running system commands, launching other programs, and controlling input/output streams from within Python. It is particularly useful for tasks such as running shell commands, interacting with external scripts or programs, and managing concurrent subprocesses.
for i in range(1, 101):: This line initiates a for loop that will execute the following code 100 times, with i taking on values from 1 to 100.
username = f"User{i}": This line creates a string username using an f-string. The value of i is inserted into the string, creating unique usernames like "User1", "User2", and so on.
password = "StrongPassword123": This line sets a default password for the new users. In a production environment, you would want to generate secure passwords or prompt the user for password input.
text
command = f'New-LocalUser -Name "{username}" -Password (ConvertTo-SecureString -AsPlainText "{password}" -Force) -AccountNeverExpires $true'
: This line constructs a PowerShell command to create a new local user. It uses the username and password variables within the command string using f-string formatting. The command creates a new local user with the specified username, assigns the specified password using a secure string conversion, and sets the account to never expire.
text
subprocess.run(["powershell", "-Command", command], shell=True)
: This line uses the subprocess.run function to execute the constructed PowerShell command. It starts a new process with the provided 'powershell' command and passes the constructed PowerShell command as an argument. The shell=True parameter indicates that a shell should be used to execute the command.
In summary, the Python script utilizes the subprocess module to run PowerShell commands for creating 100 local users on a Windows system. It dynamically generates unique usernames, sets a default password, constructs the PowerShell command for user creation, and then executes the command for each user within the specified range.

Section 4: Illustrating Standard Windows DEVOPS Workflows

Open PowerShell as an administrator and navigate to the directory where the create_users.py file is located.

Execute the Python script using the command python create_users.py.
Verify the successful creation of the 100 users using the Get-LocalUser PowerShell command.
Lab Assessment Criteria:
The successful installation of Windows Server in VMWARE.
The correct installation and configuration of Anaconda PYTHON and Sublime Text Editor.
The accurate creation of 100 users from the provided Python script using PowerShell.
The documentation of any issues encountered and their resolutions.
Additional Notes:
Encourage students to reference Python documentation, PowerShell documentation, and community forums for troubleshooting any installation, configuration, or coding issues.
Remind students to ensure accurate and secure user creation, adhering to best practices in user management and code execution security.

Part 2: Modify the Python script to import usernames from a text file

megaphone

You can modify the Python script to import usernames from a text file. Here's an updated version of the script that reads usernames from a text file and creates user accounts based on the usernames provided:

python
import subprocess
# Open the text file containing the usernames with open('usernames.txt', 'r') as file: usernames = file.readlines()
# Strip any whitespace characters from the usernames usernames = [username.strip() for username in usernames]
# Iterate through the list of usernames and create user accounts for username in usernames: password = "StrongPassword123" # Example default password command = f'New-LocalUser -Name "{username}" -Password (ConvertTo-SecureString -AsPlainText "{password}" -Force) -AccountNeverExpires $true' subprocess.run(["powershell", "-Command", command], shell=True) In this modified script:
The open('usernames.txt', 'r') statement opens a text file named usernames.txt in read mode. The readlines() method reads all the lines from the file and returns them as a list. The list comprehension [username.strip() for username in usernames] is used to remove any leading or trailing whitespace characters from each username. The modified for loop iterates through the list of usernames and creates a user account for each username.
You just need to create a text file named usernames.txt with each username on a separate line. When you run the script, it will read the usernames from the file and create user accounts accordingly.
By incorporating this modification, you can import usernames from a text file and use them to create user accounts on the Windows system.

Make a suitable TEXT FILE with some names. Here are samples you can use:

SherlockH HarryP0tter RonW3asley Herm10neG GandalfTh3Grey Frod0Baggins DaenerysT TormundGi4ntsbane TyrionL4nnister AryaSt4rk ObiWanK3nobi LukeSkywalker PrincessLeia HanSol0 DarthVad3r CaptainJackSparrow ElizabethSwann WillTurn3r JonSnow24 DanaScully FoxMuld3r WalterWhit3 JessePinkm4n TyrionL4nnister CerseiL4nnister JamieL4nnister DanaerysTargaryen J0ffreyB4rotheon SansaSt4rk WalterWhit3 Skyl3rWhit3 SaulGoodm4n D0nDraper PeggyOls3n TyrionL4nnister ChanceGardn3r EllieBishop AnthonyDiN0zzo Ziv4David LeroyJ3throGibbs RossGell3r RachelGre3n ChandlerB1ng MonicaGell3r PhoebeBuffay JoeyTr1b1anni FranFine MaxwellSh3ffield NilesCrane FrasierCr4ne MartinCr4ne JohnLock3 SherlockHolmes JoanWatson MycroftHolmes GregHouse LisaCuddy JamesWils0n CameronHow3 ForemanD GregoryHouse BuffySumm3rs WillowRosenb3rg XanderHarris RupertGiles Angelus SpikeV4mpire Cord3liaChase MarshallEr1ksen LilyAld3rin TedM0sby BarneyStins0n RobinSch3rbatsky LizL3mon JackD0naghy KennethParc3l TracyJ0rdan J0nathanL3ast DougH3ffernan CarrieBr4dshaw SamanthaJ0nes Charlott3Yorke Mirand4Hobbes Mr.Big Ton3yS0prano CarmelaS0prano Dr.Melfi PaulieGualti3ri SilvioD4nte Chr1stopherMoltisanti TonyBlund3tt0 AJS0prano MeadowS0prano UncleJun1or AdrianMonk Nat4lieTeeger LelandStottlemey3r SharonaFleming RandyDisher CaptainSt0ttl3meyer.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.