PowerShell: Pavel Kodotšigov, CreateFromCSV.ps1
From ICO wiki
<#
.SYNOPSIS
RAKENDUS LISAB DOMEENI KASUTAJAID ANTUD CVS-FAILIST
.DESCRIPTION
ACTIVE DIRECTORY KASUTAJATE LISAMINE ETTENÄIDATUD CVS-FAILIST
CVS FAILI STRUKTUURI NÄIDIS:
objectClass,displayName,samAccountName,sn,givenName
user,John Travolta,JohnTravol,Travolta,John
.EXAMPLE
CreateFromCSV.ps1 <CVS-FAIL>
#>
# Leiame kasutajate konteiner
$targetOU = (Get-ADDomain).UsersContainer
# CVS faili asukoht
$impfile = $args[0]
if (!$impfile) {
write-host "Viga: Palun sisesta CVS-faili asukoht - väljun...."
exit
}
# Funktsioon kontrollimiseks, kas on olemas AD objekt
function Test-XADObject() {
[CmdletBinding(ConfirmImpact="Low")]
Param (
[Parameter(Mandatory=$true,
Position=0,
ValueFromPipeline=$true,
HelpMessage="Kontrollimine, kas on olemas AD objekt."
)]
[Object] $Identity
)
trap [Exception] {
return $false
}
$auxObject = Get-ADObject -Identity $Identity
return $true
}
# Importime "Active Directory Powershell" Moodul
Import-Module ActiveDirectory -ErrorAction SilentlyContinue
# Otsime jooksev domain info
$domdns = (Get-ADDomain).dnsroot
# Kontrollime kas antud OU on olemas
$validOU = Test-XADObject $targetOU
If (!$validOU)
{
write-host "Viga: Sellist OU uute kasutajate jaoks ei eksisteeri - väljun...."
exit
}
$users = Import-CSV $impFile
foreach ($user in $users)
{
$samname = $user.samaccountname
$dplname = $user.displayname
$givname = $user.givenname
$surname = $user.sn
$password = $user.password
$upname = "$samname" + "@" +"$domdns"
$group = $user.group
$CNgroup = "CN=" +$group + ',' + $targetOU
$validCN = Test-XADObject $CNgroup
If (!$validCN)
{
echo "Loome uut grupp $group"
New-ADGroup -Name $group -Path $targetOU -groupScope universal
}
#Lisame kasutajat
$cmd = "New-ADUser –Name $samname –SamAccountName ""$samname"" –DisplayName ""$dplname"" -givenname
""$givname"" -surname ""$surname"" -userprincipalname ""$upname"" -Path '$targetOU'"
invoke-expression $cmd
#Lisame gruppi
Add-ADGroupMember -identity $CNgroup -member $samname
#Paneme salasõna
set-adaccountpassword -identity "$samname" -reset -newpassword (convertto-securestring "$password" -
asplaintext -force)
}