-1

我的脚本遇到了这个奇怪的问题。它可以很好地创建用户,但是当我添加组成员身份时,它似乎无法正常工作。这很奇怪,我不知道为什么会出现空值错误。

$Path = "C:\T2\BulkUsers\AccountProvisioningTemplate.csv"
$CSVs = Import-CSV $Path
$Group1 = 'G1'
$Group2 = "G2"

$FirstNames = @()
$Initials = @()
$LastNames = @()
$FullNames = @()
$DisplayNames = @()
$EmployeeNumbers = @()
$TelephoneNumbers = @()
$SAMs = @()
$UPNs = @()
$Companys = @()
$Departments = @()
$Offices = @()
$Citys = @()
$States = @()
$Zips = @()
$Titles = @()
$Contractor = @()


ForEach($CSV in $CSVs) { $FirstNames += ($CSV.FirstName) }
ForEach($CSV in $CSVs) { $Initials += ($CSV.Initials) }
ForEach($CSV in $CSVs) { $LastNames += ($CSV.LastName) }
ForEach($CSV in $CSVs) { $FullNames += ($CSV.FullName) }
ForEach($CSV in $CSVs) { $DisplayNames += ($CSV.DisplayName) }
ForEach($CSV in $CSVs) { $EmployeeNumbers += ($CSV.EmployeeNumber) }
ForEach($CSV in $CSVs) { $TelephoneNumbers += ($CSV.TelephoneNumber) }
ForEach($CSV in $CSVs) { $SAMs += ($CSV.SAM) }
ForEach($CSV in $CSVs) { $UPNs += ($CSV.UPN) }
ForEach($CSV in $CSVs) { $Companys += ($CSV.Company) }
ForEach($CSV in $CSVs) { $Departments += ($CSV.Department) }
ForEach($CSV in $CSVs) { $Offices += ($CSV.Office) }
ForEach($CSV in $CSVs) { $Citys += ($CSV.City) }
ForEach($CSV in $CSVs) { $States += ($CSV.State) }
ForEach($CSV in $CSVs) { $Zips += ($CSV.Zip) }
ForEach($CSV in $CSVs) { $Titles += ($CSV.Title) }
ForEach($CSV in $CSVs) { $Contractor += ($CSV.Contractor) }

$FN = 0
$IN = 0
$LN = 0
$FN1 = 0
$DN = 0
$EN = 0
$TN = 0
$S = 0
$U = 0
$Comp = 0
$Dept = 0
$Off = 0
$C = 0
$S = 0
$Z = 0
$T = 0
$ConC = 0
$Con = "No"

$OU = "OU=General Users & Computers,DC=corp,DC=abc,DC=com"
$Password1 = ConvertTo-SecureString "Changeme123" -AsPlainText -Force

ForEach($CSV in $CSVs) { New-ADUser -GivenName $CSVs.FirstName[$FN] -Initials $CSVs.Initials[$IN] -Surname $CSVs.LastName[$LN] -name $CSVs.FullName[$FN1] -DisplayName $CSVs.DisplayName[$DN] -EmployeeID $CSVs.EmployeeNumber[$EN] -EmployeeNumber $CSVs.EmployeeNumber[$EN] -OfficePhone $CSVs.TelephoneNumber[$TN] -SamAccountName $CSVs.SAM[$S] -UserPrincipalName $CSVs.UPN[$U] -Path $OU -Company $CSVs.Company[$Comp] -Department $CSVs.Department[$Dept] -Office $CSVs.Office[$Off] -City $CSVs.City[$C] -State $CSVs.State[$S] -PostalCode $CSVs.Zip[$Z] -Title $CSVs.Title[$T] -AccountExpirationDate $null -AccountPassword $Password1 -CannotChangePassword $False -ChangePasswordAtLogon $False -Enabled $True; $FN++; $IN++; $LN++; $FN1++; $DN++; $EN++; $TN++; $S++; $U++; $Comp++; $Dept++; $Off++; $C++; $S++; $Z++; $T++ }

Start-Sleep 2

ForEach($CSV in $CSVs) { IF ($Contractor[$ConC] -Match $Con) { Add-ADGroupMember -Identity $Group1 -Members $CSVs.SAM[$S]; Add-ADGroupMember -Identity $Group2 -Members $CSVs.SAM[$S]; $S++ } ELSE { $S++ } ; $CSVs.SAM[$S]; $SAMs[$S]; $ConC++ }

我收到错误消息,但不知道为什么。它可以很好地创建用户,当我输入对 $SAMs[$] 的引用时,它会在 CSV 中为用户报告正确的 SamAccountName。

Add-ADGroupMember : Cannot validate argument on parameter 'Members'. The argument is null or empty. Provide an
argument that is not null or empty, and then try the command again.
At C:\T2\BulkUsers\BulkUserImport.ps1:69 char:120
+ ... roup1 -Members $SAMs[$S]; Add-ADGroupMember -Identity $Group2 -Members $SAMs[$S] ...
+                    ~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Add-ADGroupMember], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.AddADGrou
   pMember

Add-ADGroupMember : Cannot validate argument on parameter 'Members'. The argument is null or empty. Provide an
argument that is not null or empty, and then try the command again.
At C:\T2\BulkUsers\BulkUserImport.ps1:69 char:176
+ ... roup2 -Members $SAMs[$S]; $S++ } ELSE { $S++ } ; $ConC++ }
+                    ~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Add-ADGroupMember], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.AddADGrou
   pMember

Add-ADGroupMember : Cannot validate argument on parameter 'Members'. The argument is null or empty. Provide an
argument that is not null or empty, and then try the command again.
At C:\T2\BulkUsers\BulkUserImport.ps1:69 char:120
+ ... roup1 -Members $SAMs[$S]; Add-ADGroupMember -Identity $Group2 -Members $SAMs[$S] ...
+                    ~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Add-ADGroupMember], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.AddADGrou
   pMember

Add-ADGroupMember : Cannot validate argument on parameter 'Members'. The argument is null or empty. Provide an
argument that is not null or empty, and then try the command again.
At C:\T2\BulkUsers\BulkUserImport.ps1:69 char:176
+ ... roup2 -Members $SAMs[$S]; $S++ } ELSE { $S++ } ; $ConC++ }
+                    ~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Add-ADGroupMember], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.AddADGrou
   pMember

有趣的是,如果我注释掉用户创建部分,我会得到正确的 SAM 帐户名称,但如果它运行,我什么也得不到。

4

2 回答 2

2

写一个答案来给出一个代码示例,

正如 Mathias 所说,您太努力了,我不知道您为什么会收到错误,但我相信您应该只对用户的所有属性执行一次 foreach。

CSV 文件:

Firstname|Lastname|OU|SAM|Description|Password
John|Smith01|"OU=Users,DC=domain,DC=com"|John.Smith01|Test Account 01|Passw0rd
John|Smith02|"OU=Users,DC=domain,DC=com"|John.Smith01|Test Account 02|Passw0rd
John|Smith03|"OU=Users,DC=domain,DC=com"|John.Smith01|Test Account 03|Passw0rd
John|Smith04|"OU=Users,DC=domain,DC=com"|John.Smith01|Test Account 04|Passw0rd
John|Smith05|"OU=Users,DC=domain,DC=com"|John.Smith01|Test Account 05|Passw0rd

代码:

$Users = Import-Csv -Delimiter "|" -Path "C:\Setup\Users.csv"            
foreach ($User in $Users)            
{            
    $Displayname = $User.Firstname + " " + $User.Lastname            
    $UserFirstname = $User.Firstname            
    $UserLastname = $User.Lastname            
    $OU = $User.OU          
    $SAM = $User.SAM            
    $UPN = $User.Firstname + "." + $User.Lastname + "@domain.com"           
    $Description = $User.Description            
    $Password = $User.Password            
    New-ADUser -Name "$Displayname" -DisplayName "$Displayname" -SamAccountName $SAM -UserPrincipalName $UPN -GivenName "$UserFirstname" -Surname "$UserLastname" -Description "$Description" -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force) -Enabled $true -Path "$OU" -ChangePasswordAtLogon $false –PasswordNeverExpires $true 
}

希望这可以帮助您并让您的用户启动并运行。如果您还需要添加组,您也可以使用 Add-ADGroupMember/Remove-ADGroupMember 命令来执行此操作。

于 2018-07-16T13:59:19.147 回答
0

这是正确的代码。Mathias 请发布一个简单的答案

“那是因为 $S 的值大于数组的长度,所以 $SAMs[$S] 的结果是 $null。你需要在开始睡眠的同时“重置”它”

所以我可以奖励你。我实际上有一些错误。就像我双重使用了已更改为 $SAM 的变量 $S,以免与状态 $S 冲突。此外,我的 CSV 有点高,但主要问题是未能清除关键的 $S($SAM) 的值。如果您像我一样分解代码,很容易忘记这样做。

现在我将不得不在谷歌上搜索什么是 Splatting,因为这是我第一次听到这个词。

然而,我想用一些话向所有对我为什么把它弄得如此复杂感到沮丧的人解释一下。实际上有三个不同的原因。

1)我不是一个超级熟练的 powershell 脚本编写者。是的,我不是试图编写主机“Hello World”的菜鸟,但我也不是程序员,显然我来这里是为了向像你们这样的先生和女士们学习,他们显然比我更精通 Powershell。

2)其次,这是我作为一个人的思考方式。我喜欢将问题分解成碎片以解决它们。我也喜欢将解决方案作为小块组装成一个整体。我意识到,虽然我可以在一天结束时遵循您的风格来减少行数并提高效率,当我需要从几个月或几年前编写的脚本中提取项目时,我不必努力工作解码我采取的步骤。我可以看到每一部分以及它作为小组代码的作用。我承认我可能可以将变量从创建用户传递到添加成员,或者从 CSV 读取更好一些,但这是我第一次从 CSV 自动创建活动目录帐户,所以我必须学习如何分步进行。我发布了 1 个先前的问题,帮助我将 CSV 数据加载到数组中。我还了解到我可以使用列标题引用 CSV 数据。剧本是一次很棒的学习经历。此外,它还没有完成,我现在必须添加 Exchange 配置步骤,因此我还必须学习如何将 Exchange 会话正确加载到我现有的 Powershell 中,这是我去年苦苦挣扎数周但从未完全正确的事情。

3)第三个也是最后一个原因是,有一天我可能会离开这份工作,我希望我之后的 IT 人员(他们可能没有像我那样编写脚本的能力)能够将这些东西分解成碎片,然后看看这些部分组合在一起,以便他们可以学习和维护我所写的内容。有时当我在这里阅读代码时,我会学到很多东西(但我在 Powershell 方面有扎实的基础)。尽管我必须承认,你们编写的一些代码对我来说仍然像是黑魔法,因为它既强大又简单。各位先生女士们非常有才华,我感谢你们给我的所有帮助。我希望有一天能达到你的水平,但实际上我不是一名脚本编写者或程序员。我是 Active Directory、Exchange、SAN、虚拟化、Citrix 和整体基础架构工程师。在过去一年左右的时间里,我开始认真学习 Powershell,这意味着我在脚本游戏中基本上是个蹒跚学步的孩子。我正在尽我所能地成长,你的每一点知识都会有所帮助,但我写这一切是为了说服你接受这样一个事实,即你们比我们中的一些人处于更高的水平,如果你能接受的话那么也许你会明白为什么我没有做你在给定情况下会做的事情。

无论如何,这是我承诺的最终代码

$Path = "C:\T2\BulkUsers\AccountProvisioningTemplate.csv"
$CSVs = Import-CSV $Path
$Group1 = 'G1'
$Group2 = "G2"

$FirstNames = @()
$Initials = @()
$LastNames = @()
$FullNames = @()
$DisplayNames = @()
$EmployeeNumbers = @()
$TelephoneNumbers = @()
$SAMs = @()
$UPNs = @()
$Companys = @()
$Departments = @()
$Offices = @()
$Citys = @()
$States = @()
$Zips = @()
$Titles = @()
$Contractor = @()


ForEach($CSV in $CSVs) { $FirstNames += ($CSV.FirstName) }
ForEach($CSV in $CSVs) { $Initials += ($CSV.Initials) }
ForEach($CSV in $CSVs) { $LastNames += ($CSV.LastName) }
ForEach($CSV in $CSVs) { $FullNames += ($CSV.FullName) }
ForEach($CSV in $CSVs) { $DisplayNames += ($CSV.DisplayName) }
ForEach($CSV in $CSVs) { $EmployeeNumbers += ($CSV.EmployeeNumber) }
ForEach($CSV in $CSVs) { $TelephoneNumbers += ($CSV.TelephoneNumber) }
ForEach($CSV in $CSVs) { $SAMs += ($CSV.SAM) }
ForEach($CSV in $CSVs) { $UPNs += ($CSV.UPN) }
ForEach($CSV in $CSVs) { $Companys += ($CSV.Company) }
ForEach($CSV in $CSVs) { $Departments += ($CSV.Department) }
ForEach($CSV in $CSVs) { $Offices += ($CSV.Office) }
ForEach($CSV in $CSVs) { $Citys += ($CSV.City) }
ForEach($CSV in $CSVs) { $States += ($CSV.State) }
ForEach($CSV in $CSVs) { $Zips += ($CSV.Zip) }
ForEach($CSV in $CSVs) { $Titles += ($CSV.Title) }
ForEach($CSV in $CSVs) { $Contractor += ($CSV.Contractor) }

$FN = 0
$IN = 0
$LN = 0
$FN1 = 0
$DN = 0
$EN = 0
$TN = 0
$SAM = 0
$U = 0
$Comp = 0
$Dept = 0
$Off = 0
$C = 0
$S = 0
$Z = 0
$T = 0


$OU = "OU=General Users & Computers,DC=corp,DC=abc,DC=com"
$Password1 = ConvertTo-SecureString "Changeme123" -AsPlainText -Force

ForEach($CSV in $CSVs) { New-ADUser -GivenName $CSVs.FirstName[$FN] -Initials $CSVs.Initials[$IN] -Surname $CSVs.LastName[$LN] -name $CSVs.FullName[$FN1] -DisplayName $CSVs.DisplayName[$DN] -EmployeeID $CSVs.EmployeeNumber[$EN] -EmployeeNumber $CSVs.EmployeeNumber[$EN] -OfficePhone $CSVs.TelephoneNumber[$TN] -SamAccountName $CSVs.SAM[$SAM] -UserPrincipalName $CSVs.UPN[$U] -Path $OU -Company $CSVs.Company[$Comp] -Department $CSVs.Department[$Dept] -Office $CSVs.Office[$Off] -City $CSVs.City[$C] -State $CSVs.State[$S] -PostalCode $CSVs.Zip[$Z] -Title $CSVs.Title[$T] -AccountExpirationDate $null -AccountPassword $Password1 -CannotChangePassword $False -ChangePasswordAtLogon $False -Enabled $True; $CSVs.SAM[$SAM]; $SAM; $FN++; $IN++; $LN++; $FN1++; $DN++; $EN++; $TN++; $SAM++; $U++; $Comp++; $Dept++; $Off++; $C++; $S++; $Z++; $T++ }

Start-Sleep 2

$ConC = 0
$Con = "No"
$SAM = 0

 "`n`n"

ForEach($CSV in $CSVs) { IF ($Contractor[$ConC] -Match $Con) { Add-ADGroupMember -Identity $Group1 -Members $CSVs.SAM[$SAM]; Add-ADGroupMember -Identity $Group2 -Members $CSVs.SAM[$SAM]; $SAM++ } ELSE { $SAM++ } ; $ConC++ }
于 2018-07-16T15:08:33.517 回答