K
K
Knorke_022022-02-06 16:23:09
PowerShell
Knorke_02, 2022-02-06 16:23:09

What is the correct way to write a PowerShell script?

The bottom line is, you need to write a script so that users are created in Active Directory with it:
1. Number of users
2. User names
3. Password for users
I wrote such a script, I see an error in it, but I can’t do it, I ask for help
Import-Module ActiveDirectory

# Variables
$number= Read-Host "Enter the number of users"
$input = Read-Host 'Enter a name'
$pass = Read-Host 'Enter the password'
$count=1..$number

# Create OU
New -ADOrganizationalUnit -Name "DemoOffice" -Path “DC=demo,DC=lab”
New-ADOrganizationalUnit -Name "Users" -Path “OU=DemoOffice,DC=demo,DC=lab”
New-ADOrganizationalUnit -Name "Computers" -Path “OU=DemoOffice,DC=demo,DC=lab”

# Loop with users
foreach ($i in $count) {

$Users = @{

Name = "$input"
GivenName = " $input"
UserPrincipalName = "[email protected]"
Path = "OU=Users,OU=DemoOffice,DC=demo,DC=lab"
ChangePasswordAtLogon = $true
AccountPassword = "$pass" | ConvertTo-SecureString -AsPlainText -Force
Enabled = $true

}

New-ADUser @Users

}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Knorke_02, 2022-02-06
@Knorke_02

Issue resolved, Thanks Eugene
Import-Module ActiveDirectory
# Create OU
New-ADOrganizationalUnit -Name "DemoOffice" -Path “DC=demo,DC=lab”
New-ADOrganizationalUnit -Name "Users" -Path “OU=DemoOffice,DC=demo, DC=lab”
New-ADOrganizationalUnit -Name "Computers" -Path “OU=DemoOffice,DC=demo,DC=lab”
$number = Read-Host "Enter number of users"
$count=1..$number
$users = foreach ($i in $count)
{
Read-Host "Enter username number $i"
}
$pass = Read-Host 'Enter the password'
# Loop with users
foreach ($user in $users) {
$Username = @{
Name = "$user"
GivenName = "$user"
UserPrincipalName = "[email protected]"
Path = "OU=Users,OU=DemoOffice,DC=demo,DC=lab"
ChangePasswordAtLogon = $true
AccountPassword = "$pass" | ConvertTo-SecureString -AsPlainText -Force
Enabled = $true
}
New-ADUser @Username
}

M
MaxKozlov, 2022-02-06
@MaxKozlov

Move the $input filling from read-host inside the loop.
And, for a better understanding, it's worth renaming $users to $user because in the sense it's about one user

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question