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