Users in a domain login to different workstations every day. This part of the…
- User
User: Users in a domain login to different workstations every day. This part of the script is going to collect logon successfully events, event id 4624 of a user among different workstations and Windows servers in the domain. (a) List all the existing usernames in the domain. Read the input from the user and check if the username already exists in the domain.
<# Print menu for Choice 2: User Login Success Informtion #> function output_choice2{ Write-Host "Login Information loading .....
n" -ForegroundColor DarkGray Write-Host "nExisting Users :" Write-Host "--------------"code<# Part 3 #>} Assistant: To create a script that lists all existing usernames in a domain and checks if a specific username exists based on user input, you can utilize PowerShell's Active Directory cmdlets. Ensure the Active Directory module is installed and the script is run in an environment with the necessary permissions.
Here's a script that demonstrates how you might implement this functionality:
powershell# Import the Active Directory module Import-Module ActiveDirectory # Function to output user login success information function output_choice2 { Write-Host "Login Information loading ..... `n" -ForegroundColor DarkGray Write-Host "`nExisting Users :" Write-Host "--------------" # Get all users from the Active Directory $users = Get-ADUser -Filter * | Select-Object -ExpandProperty SamAccountName # Display the list of users $users | ForEach-Object { Write-Host $_ } # Prompt the user to enter a username $username = Read-Host -Prompt "`nEnter a username to check" # Check if the username exists in the list if ($users -contains $username) { Write-Host "`nThe username '$username' exists in the domain." -ForegroundColor Green } else { Write-Host "`nThe username '$username' does not exist in the domain." -ForegroundColor Red } } # Call the function to execute it output_choice2 - Assistant
Certainly! Below is an example of how the sample output might look when the script is executed in a PowerShell terminal.
Sample Output
codeLogin Information loading ..... Existing Users : -------------- user1 user2 user3 user4 user5 Enter a username to check: user3 The username 'user3' exists in the domain.Another Example
If a username does not exist:
codeLogin Information loading ..... Existing Users : -------------- user1 user2 user3 user4 user5 Enter a username to check: user6 The username 'user6' does not exist in the domain.Explanation of Output
-
Loading Message: The message indicates that the login information is being loaded, providing a user-friendly interface.
-