#Installeer eerst (download de module): Install-Module -Name ImportExcel
Import-Module ImportExcel
$FilePath = "C:\Users\Administrator2\Desktop\Script\Data_users_wpl.xlsx" #locatie van excel
$data = Import-Excel -Path "$FilePath" #importeren van excel
$domain = "dromenvanger" #domeinnaam
$shareRoot = "\\wpl20_DC01\C$\Shares" # verander naar servernaam en locatie van shares rootfolder
function Get-RandomPassword {
[Parameter(Mandatory)]
[ValidateRange(4,[int]::MaxValue)]
[int] $length,
[int] $upper = 1,
[int] $lower = 1,
[int] $numeric = 1,
[int] $special = 1
)
if($upper + $lower + $numeric + $special -gt $length) {
throw "number of upper/lower/numeric/special char must be lower or equal to length"
}
$uCharSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
$lCharSet = "abcdefghijklmnopqrstuvwxyz"
$nCharSet = "0123456789"
$sCharSet = "/*-+,!?=()@;:._"
$charSet = ""
if($upper -gt 0) { $charSet += $uCharSet }
if($lower -gt 0) { $charSet += $lCharSet }
if($numeric -gt 0) { $charSet += $nCharSet }
if($special -gt 0) { $charSet += $sCharSet }
$charSet = $charSet.ToCharArray()
$rng = New-Object System.Security.Cryptography.RNGCryptoServiceProvider
$bytes = New-Object byte[]($length)
$rng.GetBytes($bytes)
$result = New-Object char[]($length)
for ($i = 0 ; $i -lt $length ; $i++) {
$result[$i] = $charSet[$bytes[$i] % $charSet.Length]
}
$password = (-join $result)
$valid = $true
if($upper -gt ($password.ToCharArray() | Where-Object {$_ -cin $uCharSet.ToCharArray() }).Count) { $valid = $false }
if($lower -gt ($password.ToCharArray() | Where-Object {$_ -cin $lCharSet.ToCharArray() }).Count) { $valid = $false }
if($numeric -gt ($password.ToCharArray() | Where-Object {$_ -cin $nCharSet.ToCharArray() }).Count) { $valid = $false }
if($special -gt ($password.ToCharArray() | Where-Object {$_ -cin $sCharSet.ToCharArray() }).Count) { $valid = $false }
if(!$valid) {
$password = Get-RandomPassword $length $upper $lower $numeric $special
}
return $password
}
foreach ($row in $data) {
$voornaam = $row.voornaam #excel rij moet beginnen met "voornaam"
$achternaam = $row.achternaam #excel rij moet beginnen met "achternaam"
$ouName = $row.OU #excel rij moet beginnen met "OU"
$groupName = $row.Group #excel rij moet beginnen met "Group"
$username = "$voornaam.$achternaam@$domain.local"
if (-not (Get-ADuser -filter {SamAccountName -eq $username})) {
$password = Get-RandomPassword 8
$params = @{
SamAccountName = $username
UserPrincipalName = $username
Name = "$voornaam $achternaam"
GivenName = $voornaam
Surname = $achternaam
AccountPassword = convertTo-SecureString -String $password -AsPlainText -Force
ChangePasswordAtLogon = $true
Enabled = $true
Path = $ouName # Set the OU for the new user
}
$newUser = New-ADUser @params
Add-ADGroupMember -Identity $groupName -Members $newUser
$userSharePath = Join-Path -Path $shareRoot -Childpath $username
New-Item -Path $userSharePath -ItemType Directory | Out-Null
$acl = Get-Acl -Path $userSharePath
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule($username, "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow")
$acl.AddAccessRule($rule)
Set-Acl -Path $userSharePath -AclObject $acl
$row.Username = $username #excel rij moet beginnen met "Username"
$row.Password = $password #excel rij moet beginnen met "Password"
}
$data | Export-Excel -Path $excelFilePath -AutoSize -NoHeader -StartRow 1 -ClearSheet
}
I keep getting errors and i cant find really any solutions, maybe some of you can help me with it.