使用 PowerShell 脚本在 AD 中批量添加用户时出错

使用 PowerShell 脚本在 AD 中批量添加用户时出错

我正在尝试使用从 csv 文件中提取信息的 PowerShell 脚本将用户添加到 AD。我基本上可以正常工作,因为创建了用户并且它们似乎可以正常工作。但是,我在运行脚本时遇到了一些异常,我需要一些帮助来调试它并找出它失败的地方。脚本、csv 代码片段和错误如下。另请注意,似乎错误不会在每次创建用户时发生;我只有几次错误迭代。

有没有办法可以将其包装在 if/then 语句中,以打印出错误点发生的情况,或者查看哪个 AD 对象失败以及原因?或者其他调试方法?

$objOU=[ADSI]“LDAP://OU=People,DC=testdomain,DC=com”
$dataSource=import-csv “test users2.csv”
foreach($dataRecord in $datasource) {
    $cn=$dataRecord.FirstName + ” ” + $dataRecord.LastName
    $sAMAccountName=$dataRecord.UserName
    $givenName=$dataRecord.FirstName
    $sn=$dataRecord.LastName
    $sAMAccountName=$sAMAccountName.ToLower()
    $displayName=$givenName + " " + $sn
    $userPrincipalName=$sAMAccountName + “@testdomain.com”
    $sISID=$dataRecord.Sisid
    $objUser=$objOU.Create(“user”,”CN=”+$cn)
    $objUser.Put(“sAMAccountName”,$sAMAccountName)
    $objUser.Put(“userPrincipalName”,$userPrincipalName)
    $objUser.Put(“displayName”,$displayName)
    $objUser.Put(“givenName”,$givenName)
    $objUser.Put(“sn”,$sn)
    $objUser.Put("description",$dataRecord.Gender + ", " + "Class of " + $dataRecord.Graduation + ", " + $sISID)
    $objUser.SetInfo()
    $objUser.SetPassword($dataRecord.Password)
    $objUser.psbase.InvokeSet(“AccountDisabled”,$false)
    $objUser.SetInfo()
   }

USERNAME,PASSWORD,SISID,FIRSTNAME,LASTNAME,GRADUATION,GENDER
usera,dfqt4d,1111110681,Akeem,xxxxx,2016,M
userb,nw97ph,1111166963,Ariel,xxxxx,2015,F

Exception calling "SetInfo" with "0" argument(s): "A device attached to the system is not functioning. " At C:\For Sharing\add users script.ps1:19 char:17
    + $objUser.SetInfo <<<< ()
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : CatchFromBaseAdapterMethodInvokeTI   

Exception calling "SetPassword" with "1" argument(s): "There is no such object on the server. " At C:\For Sharing\add users script.ps1:20 char:21
    + $objUser.SetPassword <<<< ($dataRecord.Password)
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : CatchFromBaseAdapterMethodInvokeTI   

Exception calling "InvokeSet" with "2" argument(s): "The directory property cannot be found in the cache. " At C:\For Sharing\add users script.ps1:21 char:26
    + $objUser.psbase.InvokeSet <<<< (“AccountDisabled”,$false)
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : DotNetMethodTargetInvocation

答案1

PowerShell 2.0 支持试着抓块,这样您就可以捕获并处理任何异常。

您可以Write-Host在 catch 块中放置一个,以向控制台回显哪个用户导致了脚本问题。

例如,您的脚本看起来像这样。

$objOU=[ADSI]“LDAP://OU=People,DC=testdomain,DC=com”
$dataSource=import-csv “test users2.csv”
foreach($dataRecord in $datasource)
{
    try
    {
        $cn=$dataRecord.FirstName + ” ” + $dataRecord.LastName
        $sAMAccountName=$dataRecord.UserName
        $givenName=$dataRecord.FirstName
        $sn=$dataRecord.LastName
        $sAMAccountName=$sAMAccountName.ToLower()
        $displayName=$givenName + " " + $sn
        $userPrincipalName=$sAMAccountName + “@testdomain.com”
        $sISID=$dataRecord.Sisid
        $objUser=$objOU.Create(“user”,”CN=”+$cn)
        $objUser.Put(“sAMAccountName”,$sAMAccountName)
        $objUser.Put(“userPrincipalName”,$userPrincipalName)
        $objUser.Put(“displayName”,$displayName)
        $objUser.Put(“givenName”,$givenName)
        $objUser.Put(“sn”,$sn)
        $objUser.Put("description",$dataRecord.Gender + ", " + "Class of " + $dataRecord.Graduation + ", " + $sISID)
        $objUser.SetInfo()
        $objUser.SetPassword($dataRecord.Password)
        $objUser.psbase.InvokeSet(“AccountDisabled”,$false)
        $objUser.SetInfo()
    }
    catch
    {
        Write-Host "*** Exception Handler ***"
        Write-Host "Problem username: " $dataRecord.UserName
        Write-Host "*************************"
    }
}

至于脚本可能崩溃的原因,sAMAccountName 是否唯一(例如 John Smith 和 James Smith 都是 jsmith)以及 sAMAccountName 是否包含任何特殊字符(符号、带重音符号的字母等)?我能想到的几个原因可以解释为什么它可能导致异常。

相关内容