Vote count:
0
I really really need your help. I got a task to create/import users from csv to AD.
But I have two problems 1. not all the user have all the attributes filled = there are empty "cells" in CSV 2. Two custom attributes contains dash - and PSH is returning error that this is not allowed.
I tried to search, there are some solutions but I am not sure how to implement it to my file (| ForEach-Object {$CSV = $_ ?????. Cant make it work. Can you help, PLEASE... ???
Here is what I have so far
Import-Module ActiveDirectory
$ErrorActionPreference = 'SilentlyContinue'
$ScriptDir = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
$log = "$ScriptDir\Error-Import.log"
$log1 = "$ScriptDir\Success-Import.log"
$date = Get-Date
$inputFile = Import-CSV $ScriptDir\allusers.csv
Function cUser
{
"ERROR LOG from (" + $date + ") :" | Out-File $log -Append
"————————————————-" | Out-File $log -Append
"Following accounts has been successfully created (" + $date + ") :" | Out-File $log1 - Append
"————————————————-" | Out-File $log1 -Append
foreach($line in $inputFile)
{
$sam = $line.samaccoutnname
#check if user already exists
$exists = Get-ADUser -LDAPFilter "(sAMAccountName=$sam)"
if (!$exists)
{
# when does not exists - load data from CSV and set as variable
$gn = $line.givenName
$sn = $line.sn
$title = $line.title
$stAd=$line.streetAddress
$upn = $line.sAMAccountName + "`@targetrange.local"
$city=$line.l
$st=$line.st
$postc=$line.postalCode
$ehrd=$line.award-hireDate
$comp=$line.company
$dvs=$line.division
$edvs=$line.award-subdivision
$dept=$line.department
$st=$line.street
$tarA=$line.targetAddress
$dn=$line.displayName
$ext2=$line.extensionAttribute2
$ext4=$line.extensionAttribute4
$ext5=$line.extensionAttribute5
$alias = $line.sAMAccountName
$cn=$line.cn
$hdir=$line.unixHomeDirectory
$desc=$line.description
$pat = $line.OU
$pwd = $line.pass
$spass =ConvertTo-SecureString -AsPlainText -Force -String $pwd
$aexp=$line.accountExpires
$Expconv=(Get-Date $aexp ).ToFileTime()
New-ADUser -SamAccountName $alias -UserPrincipalName $upn -GivenName $gn -Surname $sn -title $title -StreetAddress $stAd -l $city -State $st -PostalCode $postc -award-hireDate $ehrd -Company $comp -Division $dvs -award-subdivision $edvs -Department $dept -street $st -targetAddress $tarA -extensionAttribute2 $ext2 -extensionAttribute4 $ext4 -extensionAttribute5 $ext5 -DisplayName $dn -Name $cn -unixHomeDirectory $hdir -Description $Desc -path $pat -AccountExpirationDate $Expconv -AccountPassword $spass -PassThru | Enable-ADAccount
$exists1 = Get-ADUser -LDAPFilter "(sAMAccountName=$sam)"
if ($exists1)
{
"User successfully created : " + $sam | Out-File $log1 -Append
}
Else
{
"SKIPPED – MISSING OR INCORRECT VALUES : " + $sam | Out-File $log -Append
}
}
# if user already exists - skip and write to logfile
Else
{
"SKIPPED – ALREADY EXISTS OR DUPLICATE ALIAS : " + $sam | Out-File $log -Append
}
}
"————————————————-" + "`n" | Out-File $log -Append
}
createUsers
# finito
}
If you can help, it would save my life :-) How to skip empty cells??? And to make attributes with - working - apostrophe is enough? 'award-hiredate' ?
Thank you in advance, its already 2 AM here and I am lost, totally ...Please help
asked 56 secs ago
powershell - new-aduser skip empty cells CSV - attributes with -
Aucun commentaire:
Enregistrer un commentaire