获取空值不知道为什么

Getting Null Value not sure why

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

$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 帐户名称,但如果它运行,我什么也得不到。

正在写答案给出代码示例,

正如 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 命令。

这是正确的代码。 Mathias 请 post 简单回答

"That's because the value of $S is larger than the length of the array, so $SAMs[$S] results in $null. You'll need to "reset" it around the same time you do Start-Sleep"

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

现在我必须 google Splatting 是什么,因为这是我第一次听到这个词。

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

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

2) 其次,这是我作为一个人的思考方式。我喜欢把问题分解成小块来解决它们。我也喜欢将 assemble 解决方案作为一个小块整合成一个整体。我意识到,当我需要从几个月或几年前编写的脚本中提取项目时,虽然我可以通过在一天结束时遵循您的风格来减少我的行数并提高效率,但我不必努力工作解码我采取的步骤。我可以将每一部分及其功能视为一小组代码。我承认我可能可以将创建用户的变量传递给 Add-Member 或从 CSV 中读取更好一点,但这是我第一次从 CSV 中自动创建活动目录帐户,所以我不得不了解如何分步进行。我 posted 前一个问题帮助我将 CSV 数据加载到数组中。我还了解到我可以使用列标题引用 CSV 数据。该脚本是一次很棒的学习经历。它还没有完成我现在必须添加 Exchange 配置步骤,所以我还必须学习如何正确地将 Exchange session 加载到我现有的 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++ }