新ADUser便有:对象名称有语法错误 [英] New-Aduser : The object name has bad syntax

查看:821
本文介绍了新ADUser便有:对象名称有语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我用它来创建工作正常csv文件大宗用户的脚本。

I have a script which i use to create bulk users from a csv file which works fine.

Import-Csv e:\temp\newemps.csv | %{
    $ou = $_.ou
    $firstname = $_.first
    $lastName = $_.last
    $accountName = $("{0}{1}" -f $firstname.Substring(0,1),$lastName).ToLower()
    $description = $_.desc
    $password = "Welcome1"
    $name = "$firstName $lastName"
    New-AdUser -SamAccountName $accountName -GivenName $firstName -UserPrincipalName "$accountName@ba.net" -Surname $lastName -DisplayName $name  -Name $name -AccountPassword (ConvertTo-SecureString -AsPlainText $password -Force) -Enabled $true -Path $ou -Description $description -ChangePasswordAtLogon:$False

    If ($_.Group -ne ""){
        Add-adgroupmember -identity $_.group  -members $accountName
    }

    If ($_.email -eq "y"){
        Enable-Mailbox -Identity $accountName -Alias $accountName
        Set-Mailbox $accountName -MaxSendSize 10mb -MaxReceiveSize 10mb
        Get-CasMailbox $accountName -OwaEnabled:$false -ActiveSyncEnabled:$false
    }
}

我试图修改此脚本,这样我就可以创造出一些通用的帐户,将不会按照我们的一般惯例。输入是此处的字符串作为应该到CSV作为唯一的唯一项目是机场code。我已经缩短了此处的字符串为简便起见。

I was trying modify this script so that i could create some generic accounts that would not follow our typical convention. The input is a here-string as supposed to a csv as the only unique item is an Airport code. I have shortened the here-string for brevity.

$bases = @"
YAB
YEK
YYH
YHI
"@

$bases.Split("`n") | %{    
    $ou = "CN=Users,DC=BA,DC=NET"
    $firstname = "$_".ToString()
    $lastName = "Counter"
    $accountName =  "$_" + "Counter"
    $description = "Base Front Counter"
    $password = "Welcome1"
    $name = "$firstName $lastName"
    New-AdUser -SamAccountName $accountName -GivenName $firstName -UserPrincipalName "$accountName@ba.net" -Surname $lastName -DisplayName $name  -Name $name -AccountPassword (ConvertTo-SecureString -AsPlainText $password -Force) -Enabled $true -Path $ou -Description $description -ChangePasswordAtLogon:$False
}

也有一些是关于使用此处的字符串,我不能占。它成功创建的唯一帐户是一个为友发(下面的字符串的最后一个)。对于所有其他它给新-ADUser便有:对象名称有语法错误。互联网的研究显示了CSV-进口,其中数据具有空白等问题存在,但林不知道问题就在这里了许多错误。

There is something about using a here-string that I am not accounting for. The only account it successfully creates is the one for YHI (The last one of the here-string). For all others it gives New-AdUser : The object name has bad syntax. Internet research shows many errors for csv-imports where the data has whitespace and other issues there but im not sure what the issue is here.

在结束我只是做,而不是使用下面的字符串,但我想知道我在做什么错了一个CSV文件。

In the end I just made a csv file instead of using the here-string but I would like to know what i was doing wrong.

推荐答案

这为我工作。摆脱了空值和新行值,只是给了我各行各字符串值。接缝有可能是一些白色的空间,或者说干扰,如果你只是做拆分一些其他的字符`N

This worked for me. got rid of the null values and the new line values and just gave me each string value from each line. Seams there may have been some white space or some other characters that interfere if you just do split "`n"

$test = @"
user1
user2
user3
"@
$test.split("`r`n") | ForEach-Object {if($_){get-aduser $_}}

这篇关于新ADUser便有:对象名称有语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆