【问题标题】:powershell - new-aduser skip empty cells CSV - attributes with -powershell - new-aduser 跳过空单元格 CSV - 属性 -
【发布时间】:2014-09-17 23:47:49
【问题描述】:

我真的很需要你的帮助。我的任务是从 csv 创建/导入用户到 AD。

但是我有两个问题

  1. 并非所有用户都填写了所有属性 = CSV 中有空的“单元格”
  2. 两个自定义属性包含破折号 - 并且 PSH 返回错误,这是不允许的。

我尝试搜索,有一些解决方案,但我不确定如何将其实施到我的文件中 (| ForEach-Object {$CSV = $_ ?????。无法使其工作。

这是我目前所拥有的

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
  } 

如果您能提供帮助,那将挽救我的生命 :-) 如何跳过空单元格???并且使用-working-撇号来制作属性就足够了吗? '奖励聘用'?

【问题讨论】:

  • 第 20 行 ish $sam = $line.samaccoutnname 有一个错字,可能导致脚本失败。
  • 那么 ANY 单元格是否可以为空,您必须考虑到它。如果一行中有一个空单元格,您会忽略它而不是根据它创建用户吗?

标签: powershell csv active-directory


【解决方案1】:

您提到您想跳过空单元格。我不确定你想走多远,但我认为这可以解决问题。你有电话线。

$inputFile = Import-CSV $ScriptDir\allusers.csv 

我建议更新如下

$inputFile = Import-CSV $ScriptDir\allusers.csv |  Where-Object{$_.psobject.properties.value -notcontains ""}

这将做的是将每个条目分解为其值的数组。如果任何值为空或"",则它们将不会匹配到$inputFile

Count                         EventID                       EventType                    
-----                         -------                       ---------                    
14                                                          Information                  
2                             7040                          Information                  
2                             0                                                          
1                             8224                          Information   

将以上数据放入过滤器,输出为

Count                         EventID                       EventType                    
-----                         -------                       ---------                    
2                             7040                          Information                  
1                             8224                          Information    

如您所见,带有空数据的行被过滤掉了,数据结构仍然保留。

连字符

至于连字符- 问题,我假设您得到的错误是Unexpected token '-Type' in expression or statement. 猜测它正在尝试处理$line.award-hireDate 中的-hireData。在这种情况下,您尝试做的是最好的。只需在参数名称周围使用引号。如果参数中有空格,您也会这样做。

$line."award-hireDate"
$line."award hireDate"

【讨论】:

  • 非常感谢!!!!多亏了你,我现在明白它是如何工作的。我将脚本拆分为两个单独的函数,因为我需要创建所有用户,但只需要设置 NOT EMPTY 属性。而且您的更新起到了作用!我和它战斗了好几天!谢谢谢谢!
  • 很高兴听到这个消息。我很高兴它有帮助。请将此标记为已接受的答案,以表示您的感谢。希望在 SO 上再次见到你。
猜你喜欢
  • 2021-12-03
  • 1970-01-01
  • 1970-01-01
  • 2018-09-12
  • 1970-01-01
  • 2014-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多