Last active
February 21, 2022 08:33
-
-
Save b4tman/fa6716f6e70460365b9f2075656597d5 to your computer and use it in GitHub Desktop.
Создание локальных пользователей Windows для RDP подключений из CSV списка
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# перед созданием лучше отключить политику "Пароль должен отвечать требованиям сложности" | |
# в gpedit.msc а потом включить | |
$group_Users = Get-LocalGroup -Name "Пользователи" | |
$group_RDP = Get-LocalGroup -Name "Пользователи удаленного рабочего стола" | |
function CreateUser ($Username, $Password) { | |
$sec_passwd = ConvertTo-SecureString -String $Password -AsPlainText -Force | |
return New-LocalUser -Name $Username -AccountNeverExpires -Description $Username -FullName $Username -Password $sec_passwd -PasswordNeverExpires | |
} | |
$users = Get-Content users.csv|ConvertFrom-Csv -Delimiter ';' | |
$users|foreach -Process { | |
$current_user = Get-LocalUser -Name $_.User -ErrorAction SilentlyContinue | |
if ($current_user -eq $null) { | |
$current_user = CreateUser -Username $_.User -Password $_.Password | |
Write-Output "Добавлен пользователь: $current_user" | |
} else { | |
Write-Output "Пользователь существует: $current_user" | |
} | |
Add-LocalGroupMember -Group $group_Users -Member $current_user -ErrorAction SilentlyContinue | |
Add-LocalGroupMember -Group $group_RDP -Member $current_user -ErrorAction SilentlyContinue | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$folder="." | |
$count=25 | |
$PassLen=10 | |
$SChars=1 | |
$prefix="RDP" | |
push-location $folder | |
[string[]]$users_names = @() | |
$users = @() | |
Function Generate-Complex-Domain-Password ([Parameter(Mandatory=$true)][int]$PassLenght) | |
{ | |
Add-Type -AssemblyName System.Web | |
$requirementsPassed = $false | |
do { | |
$newPassword=[System.Web.Security.Membership]::GeneratePassword($PassLenght, $SChars) | |
If ( ($newPassword -cmatch "[A-Z\p{Lu}\s]") ` | |
-and ($newPassword -cmatch "[a-z\p{Ll}\s]") ` | |
-and ($newPassword -match "[\d]") ` | |
-and ($newPassword -match "[^\w]") | |
) | |
{ | |
$requirementsPassed=$True | |
} | |
} While ($requirementsPassed -eq $false) | |
return $newPassword | |
} | |
For ($i = 1; $i -le $count; $i++) { | |
[string[]]$user_names += [string]"${prefix}${i}" | |
} | |
$user_names | foreach-object -Process { | |
$row = "" | Select User,Password | |
$row.User = $_ | |
$row.Password = Generate-Complex-Domain-Password($PassLen) | |
$users += $row | |
} | |
$users | ConvertTo-Csv -Delimiter ';'| Set-Content users.csv | |
$users | |
pop-location |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment