PowerShell: Pavel Kodotšigov, CreateFromCSV.ps1: Difference between revisions
From ICO wiki
Jump to navigationJump to search
No edit summary |
No edit summary |
||
(2 intermediate revisions by the same user not shown) | |||
Line 5: | Line 5: | ||
.SYNOPSIS | .SYNOPSIS | ||
RAKENDUS LISAB DOMEENI KASUTAJAID ANTUD | RAKENDUS LISAB DOMEENI KASUTAJAID ANTUD CVS-FAILIST | ||
.DESCRIPTION | .DESCRIPTION | ||
ACTIVE DIRECTORY KASUTAJATE LISAMINE ETTENÄIDATUD | ACTIVE DIRECTORY KASUTAJATE LISAMINE ETTENÄIDATUD CVS-FAILIST | ||
CVS FAILI STRUKTUURI NÄIDIS: | |||
objectClass,displayName,samAccountName,sn,givenName | objectClass,displayName,samAccountName,sn,givenName,password,group | ||
user,John Travolta,JohnTravol,Travolta,John | user,John Travolta,JohnTravol,Travolta,John,Passw00rd,MyGroup | ||
.EXAMPLE | .EXAMPLE | ||
CreateFromCSV.ps1 < | CreateFromCSV.ps1 <CVS-FAIL> | ||
#> | #> | ||
# | # Leiame kasutajate konteiner | ||
$targetOU = (Get-ADDomain).UsersContainer | |||
# CVS faili asukoht | |||
$impfile = $args[0] | $impfile = $args[0] | ||
if (!$impfile) { | if (!$impfile) { | ||
write-host "Viga: Palun sisesta | write-host "Viga: Palun sisesta CVS-faili asukoht - väljun...." | ||
exit | exit | ||
} | } | ||
Line 52: | Line 55: | ||
Import-Module ActiveDirectory -ErrorAction SilentlyContinue | Import-Module ActiveDirectory -ErrorAction SilentlyContinue | ||
# Otsime jooksev domain info | # Otsime jooksev domain info | ||
Line 78: | Line 80: | ||
$givname = $user.givenname | $givname = $user.givenname | ||
$surname = $user.sn | $surname = $user.sn | ||
$password = $user.password | |||
$upname = "$samname" + "@" +"$domdns" | $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 | $cmd = "New-ADUser –Name $samname –SamAccountName ""$samname"" –DisplayName ""$dplname"" -givenname | ||
Line 85: | Line 99: | ||
invoke-expression $cmd | 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) | |||
} | } | ||
</source> | </source> |
Latest revision as of 00:00, 8 January 2012
<#
.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,password,group
user,John Travolta,JohnTravol,Travolta,John,Passw00rd,MyGroup
.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)
}