获取空值不确定为什么

时间:2018-07-16 13:45:36

标签: arrays powershell import-csv

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

$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帐户名,但是如果运行它,我将一无所获。

2 个答案:

答案 0 :(得分: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命令来完成。

答案 1 :(得分:0)

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

  

“这是因为$ S的值大于$ S的长度   数组,因此$ SAMs [$ S]结果为$ null。您需要将其“重置”   您同时进行“开始睡眠”

所以我可以奖励您。我实际上有一些错误。就像我双重使用了已更改为$ SAM的变量$ S一样,以免与State $ S冲突。另外我的CSV有点麻烦,但主要问题是未能清除作为关键的$ S($ SAM)的值。如果您按照我的方式破坏代码,就很容易忘记这样做。

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

但是,我想对所有人感到沮丧,以解释为什么我将其变得如此复杂。实际上有三个不同的原因。

1)我不是超级熟练的powershell脚本编写者。是的,我不是试图编写主机“ Hello World”的菜鸟,但我也不是程序员,很显然,我是在这里向绅士和女士们学习,例如你们自己,他们对Powershell的掌握明显超过了我。

2)其次,这是我作为人的思考方式。我喜欢将问题分解为多个部分以便解决。我还喜欢将解决方案作为小块组装成一个整体。我意识到,当我需要从几个月或几年前编写的脚本中提取内容时,可以通过遵循您的样式来减少行数并提高效率,但是我不必费劲解码我已采取的步骤。我可以看到每一段代码以及它们作为一小组代码的作用。我承认我可能可以将变量从创建用户传递到添加成员,或者更好地从CSV读取,但这是我第一次从CSV自动创建活动目录帐户,因此我必须学习如何分步进行。我发布了1个先前的问题,可帮助我将CSV数据加载到数组中。我还了解到可以使用列标题引用CSVs数据。该脚本是一次很棒的学习经历。同样,它仍然没有完成,我现在必须添加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++ }