Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell ADSI未正确验证?_Powershell_Variables_Cmdlet - Fatal编程技术网

Powershell ADSI未正确验证?

Powershell ADSI未正确验证?,powershell,variables,cmdlet,Powershell,Variables,Cmdlet,我有一个超过1500个用户的CSV,每个类OU有30个用户,然后每个类OU有6个类OU。我试图实现的是,foreach(CSV中的用户),检查OU是否存在,然后检查父OU是否存在,仅在不存在时创建父OU,然后创建OU,然后创建用户,或者如果OU存在,只创建用户 以下是我正在使用的代码: $ErrorActionPreference=“停止” 导入模块ActiveDirectory $CSV=导入CSV“C:\Scripts\addstudiors.CSV” foreach($CSV中的用户){

我有一个超过1500个用户的CSV,每个类OU有30个用户,然后每个类OU有6个类OU。我试图实现的是,
foreach(CSV中的用户)
,检查OU是否存在,然后检查父OU是否存在,仅在不存在时创建父OU,然后创建OU,然后创建用户,或者如果OU存在,只创建用户

以下是我正在使用的代码:

$ErrorActionPreference=“停止”
导入模块ActiveDirectory
$CSV=导入CSV“C:\Scripts\addstudiors.CSV”
foreach($CSV中的用户){
#变数
$GivenName=$user.GivenName
$LANSAME=$user.LANSAME
$UserName=$user.UserName
$Class=$user.YearClass
$INTANCE=$user.INTANCE
$DisplayName=$GivenName+“”+$name
#创建用户
$UserOUParent=“OU=Year”+$iIntake+“入学人数,OU=student,OU=Users,OU=Roding,DC=Zulbag,DC=com”
$UserOU=“OU=Class”+$Class.Substring(1,1)+”,OU=Year“+$iIntake+”入学人数,OU=Students,OU=Users,OU=Roding,DC=Zulbag,DC=com”
$NewUserOUParentCheck=[ADSI]::存在(“LDAP://$UserOUParent”)
$NewUserOUCheck=[ADSI]::存在(“LDAP://$UserOU”)
if($NewUserOUCheck-eq$false){
if($newUserOutparentcheck-eq$false){
“创建父Ou”
新组织`
-名称(“年份”+$摄入量+“摄入量”)`
-路径“OU=Students,OU=Users,OU=Roding,DC=Zulbag,DC=Com”`
-防止意外删除$False
}
“创建OU”
新组织`
-名称(“类”+$Class.Substring(1,1))`
-路径(“OU=Year”+$inctain+“inctain,OU=student,OU=Users,OU=Roding,DC=Zulbag,DC=Com”)`
-防止意外删除$False
}
“创建用户”
新成年人`
-名称$DisplayName`
-姓$姓`
-GivenName$GivenName`
-DisplayName$DisplayName`
-SamAccountName$UserName`
-UserPrincipalName($UserName+“@Zulbag.com”)`
-AccountPassword(转换为SecureString“Testing123”-AsPlainText-force)`
-无法更改密码$true`
-ChangePasswordAtLogon$false`
-PasswordNeverExpires$true`
-电子邮件地址($UserName+“@Zulbag.com”)`
-国家“GB”`
-路径(“OU=Class”+$Class.Substring(1,1)+”,OU=Year“+$inctain+”inctain,OU=student,OU=Users,OU=Roding,DC=Zulbag,DC=Com”)`
-档案路径(“D:\Shares\User Accounts\Students\Income Year”+$Income+“\Class”+$Class.Substring(1,1)+“\Profiles\”+$DisplayName)`
-启用$true
开始睡眠-5秒
#添加到组
$Group=“CN=Redirection”+$inctain.Substring(2,2)+$Class.Substring(1,1)+”,OU=inctain“+$inctain+”,OU=Security Group,OU=Roding,DC=Zulbag,DC=Com”
$GroupOU=“OU=intraction”+$intraction+”,OU=文件夹重定向组,OU=安全组,OU=Roding,DC=Zulbag,DC=Com”
$NewGroupCheck=[ADSI]::存在(“LDAP://$Group”)
$NewGroupOUCheck=[ADSI]::存在(“LDAP://$GroupOU”)
if($NewGroupCheck-eq$false){
if($NewGroupOUCheck-eq$false){
“创建OU”
新组织`
-名称(“摄入量”+$摄入量)`
-路径“OU=文件夹重定向组,OU=安全组,OU=Roding,DC=Zulbag,DC=Com”`
-防止意外删除$False
}
“创建组”
新广告集团`
-名称(“重定向”+$INTANCE.Substring(2,2)+$Class.Substring(1,1))`
-GroupScope“全球”`
-路径(“OU=intraction”+$intraction+”,OU=文件夹重定向组,OU=安全组,OU=Roding,DC=Zulbag,DC=Com”)
}
“添加成员”
添加ADGroupMember(“重定向”+$ADINTAIN.Substring(2,2)+$Class.Substring(1,1))$UserName
}
暂停
示例CSV:

GivenName,SurName,Class,UserName,Intake Ali,Grisdale,1B,AGris,2016 Ayomiposi,Olayera,1B,AOlay,2016 它成功创建父OU、类OU和用户,但未能创建第二个用户,而是再次错误地验证OU


有什么想法吗?

检查脚本中的路径错误

这是倒退。首先测试OU是否存在,如果缺少则创建它,然后创建新用户。抱歉,我的错误,我没有提到我将为1500多个用户执行导入,每个OU将有30个用户,我尝试的是仅在OU不存在时创建OU,然后在该OU中创建其余用户,它可以在不将cmdlet放入变量的情况下工作,这不会改变您的方法是向后的。再次测试OU是否存在。如果缺少,请创建它。然后创建新用户。这对我来说不太好,我使用[ADSI]::Exists(“LDAP://OU,但这一直给我错误的结果,它创建第一个父OU,然后是OU,然后是用户,然后尝试在下一个用户上创建同一个父OU(不应该),有什么建议吗?我甚至使用if($test-eq$false)这是另一个线程上建议的。请回答您的问题,并显示更新的代码以及错误消息。 New-ADOrganizationalUnit : An attempt was made to add an object to the directory with a name that is already in use At C:\Scripts\AddPupils-Afzal.ps1:24 char:13 + New-ADOrganizationalUnit + ~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (OU=Year 2016 In...C=Zulbag,DC=Com:String) [New-ADOrganizationalUnit], ADException + FullyQualifiedErrorId : ActiveDirectoryServer:8305,Microsoft.ActiveDirectory.Management.Commands.NewADOrganizationalUnitdirectory