无法通过PowerShell将用户添加到本地组

尝试导入macauthentication的大量用户列表。 我可以创build用户,但无论我尝试什么,我都不能将它们添加到本地组。 我不断地得到“一个无效的目录path名被传递”给add函数。

这是实际的错误:

Method invocation failed because [System.DirectoryServices.DirectoryEntry] does not contain a method named 'op_Addition'. At C:\Users\Administrator\Desktop\Import Script\Import1.ps1:12 char:2 + $objuser = [ADSI]"WinNT://RadiusSVR/" + $_.MAC + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound Exception calling "Add" with "1" argument(s): "An invalid directory pathname was passed $Computername = $env:COMPUTERNAME $objgroup = [ADSI]"WinNT://./TEST" $target = [ADSI]"WinNT://$Computername" Import-Csv testlist1.csv | ForEach-Object { $newuser = $target.Create("user", $_.MAC) $newuser.SetPassword($_.MAC) $newuser.SetInfo() $newuser.FullName = ($_.NAME) $newuser.SetInfo() $newuser.psbase.InvokeSet('AccountDisabled', $false) $newuser.SetInfo() $objuser = [ADSI]"WinNT://./" + $_.MAC $objgroup.Add($objuser) } 

我试过replaceWinNT://。 与WinNT:// RadiusSVR但同样的问题,如果我尝试WinNT:// $计算机名相同。

有人问之前,是的,我的意思是设置用户名和密码的Mac地址。 用户创build得很好,但是把它们放到所说的组中就是问题了。

尝试以下操作:

 $objuser = [ADSI]"WinNT://./$($_.MAC)" $objgroup.Add($objuser) 

是否有没有使用New-ADUser和Add-ADGroupMember cmdlet的原因?

尝试改变这一点

 [ADSI]"WinNT://./" + $_.MAC 

对此

 [ADSI]("WinNT://./" + $_.MAC) 

您正在将"WinNT://./"转换为ADSI对象,然后尝试添加$_.Mac "WinNT://./"而无法工作。 创build整个string,然后将其转换为ADSI对象。

最后在与包含$ objuser的行的多次迭代战斗后,有人build议在另一个论坛中这样做。 它工作,毫不费力。

无论我尝试什么其他方式是抛出types不匹配的错误。

 $Info = Import-CSV -Path "C:\Users\Administrator\Desktop\Import Script\testlist1.csv" foreach ($user in $Info) { #Create the user using the property from the CSV called Name net user $user.MAC /add; #Set the user's password to the property from the CSV called MAC net user $user.MAC $user.MAC #Actually add them to group net localgroup "TEST" /add $user.MAC}